在 Visual Basic 6 中访问动态加载的 DLL(使用 LoadLibrary) [英] Accessing dynamically loaded DLL (with LoadLibrary) in Visual Basic 6

查看:14
本文介绍了在 Visual Basic 6 中访问动态加载的 DLL(使用 LoadLibrary)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为 DLL 创建一个包装器,根据需要加载和卸载它(对于那些对这个问题的背景感兴趣的人,请参阅 如何解决 Tomcat 应用程序访问的内存泄漏 3rd 方 DLL(无源代码)?) .我在 Visual Basic 6 中执行此操作,并且使用以下示例进行加载和卸载:

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:

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:projectsotherVB_DLLsTestDLLTestDLL.dll")    

    Msgbox "Library address: " + lb

    FreeLibrary lb    

End Sub

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

I can see using Process Explorer that 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.

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

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

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:projectsotherVB_DLLsTestDLLTestDLL.dll")    

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

    FreeLibrary lb    

End Sub

但是,行

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

抛出错误需要对象".

推荐答案

首先,由于您是通过 LoadLibrary 调用它,因此这里没有类 - 仅导出函数供公众使用.所以你的 mainClass 参考永远不会起作用.假设您有一个导出的函数 getVersion.

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.

我会尝试以下方法:

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:projectsotherVB_DLLsTestDLLTestDLL.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

这篇关于在 Visual Basic 6 中访问动态加载的 DLL(使用 LoadLibrary)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆