CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Guest

    Dynamically Loading Dlls at RunTime

    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



  2. #2
    Join Date
    May 1999
    Posts
    3,332

    Re: Dynamically Loading Dlls at RunTime

    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.


  3. #3
    Guest

    Re: Dynamically Loading Dlls at RunTime

    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



  4. #4
    Join Date
    May 1999
    Posts
    3,332

    Re: Dynamically Loading Dlls at RunTime

    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.


  5. #5
    Join Date
    Apr 1999
    Posts
    24

    Re: Dynamically Loading Dlls at RunTime

    According to MSDN: http://msdn.microsoft.com/library/bo...ingmodule.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.


  6. #6
    Join Date
    May 1999
    Posts
    3,332

    Re: Dynamically Loading Dlls at RunTime

    ...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.


  7. #7
    Join Date
    Apr 1999
    Posts
    24

    Re: Dynamically Loading Dlls at RunTime

    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.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured