Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. A COM Interop Question

A COM Interop Question

Scheduled Pinned Locked Moved C#
comquestioncsharpasp-netannouncement
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    Xiaoming Qian
    wrote on last edited by
    #1

    Look at the following code Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application(); ... ExcelApp.Quit(); The Excel object is not release after 'Quit()' method(Excel is still in the background process list).I think it is because there is still a reference of the object. I a windows application,this will be solved when I close the application. But I'm using these code in an asp.net application.Every time they execute,the background process list grows. Is there any way to relase the object or Is there any code to read excel files without using Microsoft Excel?

    A G S 3 Replies Last reply
    0
    • X Xiaoming Qian

      Look at the following code Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application(); ... ExcelApp.Quit(); The Excel object is not release after 'Quit()' method(Excel is still in the background process list).I think it is because there is still a reference of the object. I a windows application,this will be solved when I close the application. But I'm using these code in an asp.net application.Every time they execute,the background process list grows. Is there any way to relase the object or Is there any code to read excel files without using Microsoft Excel?

      A Offline
      A Offline
      AhsanS
      wrote on last edited by
      #2

      Set it to null first.

      Ahsan Ullah Senior Software Engineer

      1 Reply Last reply
      0
      • X Xiaoming Qian

        Look at the following code Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application(); ... ExcelApp.Quit(); The Excel object is not release after 'Quit()' method(Excel is still in the background process list).I think it is because there is still a reference of the object. I a windows application,this will be solved when I close the application. But I'm using these code in an asp.net application.Every time they execute,the background process list grows. Is there any way to relase the object or Is there any code to read excel files without using Microsoft Excel?

        G Offline
        G Offline
        Green Fuze
        wrote on last edited by
        #3

        When using COM via .Net you need to make sure you release all instances, and point to null when done. check this out: http://support.microsoft.com/default.aspx?scid=kb;en-us;317109[^] It explains the problem and the resolution. Notice that you might also need to release workbooks, depends on your code.

        X 1 Reply Last reply
        0
        • X Xiaoming Qian

          Look at the following code Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application(); ... ExcelApp.Quit(); The Excel object is not release after 'Quit()' method(Excel is still in the background process list).I think it is because there is still a reference of the object. I a windows application,this will be solved when I close the application. But I'm using these code in an asp.net application.Every time they execute,the background process list grows. Is there any way to relase the object or Is there any code to read excel files without using Microsoft Excel?

          S Offline
          S Offline
          selcuks
          wrote on last edited by
          #4

          Here is a code piece from Visio Code Librarian:

          /// This method releases all references to a COM object. When
          /// Visual Studio .NET calls a COM object from managed code, it
          /// automatically creates a Runtime Callable Wrapper (RCW). The RCW
          /// marshals calls between the .NET application and the COM object. The
          /// RCW keeps a reference count on the COM object. Calling
          /// ReleaseComObject when you are finished using an object will cause
          /// the reference count of the RCW to be decremented.
          /// param name="runtimeObject": The runtime callable wrapper whose
          /// underlying COM object will be released.
          private void NullAndRelease(object runtimeObject)
          {
          try
          {

              if (runtimeObject != null)
              {
          
                  // The RCW's reference count gets incremented each time the
                  // COM pointer is passed from unmanaged to managed code.
                  // Call ReleaseComObject in a loop until it returns 0 to be
                  // sure that the underlying COM object gets released.
                  int referenceCount = System.Runtime.InteropServices.
                      Marshal.ReleaseComObject(runtimeObject);
          
                  while (0 < referenceCount)
                  {
                      referenceCount = System.Runtime.InteropServices.
                          Marshal.ReleaseComObject(runtimeObject);
                  }
              }
          

          }
          finally
          {
          runtimeObject = null;
          }
          }

          I think you can also use this one for Excel too. Regards,

          Always keep the Murphy Rules in mind!

          modified on Thursday, August 21, 2008 6:15 AM

          J 1 Reply Last reply
          0
          • S selcuks

            Here is a code piece from Visio Code Librarian:

            /// This method releases all references to a COM object. When
            /// Visual Studio .NET calls a COM object from managed code, it
            /// automatically creates a Runtime Callable Wrapper (RCW). The RCW
            /// marshals calls between the .NET application and the COM object. The
            /// RCW keeps a reference count on the COM object. Calling
            /// ReleaseComObject when you are finished using an object will cause
            /// the reference count of the RCW to be decremented.
            /// param name="runtimeObject": The runtime callable wrapper whose
            /// underlying COM object will be released.
            private void NullAndRelease(object runtimeObject)
            {
            try
            {

                if (runtimeObject != null)
                {
            
                    // The RCW's reference count gets incremented each time the
                    // COM pointer is passed from unmanaged to managed code.
                    // Call ReleaseComObject in a loop until it returns 0 to be
                    // sure that the underlying COM object gets released.
                    int referenceCount = System.Runtime.InteropServices.
                        Marshal.ReleaseComObject(runtimeObject);
            
                    while (0 < referenceCount)
                    {
                        referenceCount = System.Runtime.InteropServices.
                            Marshal.ReleaseComObject(runtimeObject);
                    }
                }
            

            }
            finally
            {
            runtimeObject = null;
            }
            }

            I think you can also use this one for Excel too. Regards,

            Always keep the Murphy Rules in mind!

            modified on Thursday, August 21, 2008 6:15 AM

            J Offline
            J Offline
            JoeRip
            wrote on last edited by
            #5

            There is no reason to call ReleaseComObject in a loop. Call FinalReleaseCOMObject instead, it sets the ref count directly to zero.

            1 Reply Last reply
            0
            • G Green Fuze

              When using COM via .Net you need to make sure you release all instances, and point to null when done. check this out: http://support.microsoft.com/default.aspx?scid=kb;en-us;317109[^] It explains the problem and the resolution. Notice that you might also need to release workbooks, depends on your code.

              X Offline
              X Offline
              Xiaoming Qian
              wrote on last edited by
              #6

              Thank you for your reply.But I stiil face a problem: private void NullAndRelease(object runtimeObject) { if (runtimeObject == null) return; try { System.Runtime.InteropServices.Marshal.FinalReleaseComObject(runtimeObject); } finally { runtimeObject = null; } } private void button1_Click(object sender, EventArgs e) { Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application(); object NullParam = System.Reflection.Missing.Value; Workbook Book = ExcelApp.Workbooks.Open(FileName, 0, true, NullParam, NullParam, NullParam, true, NullParam, NullParam, false, false, NullParam, false, true, NullParam); Worksheet Sheet = Book.Worksheets[1] as Worksheet; Range UsedRange = Sheet.UsedRange; Range Cell = null; int RowCount = UsedRange.Rows.Count; for (int i = 2; i <= RowCount; i++) { Cell = UsedRange.Cells[i, 1] as Microsoft.Office.Interop.Excel.Range; Cell = UsedRange.Cells[i, 2] as Microsoft.Office.Interop.Excel.Range; Cell = UsedRange.Cells[i, 3] as Microsoft.Office.Interop.Excel.Range; Cell = UsedRange.Cells[i, 4] as Microsoft.Office.Interop.Excel.Range; Cell = UsedRange.Cells[i, 5] as Microsoft.Office.Interop.Excel.Range; Cell = UsedRange.Cells[i, 6] as Microsoft.Office.Interop.Excel.Range; NullAndRelease(Cell); } NullAndRelease(Cell); NullAndRelease(UsedRange); NullAndRelease(Sheet); if (Book != null) Book.Close(false, NullParam, false); NullAndRelease(Book); ExcelApp.Quit(); NullAndRelease(ExcelApp); GC.Collect(); GC.WaitForPendingFinalizers(); } These code works well.But when I add the following code in the for loop: Cell = UsedRange.Cells[i, 7] as Microsoft.Office.Interop.Excel.Range; The server does not shut down. It seems that I can only read not more than 6 cells.That's why?

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups