Accessing a dynamically loaded DLL (using LoadLibrary) in Visual Basic 6

I need to create a wrapper for the DLL, load and unload it as needed (for those who are interested in the background of this question, see How to work with memory leak of a third-party DLL (without source code), accessible by Tomcat application? ) I do this in Visual Basic 6, and loading and unloading in the following example:

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 Sub cmdTestLoadingDLL_Click()

    Dim lb As Long, pa As Long    
    lb = LoadLibrary("D:\projects\other\VB_DLLs\TestDLL\TestDLL.dll")    

    Msgbox "Library address: " + lb

    FreeLibrary lb    

End Sub

I see, using Process Explorer , that a DLL is loaded into memory when a message box is displayed, and then discarded. However, calling the method is naturally not enough - I need to access the methods in a dynamically loaded DLL.

How can i achieve this? I would like to call the getVersion method in the mainClass class, which is in TestDLL, for example:

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 Sub cmdTestLoadingDLL_Click()

    Dim lb As Long, pa As Long
    Dim versionString As String

    lb = LoadLibrary("D:\projects\other\VB_DLLs\TestDLL\TestDLL.dll")    

    versionString = "- From DLL: " + mainClass.getVersion
    MsgBox versionString

    FreeLibrary lb    

End Sub

However line

versionString = "- From DLL: " + mainClass.getVersion

" ".

+3
2

, LoadLibrary, - . mainClass . , getVersion, .

:

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
+3

COM- DLL? , , .

+2

Source: https://habr.com/ru/post/1721783/


All Articles