I need to delete a DLL file when I am done using it (cleanup after code is completed).
I try using "LoadLibrary" and "FreeLibrary" in Excel VBA but no matter what I do Excel.exe clings to the DLL file.
Public Declare PtrSafe Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
Public Declare PtrSafe Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
Private Sub Load_Unload_DLL()
Dim lb As Long, pa As Long
lb = LoadLibrary("C:\Users\Administrator\Documents\MathLibrary.dll")
'MsgBox "Library address: " & lb
Dim FreeResult As Long
FreeResult = 1
Do Until FreeResult = 0
FreeResult = FreeLibrary(lb)
Loop
Name "C:\Users\Administrator\Documents\MathLibrary.dll" As "C:\Users\Administrator\Documents\MathLibrary2.dll"
Kill ("C:\Users\Administrator\Documents\MathLibrary2.dll")
End Sub
Despite "FreeResult" equaling "0" I receive the following error when executing the "Kill" command:
And Process Explorer showing that the DLL file is indeed still loaded by Excel:
The file can be renamed, but not deleted (as indicated by the code).
Am I missing something?