473,395 Members | 2,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Unload a COM DLL from C# 2005

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.

Oct 2 '07 #1
5 3822
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]
- mv*@spam.guard.caspershouse.com

<Ti******@gmail.comwrote in message
news:11**********************@o80g2000hse.googlegr oups.com...
>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.

Oct 2 '07 #2
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comha
scritto nel messaggio news:OO**************@TK2MSFTNGP03.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

<Ti******@gmail.comwrote in message
news:11**********************@o80g2000hse.googlegr oups.com...
>>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.

Oct 2 '07 #3
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.

Oct 2 '07 #4
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<Ti******@gmail.comwrote in message
news:11**********************@w3g2000hsg.googlegro ups.com...
>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.

Oct 2 '07 #5
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.
Oct 2 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Harry J. Smith | last post by:
I have written a Visual Basic program that does a long calculation and writes the results to disk as it runs. If I click the Close button the window closes but the program keeps running. How can I...
0
by: razheev | last post by:
Hi, I have a Table which contains 1 million rows. I want to do an unload of the table and do some massaging (tranforming) of data and do a load to a different table where the column attributes are...
2
by: Lauren Hines | last post by:
Hello, I have read numerous post stating that the only way to unload an assembly (DLL in my case) is to create a separate AppDomain, load the assembly, then unload it by calling AppDomain.Unload....
4
by: Claudia Fong | last post by:
Hi, I have more than one forms in my program, and I would like to for example, in form1, I will call form2 and in form2 it will call form3. But the problem is when I call form2.Show, it does...
1
by: hal | last post by:
I have an application that includes an activex component that consumes resources that must be released when the a page is unloaded. Toward this end I subscribe to the unload event of the body...
1
by: Hal | last post by:
My most sincere gratitude to anyone who can help me work around this! I have work that needs to be done in javascript on the client whenever a page is unloaded. To this end, I subscribe to...
3
by: Gauthier Segay | last post by:
Hello, I've an application where all my pages implement a PAGE_CODE string property, this property is stored in HttpContext.Current.Items. In some page, I must persist data in session while...
2
by: sam | last post by:
I want to know how to unload form / application in VB.Net. In VB 6, the coding is Unload Me but how about VB.Net? VB.Net 1.1 Coding -------------------- Private Sub InitializeComponent()...
11
by: Timofmars | last post by:
I'm try to Unload DB2 data from a table into a record sequential file on NT. I can an unload on Mainframe, but it doesn't seem to be an option in NT. In NT, all I can do is export/import. I can...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.