7,763
社区成员
发帖
与我相关
我的任务
分享
Option Explicit
'调用 标准 DLL 中函数,并返回值给调用者
Private Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) 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 FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Sub ZeroMemory Lib "kernel32" Alias "RtlZeroMemory" (ByVal dest As Any, ByVal numbytes As Long)
'功能:动态调用 标准 DLL 中函数,并取得返回值
'参数:
' strPluginPathFileName: DLL 文件路径
' strFunctionName: DLL 中被调用函数名称
' strArg1~strArg5: DLL 中被调用函数的参数(此例子全为字符串类型)
'返回:字符串 (返回 标准 DLL 中函数的返回值)
Public Function GetPluginInfo(ByVal strPluginPathFileName As String, ByVal strFunctionName As String, Optional ByRef strArg1 As String = "", Optional ByRef strArg2 As String = "", Optional ByRef strArg3 As String = "", Optional ByRef strArg4 As String = "", Optional ByRef strArg5 As String = "") As String
Dim hModule As Long '模块句柄
Dim pFunction As Long '函数地址
Dim pContent As Long '插件返回内容指针
Dim tmpContent As String '临时存放取得的内容
' On Error Resume Next
hModule = LoadLibrary(strPluginPathFileName)
pFunction = GetProcAddress(hModule, strFunctionName)
Dim tmpinfo As New FunctionPtr
Dim tmpFunction As Object
If strArg1 = "" And strArg2 = "" And strArg3 = "" And strArg4 = "" And strArg5 = "" Then
Set tmpFunction = tmpinfo.Create(pFunction, vbString)
CopyMemory tmpContent, StrPtr(tmpFunction.Invoke()), 4
Else
Set tmpFunction = tmpinfo.Create(pFunction, vbString, vbString, vbString, vbString, vbString, vbString)
CopyMemory tmpContent, StrPtr(tmpFunction.Invoke(strArg1, strArg2, strArg3, strArg4, strArg5)), 4
Debug.Print tmpContent
End If
FreeLibrary hModule
GetPluginInfo = tmpContent
Set tmpinfo = Nothing
Set tmpFunction = Nothing
End Function