?
Solved

Deleting an unmanaged DLL on Me.FormClosing

Posted on 2010-01-10
5
Medium Priority
?
321 Views
Last Modified: 2012-05-08
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
Comment
Question by:Craz3d
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 26282309
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 26283413
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
 
LVL 1

Author Comment

by:Craz3d
ID: 26329796
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
 
LVL 1

Author Comment

by:Craz3d
ID: 26329802
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
 
LVL 1

Accepted Solution

by:
Craz3d earned 0 total points
ID: 26335087
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question