Home  |  Index  |  Dotnet4all Snippets  |  Submit resources
About  |  Mail us  
Dotnet4all Logo

  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








 
Previous Posts
    - How to convert Richtextbox to Textbox in .NET
    - How to detect if application is already running in...
    - GetForeGroundWindow
    - Reflection: Determining a Method's Attributes from...
    - Open a form from menu
    - Adding a reference in VS.NET
    - Converting String Value to a Formula for calculati...
    - Microsoft Office Document Image Writer & Vb.Net
    - AddRange method for CheckedListBox
    - Any way to extract certain bytes from the remote f...

Archives
    - 08/01/2004 - 08/08/2004
    - 08/08/2004 - 08/15/2004
    - 08/15/2004 - 08/22/2004
    - 08/22/2004 - 08/29/2004
    - 08/29/2004 - 09/05/2004
    - 09/05/2004 - 09/12/2004
    - 09/12/2004 - 09/19/2004
    - 09/19/2004 - 09/26/2004
    - 09/26/2004 - 10/03/2004
    - 10/03/2004 - 10/10/2004
    - 01/02/2005 - 01/09/2005
    - 01/09/2005 - 01/16/2005
    - 01/30/2005 - 02/06/2005
    - 01/01/2006 - 01/08/2006


Disclaimer & Terms of Use  | DotNet4All.Com concept & © 2004 - 2007 by  Zelders²  - Holland