Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / January 2005

Tip: Looking for answers? Try searching our database.

.NET / Excel Hell

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
pearsons_11114 - 26 Jan 2005 00:59 GMT
And I thought COM to Excel was a pain.  Having eventually discovered the tech
note that lays out the idiom for referencing Excel COM objects from .NET, I'm
still getting the familiar memory errors on program termination under certain
circumstances. The problem seems to revolve around passing Excel references
across procedure or method boundaries. I've included some sample code that
reproduces the problem consistently.  It only occurs when the loop is
executed repeatedly and if the application object passed into the function
call (as shown below). If the application object setup and teardown is moved
into the function, then no problem. This came up repeatedly in trying to
write some wrapper classes that would hide the tedious required idiom.  Is
this a futile quest? In effect, can Excel objects only be acccessed in local
variables from .NET? Thanks!
--
Module TestExcel
 Public Sub Main()
   Dim Application As New Excel.Application

   For i As Integer = 1 To 2
     Debug.WriteLine(GetCellValue(Application))
   Next

   Application.Quit()
   System.Runtime.InteropServices.Marshal.ReleaseComObject(Application)
 End Sub

 Public Function GetCellValue(ByVal Application As Excel.Application) As
String
   Dim Workbooks As Excel.Workbooks = Application.Workbooks
   Dim Workbook As Excel.Workbook = Workbooks.Add([some file name])
   Dim Worksheets As Excel.Sheets = Workbook.Worksheets
   Dim Worksheet As Excel.Worksheet = Worksheets(1)
   Dim Range As Excel.Range = Worksheet.Cells(1, 1)
   Dim rtn As String = Range.Value
   System.Runtime.InteropServices.Marshal.ReleaseComObject(Range)
   System.Runtime.InteropServices.Marshal.ReleaseComObject(Worksheet)
   System.Runtime.InteropServices.Marshal.ReleaseComObject(Worksheets)
   Workbook.Close(False)
   System.Runtime.InteropServices.Marshal.ReleaseComObject(Workbook)
   System.Runtime.InteropServices.Marshal.ReleaseComObject(Workbooks)
   Return rtn
 End Function
End Module
pearsons_11114 - 26 Jan 2005 01:35 GMT
Using Excel 9 / 2000, BTW.
Dino Chiesa [Microsoft] - 26 Jan 2005 19:51 GMT
maybe you have a timing issue, because when I try that code in my machine,
no memory errors occur.   I am using Excel 2003.

> Using Excel 9 / 2000, BTW.
pearsons_11114 - 26 Jan 2005 20:21 GMT
Well, exactly. That's why they call it "non-deterministic". ;-)

Seriously, the randomness of garbage collection is the challenge with these
types of errors. Try varying the loop count, especially raising it quite a
bit. Clearly ReleaseComObject is failing under certain circumstances.  
Forcing garbage collection just before exiting the function solves the
problem, both in this example and in my actual application. In my actual
application the problem is occurring inside a loop, so not a viable
workaround.  Since you are using Excel 2003, perhaps there is a problem
specific to 2000. What I want to find out is whether  my usage is incorrect
or if it is a bug.  Your display name says "Microsoft", so perhaps you can
try it out on 2000? Any insight would be appreciated.

Just in passing I remark that after perusing this group and the tech
articles I'm rather astounded at what  a low priority MS seems to have given
Office interop, as opposed to ADO, which they clearly spent a lot of time
thinking about. I would think Office would be at least as much at the core of
the .NET strategy. Even if the recommend usage worked all the time, it's
incredibly awkward to use. If I can write a decent wrapper in a day or two,
you'd think it would be easy for MS.

> maybe you have a timing issue, because when I try that code in my machine,
> no memory errors occur.   I am using Excel 2003.
Chuck Heatherly - 28 Jan 2005 14:50 GMT
>Well, exactly. That's why they call it "non-deterministic". ;-)
>
>Seriously, the randomness of garbage collection is the challenge with these
>types of errors. Try varying the loop count, especially raising it quite a
>bit. Clearly ReleaseComObject is failing under certain circumstances.  

I tried the code sample using Excel 2000 and also didn't see any memory errors,
running the application through the IDE and from the command line.

For what it's worth, the MSDN doc for ReleaseComObject says this:

<quote>
[Visual Basic]
Public Shared Function ReleaseComObject(ByVal o As Object) As Integer

Return Value
The new value of the reference count of the runtime callable wrapper associated
with o. This value is typically zero since the runtime callable wrapper keeps
just one reference to the wrapped COM object regardless of the number of managed
clients calling it.

Note:  To ensure that the runtime callable wrapper and the original COM object
are released, construct a loop from which you call this method until the
returned reference count reaches zero.
</quote>

I use ReleaseComObject with one of my programs that use ADODB objects, and when
I wrote a method like the note suggests, I noticed that the loop sometimes took
5 or 6 iterations per COM object.  So see if this helps.

Chuck

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.