gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 09:18:35 29 4
gpt4 key购买 nike

我需要为 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 中的方法。

我怎样才能做到这一点?我想在 TestDLL 中的类 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 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

关于windows - 在 Visual Basic 6 中访问动态加载的 DLL(使用 LoadLibrary),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1667397/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com