c#调用c++ dll 文件 报错:尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
求高人指点,谢谢!c++
HANDLE CreatCaptureThread (void)
{
DWORD nID = 0;
HANDLE hThread = CreateThread (NULL,0,ThreadProc,NULL,0,&nID);
if (hThread == NULL)
printf ("创建线程失败\n");
return hThread;
}
/*
用这个函数,获取数据包的指针
*/
void* GetPacketPtr()
{
void* packet;
WaitForSingleObject(SignalMutex,INFINITE);
if(EndPtr == FrontPtr)
packet=NULL;
else {
packet=Classbuffer[EndPtr];
EndPtr++;
}
ReleaseMutex(SignalMutex);
return packet;
}
UINT16 GetChannel (void* ptr){
return 0x00000000;
}
c#
[DllImport("ParselDll.dll", EntryPoint = "CreatCaptureThread")]
public extern static IntPtr CreatCaptureThread();
[DllImport("ParselDll.dll", EntryPoint = "GetPacketPtr")]
public extern static IntPtr GetPacketPtr();
[DllImport("ParselDll.dll", CharSet = CharSet.Ansi, EntryPoint = "GetMacFrameCtrl")]
public extern static UInt16 GetMacFrameCtrl(IntPtr ptr);
IntPtr ptr = DLL.CreatCaptureThread();
if (ptr == null)
{
MessageBox.Show("线程启动失败");
}
else
{
IntPtr ptrStr = DLL.GetPacketPtr();
UInt16 i = DLL.GetMacFrameCtrl(ptrStr);
MessageBox.Show(i.ToString());
}
c++文件不是我写的,这样调用运行报错:尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
dll动态库放在启动项目的bin\Debug下