我是编程新手,我还在学习,所以我可能不会对以下所有问题使用正确的术语。
我为第三方应用程序编写了一个自定义代码,将其编译为 dll,并将应用程序配置为加载正在工作的 dll,我已经确认了它。这种定制是用 C++ 编写的。我确保在自定义项中导出了一个函数,并限制它使用 DLL 导出查看器可见。
现在,我在 vb.net 中有一个不同的应用程序,我正在尝试专门连接到 C++ 自定义 DLL 以访问导出的函数。
这是我正在尝试做的代码示例。
Class oClass
<DllImport("kernel32.dll", ExactSpelling:=True, PreserveSig:=False, SetLastError:=True, CharSet:=CharSet.Ansi)>
Private Shared Function LoadLibraryA(dllToLoad As String) As IntPtr
End Function
<DllImport("kernel32.dll", ExactSpelling:=True, PreserveSig:=False)>
Private Shared Function GetProcAddress(hModdule As IntPtr, procedureName As String) As IntPtr
End Function
<DllImport("kernel32.dll", ExactSpelling:=True, PreserveSig:=False)>
Private Shared Function FreeLibrary(hModdule As IntPtr) As Boolean
End Function
Private Delegate Sub FunctionDelegate(ByVal input As String)
Private Function vbCallingFunction()
Dim pDll As IntPtr = LoadLibraryA("FilePathToTheDll")
Dim pAddressOfFunctionToCall As IntPtr = GetProcAddress(pDll, "FunctionName")
Dim Testcall As FunctionDelegate = Marshal.GetDelegateForFunctionPointer(Of FunctionDelegate)(pAddressOfFunctionToCall)
Testcall("It worked")
End Function
所以这不起作用,当它到达 GetDelegateForFunctionPointer 时,它会抛出 System.ArgumentNullException:'Value cannot be null。参数名称:ptr'。所以我回去检查了 pDll int 指针值,它似乎等于 IntPtr.Zero。我验证了第三方应用程序正在运行并且 dll 已加载。