• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 322
  • Last Modified:

Deleting an unmanaged DLL on Me.FormClosing

Below is a shippet of how I'm loading the DLL and importing its functions.. I'm trying to figure out how to remove the DLL file from the HDD when my application exits. (I'm creating it myself on execution)

If the above isn't possible, does anyone know how to load it from an EmbeddedResource?
#Region "BASS_Load"

    Public Enum BASSInit
        ' Fields
        BASS_DEVICE_8BITS = 1
        BASS_DEVICE_DEFAULT = 0
        BASS_DEVICE_MONO = 2
        BASS_DEVICE_NOSYNC = &H10
    End Enum
    Public Enum BASSMusic
        ' Fields
        BASS_DEFAULT = 0
        BASS_MUSIC_CALCLE = &H2000
        BASS_MUSIC_FT2MOD = &H10
        BASS_MUSIC_LOOP = 4
        BASS_MUSIC_NONINTER = &H4000
        BASS_MUSIC_NOSAMPLE = &H100000
        BASS_MUSIC_POSRESET = &H100
        BASS_MUSIC_PT1MOD = &H20
        BASS_MUSIC_RAMP = 1
        BASS_MUSIC_RAMPS = 2
        BASS_MUSIC_STOPBACK = &H800
        BASS_MUSIC_SURROUND = &H200
        BASS_MUSIC_SURROUND2 = &H400
        BASS_UNICODE = -2147483648
    End Enum


    <DllImport("bassmod.dll", CharSet:=CharSet.Auto)> _
Public Shared Function BASSMOD_SetVolume(ByVal volume As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function
    <DllImport("bassmod.dll", CharSet:=CharSet.Auto)> _
    Public Shared Function BASSMOD_GetVolume() As Integer
    End Function
    <System.Runtime.InteropServices.DllImport("bassmod.dll", CharSet:=CharSet.Auto)> _
    Public Shared Function BASSMOD_Free() As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function
    <DllImport("bassmod.dll", CharSet:=CharSet.Auto)> _
    Public Shared Function BASSMOD_Init(ByVal device As Integer, ByVal freq As Integer, ByVal flags As BASSInit) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function
    <DllImport("bassmod.dll", EntryPoint:="BASSMOD_MusicLoad", CharSet:=CharSet.Auto)> _
    Private Shared Function BASSMOD_MusicLoadUnicode(<MarshalAs(UnmanagedType.Bool)> ByVal mem As Boolean, <[In](), MarshalAs(UnmanagedType.LPWStr)> ByVal file As String, ByVal offset As Integer, ByVal length As Integer, ByVal flags As Integer) As Integer
    End Function
    Public Shared Function BASSMOD_MusicLoad(ByVal file As String, ByVal offset As Integer, ByVal length As Integer, ByVal flags As BASSMusic) As Integer
        flags = (flags Or (BASSMusic.BASS_DEFAULT Or BASSMusic.BASS_UNICODE))
        Return BASSMOD_MusicLoadUnicode(False, file, offset, length, CInt(flags))
    End Function
    <DllImport("bassmod.dll", CharSet:=CharSet.Auto)> _
    Public Shared Function BASSMOD_MusicPlay() As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function
    <DllImport("bassmod.dll", CharSet:=CharSet.Auto)> _
    Public Shared Function BASSMOD_MusicStop() As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function
    <DllImport("bassmod.dll", EntryPoint:="BASSMOD_MusicLoad", CharSet:=CharSet.Auto)> _
    Private Shared Function BASSMOD_MusicLoadMemory(<MarshalAs(UnmanagedType.Bool)> ByVal mem As Boolean, <[In](), MarshalAs(UnmanagedType.LPArray)> ByVal memory As Byte(), ByVal offset As Integer, ByVal length As Integer, ByVal flags As Integer) As Integer
    End Function

#End Region

Open in new window

0
Craz3d
Asked:
Craz3d
  • 3
1 Solution
 
Arthur_WoodCommented:
Your problem is that, having loaded the DLL, the process then holds an acitve connection to the DLL, and any attempt to delete the DLL while a process is deemed to be accessing it, is bound to fail.  This is just like trying to delete or update a DLL while the progrma/ process that uses that DLL is still loaded in memory.
 
AW
0
 
Bob LearnedCommented:
About the only way that I can think of deleting the DLL is to create another AppDomain, and then load/unload the assembly there.  This brings with it, though, a different set of challenges...
0
 
Craz3dAuthor Commented:
Yeah, I'm aware that it has to be unloaded.. That's actually what the question was about; How to unload it from memory so it can be deleted, sorry that part wasn't clear above.
0
 
Craz3dAuthor Commented:
The "BASSMOD_Free" function has been imported, but it doesn't seem to be doing anything, as it still won't allow me to delete the file when my application's exiting.
<System.Runtime.InteropServices.DllImport("bassmod.dll", CharSet:=CharSet.Auto)> _
    Public Shared Function BASSMOD_Free() As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function

Open in new window

0
 
Craz3dAuthor Commented:
Figured it out myself.  Loading the library with LoadLibrary(string_containing_dll-file)

Unload / delete functions in snippet below.
<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")

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now