windows 在 Visual Basic 6 中访问动态加载的 DLL(使用 LoadLibrary)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1667397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing dynamically loaded DLL (with LoadLibrary) in Visual Basic 6
提问by simon
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-leaking 3rd party DLL (no source code) accessed by Tomcat application?) . I'm doing it in Visual Basic 6, and the loading and unloading with the following example works:
我需要为 DLL 创建一个包装器,根据需要加载和卸载它(对于那些对这个问题的背景感兴趣的人,请参阅 如何解决 Tomcat 应用程序访问的内存泄漏的第 3 方 DLL(无源代码)?)。我在 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
I can see using Process Explorerthat the DLL is loaded to memory when the messagebox is displayed, and is discarded afterwards. However, calling the method is naturally not enough - I need to access the methods within the dynamically loaded DLL.
我可以使用Process Explorer看到,当显示消息框时,DLL 被加载到内存中,然后被丢弃。但是,调用方法自然是不够的——我需要访问动态加载的 DLL 中的方法。
How can I achieve this? I would like to call the method getVersion in class mainClass, which is in TestDLL, like this:
我怎样才能做到这一点?我想在 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
However, the line
然而,该行
versionString = "- From DLL: " + mainClass.getVersion
throws an error "Object required".
抛出错误“需要对象”。
采纳答案by AngryHacker
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 getVersionthat is exported.
首先,由于您通过 LoadLibrary 调用它,因此这里没有类 - 只有函数被导出以供公共使用。所以你的 mainClass 引用永远不会工作。假设您有一个导出的函数getVersion。
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
回答by MarkJ
Do you need to call COM methods on this DLL? If so, I'm not at all sure this is possible.
你需要在这个 DLL 上调用 COM 方法吗?如果是这样,我完全不确定这是可能的。
Matthew Curland's excellent Advanced Visual Basic 6is 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.
不过,Matthew Curland 出色的Advanced Visual Basic 6是我第一个看的地方。那里有一些强大的底层 COM 东西,可以绕过普通的 VB6 技术。
还有DirectCom,它允许您在不使用 COM 的情况下调用 COM 方法。我自己从未使用过它,但人们在 VB6 新闻组上谈论它。