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

后端 未结 2 1669
渐次进展
渐次进展 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
    
    0 讨论(0)
  • 2021-01-01 04:52

    Do you need to call COM methods on this DLL? If so, I'm not at all sure this is possible.

    • Matthew Curland's excellent Advanced Visual Basic 6 is the first place I'd look, though. There's some powerful under-the-hood COM stuff in there that circumvents the normal VB6 techniques.

    • There's also DirectCom, which allows you to call COM methods without using COM. Never used it myself, but people chat about it on the VB6 newsgroup.

    0 讨论(0)
提交回复
热议问题