Accessing dynamically loaded DLL (with LoadLibrary) in Visual Basic 6

后端 未结 2 1684
渐次进展
渐次进展 2021-01-01 04:13

I have a need to create a wrapper for a DLL, loading and unloading it as needed (for those interested in the background of this question, see How to work around memory-leaki

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-01 04:34

    First of all, since you are calling it via LoadLibrary, there are no classes here - only functions are exported for public consumption. So your mainClass reference would never work. Let's assume you have a function getVersion that is exported.

    I would try the following:

    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 Foo
      On Error Resume Next
    
      Dim lb As Long, pa As Long
      Dim versionString As String
      Dim retValue as Long
    
      lb = LoadLibrary("D:\projects\other\VB_DLLs\TestDLL\TestDLL.dll")  
    
      'retrieve the address of getVersion'
      pa = GetProcAddress(lb, "getVersion")
    
      'Call the getVersion function'
      retValue = CallWindowProc (pa, Me.hWnd, "I want my version", ByVal 0&, ByVal 0&)
    
      'release the library'
      FreeLibrary lb
    End Sub
    

提交回复
热议问题