How to unload a COM DLL?
(Thursday, September 02, 2004)
Found the following interesting discussion in the Newsgroups:
How can I unload a COM DLL? by:spacejay@bluewin.ch (spacejay)
| I'm programming an VB.NET application wich is using a third-party COM-DLL-module. The Module is only loaded if needed. And now my problem: My Application doesn't end correct and hang up itself. After I have closed my application, i can see in the task manager a CPU Load about over 95%. I could verify that this only happens, if the COM Module was used by my application. If the Module wasn't used then my application ends correct. With an extendend Taskmanager I could unload manual single DLL's which an application is using. And if I unload the COM Module DLL manual, so my application ends correct.
Now my question: Is it possible (and how) to unload a DLL, which my application has loaded dircet by vb code?
I'm very thankfully for every of your answers.
Greets
Jan
--------Example---------
'The Definition of the COM-Module Private m_FaktNT As FaktNT.OLESrv
....
'If I need the COM-Module, I make a new Instance of the COM-Module m_FaktNT = New FaktNT.OLESrv()
....
'Now I can use the Interfaces of the Module m_FaktNT.AdrdGetName() m_FaktNT.AdrdGetField()
....
'At the end of my application I release the COM-Object from my application While Marshal.ReleaseComObject(m_FaktNT) >= 0 End While 'And set set the Reference to Nothing m_FaktNT = Nothing
| | | Reply: by:Mike McIntyre [MVP]
| | | I simply put the following code in a common class somewhere, import in the namespace, and call "Release([ComObjectReferenceName])"
----------------- Imports System.Runtime.InteropServices Imports System.Runtime.InteropServices.Marshal .. .. .. Public Shared Function Release(ByRef ComObject As Object) As Integer Return Release(ComObject, False) End Function
Public Shared Function Release(ByRef ComObject As Object, _ ByVal collect As Boolean) As Integer Dim Result As Integer
Try Result = ReleaseComObject(ComObject) ComObject = Nothing
If (collect) Then GC.Collect() GC.WaitForPendingFinalizers() End If
Catch End Try
Return Result End Function
Check out the System.Runtime.InteropServices.Marshal namespace and specifically the ReleaseComMethod method.
-- Mike McIntyre Visual Basic MVP www.getdotnetcode.com
|

|
0 Comments:
Post a Comment
<< Home