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

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

问题描述

我需要为DLL创建一个包装器,根据需要加载和卸载(对于那些对这个问题的背景感兴趣的人,请参阅 / a> )。我在Visual Basic 6中执行,并且使用以下示例进行加载和卸载:

 私有声明函数FreeLibrary Lib kernel32(ByVal hLibModule As Long)As Long 
私有声明函数LoadLibrary Libkernel32别名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库地址:+ lb

FreeLibrary lb

End Sub

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



我该如何实现?我想在类mainClass中调用getVersion方法,它在TestDLL中,如下所示:

 私有声明函数FreeLibrary Lib kernel32(ByVal hLibModule As Long)As Long 
私有声明函数LoadLibrary Libkernel32别名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 = - 从DLL:+ mainClass.getVersion
MsgBox versionString

FreeLibrary lb

结束Sub

但是,

  versionString = - 从DLL:+ mainClass.getVersion 

throws一个错误Object required。

解决方案

首先,既然你是通过LoadLibrary调用它,这里没有类 - 只有函数才被导出以供公众使用。所以你的mainClass引用将永远不会工作。我们假设你有一个导出功能 getVersion



我会尝试以下:


$ b $私人声明函数FreeLibrary Libkernel32(ByVal hLibModule As Long)As Long
私有声明函数LoadLibrary Libkernel32别名LoadLibraryA(ByVal lpLibFileName As String )As Long
私有声明函数GetProcAddress Libkernel32(ByVal hModule As Long,ByVal lpProcName As String)As Long
私有声明函数CallWindowProc Libuser32别名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)

'检索getVersion'的地址
pa = GetProcAddress(lb,getVersion)

'调用getVersion函数'
retValue = CallWindowProc(pa,Me.hWnd,我想要我的版本,ByVal 0& ByVal 0&)

'发布图书馆'
FreeLibrary lb
End Sub


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:\projects\other\VB_DLLs\TestDLL\TestDLL.dll")    

    Msgbox "Library address: " + lb

    FreeLibrary lb    

End Sub

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.

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:\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".

解决方案

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.

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

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

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