Unload a COM DLL from C# 2005

T

TimKnoll

I need to unload and load a COM DLL for multiple calls. The VC6 DLL's
are using static fileds which is causing me problems. I'm currently
implementing IDisposable using this as my Dispose method:

public void Dispose()
{
int referenceCount = 0;
do
{
referenceCount = Marshal.ReleaseComObject(obj);
}
while (referenceCount > 0);

obj = null;
}

I confirmed that the method is being called. But I can still see the
COM DLL in memory after Dispose is run. The next time the COM DLL is
called, its still in memory which causes problems. Changing the
legicy VC6 is not an option.

Any ideas? Thanks in advance for your help.

Tim.
 
N

Nicholas Paldino [.NET/C# MVP]

Tim,

Your implementation of Dispose isn't going to do anything. If anything,
you should be a little more diligent about the references that are being
passed around and calling ReleaseComObject on them when you are done (unless
this class has a member for a COM component which is not exposed outside the
class, in which case, you justwant to call ReleaseComObject in the Dispose
method).

That being said, you can try and call CoFreeUnusedLibraries through the
P/Invoke layer when you want to try and unload the COM dll.
 
L

Laura T.

I agree with Nicholas.
One other thing you might try is Marshal.FinalReleaseComObject() that kills
all RCWs for the COM object.
But it's not a cure for dangling COM references. You should find out who is
still keeping the COM object alive.

Nicholas Paldino said:
Tim,

Your implementation of Dispose isn't going to do anything. If
anything, you should be a little more diligent about the references that
are being passed around and calling ReleaseComObject on them when you are
done (unless this class has a member for a COM component which is not
exposed outside the class, in which case, you justwant to call
ReleaseComObject in the Dispose method).

That being said, you can try and call CoFreeUnusedLibraries through the
P/Invoke layer when you want to try and unload the COM dll.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I need to unload and load a COM DLL for multiple calls. The VC6 DLL's
are using static fileds which is causing me problems. I'm currently
implementing IDisposable using this as my Dispose method:

public void Dispose()
{
int referenceCount = 0;
do
{
referenceCount = Marshal.ReleaseComObject(obj);
}
while (referenceCount > 0);

obj = null;
}

I confirmed that the method is being called. But I can still see the
COM DLL in memory after Dispose is run. The next time the COM DLL is
called, its still in memory which causes problems. Changing the
legicy VC6 is not an option.

Any ideas? Thanks in advance for your help.

Tim.
 
T

TimKnoll

I added a call to ReleaseComObject via PInvoke and it worked. I
removed the Dispose code and added this code immediatley after the
call to COM app. It looks something like this:

obj.RunApp
Marshal.ReleaseComObject(pfg);
obj = null;
Utility.CoFreeUnusedLibraries();

I now can make my multiple calls to the COM object without any
problems. Let me know if you think this is OK. I appreciate both of
your quick and helpful responces.

Tim.
 
N

Nicholas Paldino [.NET/C# MVP]

Tim,

ReleaseComObject, or CoFreeUnusedLibraries? I think you meant the
latter...

I am curious, why are you setting the reference to null on obj? It
seems superfluous.
 
T

TimKnoll

Sorry, I meant CoFreeUnusedLibraries. I was setting the object to
null since I saw it done in many posts while searching on this
subject. I just removed it and it works just fine without doing it.
Thank you.

Tim.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top