22 May '13 - 08:49 *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register  
Pages: [1]
  Reply  |  Print  
Author Topic: [VB.NET] Forcefully unload and remove dll on application exit  (Read 4857 times)
Mashuga
Posts: 9


« on: 16 Jan '10 - 07:45 »
Reply with quoteQuote

    <System.Runtime.InteropServices.DllImport("bassmod.dll", CharSet:=CharSet.Auto)> _
    Public Shared Function BASSMOD_Free() As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function

I'm importing and calling the function, but even after Free'ing the library, I still can't manage to delete it.

Any ideas?
Logged
radio42
Posts: 4012


« Reply #1 on: 16 Jan '10 - 08:13 »
Reply with quoteQuote

.Net typically does NOT unload any dynamically loaded assembly until you exit your app. So it probably is still referenced and loaded by your app.
Logged
Mashuga
Posts: 9


« Reply #2 on: 16 Jan '10 - 11:42 »
Reply with quoteQuote

.Net typically does NOT unload any dynamically loaded assembly until you exit your app.

Any way to force it to unload the the dll and delete any references?
Logged
radio42
Posts: 4012


« Reply #3 on: 16 Jan '10 - 17:43 »
Reply with quoteQuote

You might try to use the "LoadLibrary" and "FreeLibrary" methods located in "kernel32.dll" and load/unload any dll manually.
Logged
Mashuga
Posts: 9


« Reply #4 on: 17 Jan '10 - 01:40 »
Reply with quoteQuote

    <DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
    Public Shared Function LoadLibrary(<[In](), MarshalAs(UnmanagedType.LPStr)> ByVal lpFileName As String) As IntPtr
    End Function
    <DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
    Public Shared Function GetModuleHandle(ByVal lpModuleName As String) As IntPtr
    End Function
    <DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
    Public Shared Function FreeLibrary(<[In]()> ByVal hModule As IntPtr) As Boolean
    End Function

'==============================

        BASSMOD_MusicStop()
        BASSMOD_Free() 'don't know if this is still needed since I'm doing it myself
        Dim hAddress As Long = GetModuleHandle("bassmod.dll")
        While hAddress >= 1
            FreeLibrary(hAddress)
            hAddress = GetModuleHandle("bassmod.dll")
        End While
        File.Delete("bassmod.dll")

Have to loop it for some reason, as just one call doesn't seem to take care of it.

Minimal overhead, but it works. Smiley
Logged
Pages: [1]
  Reply  |  Print  
 
Jump to:  

Powered by SMF 1.1.18 | SMF © 2013, Simple Machines