Click to See Complete Forum and Search --> : Dynamically Loading Dlls at RunTime


April 8th, 1999, 10:23 AM
Is there any way that a I can load a given DLL and declare a function at run time? The user would select a DLL, then type in the name of a function to call within that DLL.

For example, could something like this be declared at runtime:

NewDll = "advapi32.dll"
NewFunction = "RegCreateKey"

Declare Function NewFunction Lib NewDll (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long

1) Is it possible?
2) If so, are there any samples to get me started?

Thanks,
AC

Lothar Haensler
April 8th, 1999, 10:33 AM
in 16 Bit VB you can load a 32-Bit DLL dynamically calling LoadLibraryEx32W.
You can then call a function from that DLL using the CallProc32W API function.
I don't know if something similar exists in 32-Bit VB.

April 8th, 1999, 04:11 PM
Thanks, with the information you gave me I'm almost on my way. I can load the DLL and get the handle to it, I can get the handle to the specific function. But my question is, how do I initiate the function and pass parameters to it? Here is a sniplet of my code.

Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal
lpLibFileName As String) As Long
Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal
lpProcName As String) As Long
Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As
Long

Function CallFunction(mFileName as String, mFunctionName as String, mParameters as String) as Variant
Dim hLibrary As Long, hProc As Long

'Get handle to the DLL
hLibrary = LoadLibrary(mFileName)
'Get handle to the Function within the DLL
hProc = GetProcAddress(hLibrary, mFucntionName)

'What code goes here in order to execute the Function???

'Free DLL
hLibrary = FreeLibrary(hLibrary)
End Function

Lothar Haensler
April 9th, 1999, 02:25 AM
As I mentioned earlier, you'd have to call CallProc32W to actually "call" your DLL function,
but: This does not work in 32-Bit-VB, AFAIK. CallProc32W ist for 16-Bit programs!
I don't know how and if you can achieve the same thing in 32-Bit VB.

Dan O'Brien
April 9th, 1999, 07:17 AM
According to MSDN: http://msdn.microsoft.com/library/books/techlang/hcvb/html/loadingmodule.htm, "Visual Basic can’t call function pointers retrieved with the GetProcAddress API function...".

That's because GetProcAddress returns a pointer, not a handle, and VB cannot use pointers.

Lothar Haensler
April 9th, 1999, 07:55 AM
...which is not exactly true, because VB does not have to "handle" pointers, but simply pass it to API functions, as it is done in subclassing and other APIs.
With CallProc32W Visual Basic (16 Bit only) can quite well indirectly call 32-Bit API functions.

Dan O'Brien
April 9th, 1999, 08:15 AM
Maybe I wasn't specific enough. As far as I know, in 32-bit Windows (no developer I know is writing 16-bit Windows code any more), VB cannot call a C function through a function pointer returned from GetProcAddress. Maybe you'll have to write your own CallProc function in C to simulate the 16-bit CallProc32W.