Call dll in variable location

G

Guest

Hy group

I've a declaration of a function where I point to a .dll file. But I'd like
to connect to the .dll file using a variable. See the example for more
information.

Now, I'm using:
Declare Function LDBUser_GetUsers Lib "C:\CountUsersConnectedToDatabase.DLL" _
(lpszUserBuffer() As String, ByVal lpszFilename As String, _
ByVal nOptions As Long) As Integer

But I'de like to put the "C:\CountUsersConnectedToDatabase.DLL" in a string
because it will probably change of location. I allready tried the following
but it didn't work.

Declare Function LDBUser_GetUsers Lib strPathDLL_
(lpszUserBuffer() As String, ByVal lpszFilename As String, _
ByVal nOptions As Long) As Integer

Does anybody has an idea?

Thanxs
 
A

Alex Dybenko

you can use following approach:
Private Declare Function GetModuleHandle Lib "kernel32" Alias
"GetModuleHandleA" (ByVal lpModuleName As String) As Long
Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA"
(ByVal lpLibFileName As String) As Long
Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As
Long) As Long


a proc:

strDllPath="C:\CountUsersConnectedToDatabase.DLL"
hModule = GetModuleHandle(strDllPath)
If hModule = 0 Then
' need to load module into this process.
hModule = LoadLibrary(strDllPath & "irun.dll")
FreeLib = True
End If

call LDBUser_GetUsers (...)

If FreeLib Then Call FreeLibrary(hModule)
 
G

Guest

Alex

Thanxs for you're help but I didn't find the solution yet.

Now, I've several lines off declaring:

Declare Function LDBUser_GetUsers Lib "C:\CountUsersConnectedToDatabase.DLL" _
(lpszUserBuffer() As String, ByVal lpszFilename As String, _
ByVal nOptions As Long) As Integer
Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA"
(ByVal lpModuleName As String) As Long
Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal
lpLibFileName As String) As Long
Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long

and the function:

Public Function GetUsers(Optional StrDbPath As String)

ReDim lpszUserBuffer(1) As String
Dim Cusers As Long
Dim strMsgBox, strDllPath As String
Dim hModule As Long
Dim FreeLib As Boolean


strDllPath = Left(CurrentDb.Name, Len(CurrentDb.Name) - 3) & "dll"
hModule = GetModuleHandle(strDllPath)
If hModule = 0 Then
' need to load module into this process.
hModule = LoadLibrary(strDllPath & "irun.dll")
FreeLib = True
End If
If FreeLib = True Then Call FreeLibrary(hModule)

Cusers = LDBUser_GetUsers(lpszUserBuffer(), StrDbPath, 2)
MsgBox Cusers

End Function

I don't understand, why are you adding "irun.dll" to LoadLibrary (if I try
to leave it, it gives an error).
The code seems to run fine, but actually it doesn't do a thing..., it just
generates an error by Cusers = LDBUser_GetUsers(lpszUserBuffer(), StrDbPath,
2)

Thanxs

btw, strDllPath = Left(CurrentDb.Name, Len(CurrentDb.Name) - 3) & "dll"
gives the path and correct name of the .dll file. It has the same name & path
as the DB but instead of .mdb it is called .dll
 
A

Alex Dybenko

oops, sorry
hModule = LoadLibrary(strDllPath & "irun.dll")
should be
hModule = LoadLibrary(strDllPath)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top