Call DLL Function from handle
Hi guys! Help please. How can i call the function(from a DLL) and pass parameters for that function if I only have the handle to that function? Please see my code below. Thanks in advance!
Code:
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
Dim loadngResult As Long, lngProcAddress As Long, maxnumpath As Long
Dim sRet As String
Const ERROR_SUCCESS = &H0
maxnumpath = 255
sRet = String$(MAX_PATH, 0)
loadngResult = LoadLibrary("kernel32")
If loadngResult <> 0 Then
MsgBox "Load Succeessful!"
lngProcAddress = GetProcAddress(loadngResult, "GetSystemDirectoryA")
If lngProcAddress Then
MsgBox "Function found!"
'CALL FUNCTION HERE -- HOW?
Else
MsgBox "Function not found!"
End If
Else
MsgBox "Load Unsuccessful"
End If
FreeLibrary loadngResult
Re: Call DLL Function from handle
You can't! VB doesn't support function pointers. The only function that could be used is CallWindowProc which takes a function pointer as the first parameter and calls that function. However it requires a that the function you want to call will accept exactly 4 arguments (which of course GetSystemDirectory doesn't).
Re: Call DLL Function from handle
SO, it means that i cant call the GetSystemDirectory or any function that accept 5 or more arguments? Is there any solution about this? Actually, what I want here is to use the LoadLibrary function becuase as I know, LoadLibrary function searches the DLL for you so you dont need to worry where it is located, whether it is in the Sytem32, windows, etc. folder. But, unfortunately, it only returns an handle to the dll and not the file path to the dll. Can you guys suggest of anyway to solve my problem? Thanks in advance!
Re: Call DLL Function from handle
Are you trying to use LoadLibrary simply because you don't know the path to it? You haven't specified the path to the Kernel32.dll either, but still you're able to call functions that reside in it....
Re: Call DLL Function from handle
Yah...Isn't just becuase it is in the System32 folder? what if i have mydll.dll that is not in the system32 folder and i only know that mydll.dll does exist in my harddrive? will it still work?
Re: Call DLL Function from handle
It's true that LoadLibrary searches the current directory, the application directory, the system directory, the windows directory, and the directories specified by the Path environment variable while this is not done by the Declare statement.
But may I ask why you want to use it this way. All system APIs are located in the System32 folder. If you have created your own DLL you could make sure that it always accepts 4 parameters, if you want more indata let one of the parameters be a structure (user defined type).
Re: Call DLL Function from handle
Ok. Maybe i'll just tell you what im up to. Im developing a windows application that uses the dll/library created by my colleagues and since when we deploy this application to our clients it will modify our client's machine settings/configuration by registering the dll that comes with our application, which unfurtunately, my boss doesn't want to happen. So he suggest to just use LoadLibrary to dynamically load the library Instead of registering to the system. Now my problem is, some of the functions from the dll accept more than 4 arguments which unfurtunately can't be called by using CallWindowProc, as you've mentioned. Do you have any suggestion on how to solve this? Thanks in advance!
Re: Call DLL Function from handle
http://www.planetsourcecode.com/vb/s...49776&lngWId=1
Done by Paul Caton, respected coder. It works with both stdCall & cDECL function pointers. If your dlls accept ParamArrays, then you may be out of luck; but otherwise, this should do the trick.