C++ 加载C+时System.AccessViolationException异常+;在vb.net中的应用
我正在开发一个.net应用程序。我找到了一个用CPP编写的API调用示例,在将其转换为VB.net时遇到了问题C++ 加载C+时System.AccessViolationException异常+;在vb.net中的应用,c++,vb.net,C++,Vb.net,我正在开发一个.net应用程序。我找到了一个用CPP编写的API调用示例,在将其转换为VB.net时遇到了问题 C++示例 void InvokeClientMethord() { HINSTANCE adapter_dll_handle = loadLibraryA("dllName.dll"); if(adapter_dll_handle != Null) { const char* functionName = "Comma
C++示例
void InvokeClientMethord()
{
HINSTANCE adapter_dll_handle = loadLibraryA("dllName.dll");
if(adapter_dll_handle != Null)
{
const char* functionName = "CommandName";
typedef xanydictionary_ptr (WINAPIV *FARPROC)(xrstring, xanydictionary_ptr);
FARPROC ExecuteWgmCustomCmd = (FARPROC)GetProcAddress((HMODULE)adapter_dll_handle,functionName);
if (ExecuteWgmCustomCmd != NUL)
{
xrstring cust_id = "ThisIsSomeName";
xanydictionary_ptr pInputParms, pResponseParms;
pInputParms = xanydictionary::create ();
pInputParms -> set (xstring ("testMessage"), "This is test message.");
pResponseParms = ExecuteWgmCustomCmd(cust_id, pInputParms);
if (pResponseParms.isnull ())
return ;
}
}
}
在上述示例中,XYIN DISTICALY-PTR、XRSTRORY是C++ LIB
中的类型定义 typedef const char*xrstring; typedef xrchandle xanydictionary(ptr 在上述类型定义中,xrchandle是特定于API的类,而xanydictionary也是特定于API的类。它是xrchandle类中指定的字典 我的VB翻译 Public Class SomeClass
<DllImport("dllName.dll", ExactSpelling:=True, PreserveSig:=False, CharSet:=CharSet.Unicode, EntryPoint:="executeCustomCommand")>
Public Shared Function executeCustomCommand(cust_id As IntPtr, params As IntPtr) As IntPtr
End Function
Private Structure xanydictionary_ptr
Public VarName As IntPtr
Public varValue As IntPtr
End Structure
Public sub InvokeClientMethord()
Dim cust_id As String = "ThisIsSomeName"
Dim cust_id_ptr As IntPtr = Marshal.StringToHGlobalUni(cust_id)
Dim pInputParms As New xanydictionary_ptr With {
.VarName = Marshal.StringToHGlobalUni("testMessage"),
.varValue = Marshal.StringToHGlobalUni("This is test message."))
}
Dim pInputParms_ptr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(pInputParms))
Marshal.StructureToPtr(inputStrct, pInputParms_ptr, True)
Dim pResponseParms As IntPtr = executeCustomCommand(CustPrt, StructPtr)
End Sub
End Class
公共类SomeClass
公共共享函数executeCustomCommand(客户id为IntPtr,参数为IntPtr)为IntPtr
端函数
私有结构xanydictionary\u ptr
公共变量名为IntPtr
公共变量值为IntPtr
端部结构
公共子invokeClientMethod()
Dim cust_id为String=“ThisIsSomeName”
Dim cust_id_ptr As IntPtr=Marshal.StringToHGlobalUni(cust_id)
Dim pInputParms作为新的xanydictionary\u ptr{
.VarName=Marshal.StringToHGlobalUni(“testMessage”),
.varValue=Marshal.StringToHGlobalUni(“这是测试消息”)
}
Dim pInputParms_ptr As IntPtr=Marshal.AllocHGlobal(Marshal.SizeOf(pInputParms))
Marshal.StructureToPtr(inputStrct,pInputParms\u ptr,True)
Dim pResponseParms为IntPtr=executeCustomCommand(CustPrt,StructPtr)
端接头
末级
调用我的子例程时,它抛出System.AccessViolationException“试图读取或写入受保护内存”。这通常表示另一个内存已损坏。该错误发生在哪一行?@nanhydron在“Dim presponseParams As IntPtr=executeCustomCommand(CustPrt,StructPtr)”处请提供一个,什么是
xrstring
,xanydictionary\u ptr
,<代码> XYNYNGOR::创建< /COD> >?@ ALANBIRTLE,这些是C++ API中的Type。typedef xrchandle xanydictionary(ptr;typedef const char*xrstringe编辑您的问题以包括这些详细信息,下一个问题是什么是xrchandle
和xanydictionary