Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to free / unload DLL?

Status
Not open for further replies.

hstijnen

Programmer
Nov 13, 2002
172
NL
Hi,

I'm using a DLL in my VBA code for a Word macro in a template. When a function in the DLL is called, the DLL is loaded in memory. At that moment the DLL is "in use" and cannot be deleted or replaced. So maintenance is not possible when people are using the template.

How can I free or unload the DLL directly after calling a function from the DLL?

Thanks in advance

Henk Stijnen
 
This is an example from API-Guide 3.7, author and URL address in the code.

[tt]Create a new project and add this code to Form1
Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Any, ByVal wParam As Any, ByVal lParam As Any) As Long
Private Sub Form_Load()
On Error Resume Next
'KPD-Team 1999
'URL: 'E-Mail: KPDTeam@Allapi.net
'We're going to call an API-function, without declaring it!
Dim lb As Long, pa As Long
'map 'user32' into the address space of the calling process.
lb = LoadLibrary("user32")
'retrieve the address of 'SetWindowTextA'
pa = GetProcAddress(lb, "SetWindowTextA")
'Call the SetWindowTextA-function
CallWindowProc pa, Me.hWnd, "Hello !", ByVal 0&, ByVal 0&
'unmap the library's address
FreeLibrary lb
End Sub[/tt]

combo
 
That's not nearly as useful as it appears.

Suffice it to say that LoadLibrary and UnloadLibrary merely increment and decrement the reference count for the library (OK, not quite true; if the library isn't already loaded when LoadLibrary is called then it is loaded during LoadLibrary; if the reference count reaches 0 during UnloadLibrary, then the library is freed for unloading).

The following may be a better example (but is not production code):
[tt]
Option Explicit
Private Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long
Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long

Private Declare Function WNetGetUser Lib "mpr.dll" Alias "WNetGetUserA" (ByVal lpName As String, ByVal lpUserName As String, lpnLength As Long) As Long




Private Sub Command1_Click()
Dim hLib As Long

' These are for the example library call
Dim lpName As String
Dim lpUserName As String
Dim lpnLength As Long

WNetGetUser lpName, lpUserName, lpnLength

hLib = GetModuleHandle("mpr")
ForceUnloadable hLib
Beep
End Sub

Private Sub ForceUnloadable(ByVal hModule As Long)
Dim FreeResult As Long
FreeResult = 1
Do Until FreeResult = 0 ' OK, library reference count is 0, and library is therefore unloadable
FreeResult = FreeLibrary(hModule)
Loop
End Sub
 
Thank you so far, strongm.

It works, the lib is indeed freed.
But there is now a strange effect. I'v implemented the code in the startup macro of a Word template. Now, when I use the template a second time, when calling a function from the library, I get an automation error message: Links of the called object with the client are lost!

Do you have an idea what can be the matter?
 
Oh, I'm not suggesting that unloading a library that is actually in use by an application is a good idea...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top