windows – 在Visual Basic 6中访问动态加载的DLL(使用LoadLibrary)

前端之家收集整理的这篇文章主要介绍了windows – 在Visual Basic 6中访问动态加载的DLL(使用LoadLibrary)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要为DLL创建一个包装器,根据需要加载和卸载它(对于那些对此问题的背景感兴趣的人,请参阅 How to work around memory-leaking 3rd party DLL (no source code) accessed by Tomcat application?).我在Visual Basic 6中执行此操作,并使用以下示例进行加载和卸载:
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

我可以看到使用Process Explorer显示消息框时将DLL加载到内存中,然后丢弃.但是,调用方法自然是不够的 – 我需要访问动态加载的DLL中的方法.

我怎样才能做到这一点?我想在类mainClass中调用方法getVersion,它在TestDLL中,如下所示:

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

但是,行

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

抛出错误“需要对象”.

首先,由于您通过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
原文链接:https://www.f2er.com/windows/372166.html

猜你在找的Windows相关文章