问题描述
“System.AccessViolationException”类型的未经处理的异常出现在mscorlib.dll中。其他信息:尝试读取或写入受保护的内存。这通常指示其他内存已损坏。代码如下//////C++dll代码HWNDhWnd;extern"C"__declspec(dllexport)voidcOSG(HWNDmhWnd){hWnd=mhWnd;}extern"C"__declspec(dllexport)voidInitOSG(){。。。。}////////C#代码[DllImport("viewdll.dll")]publicstaticexternvoidInitOSG();[DllImport("viewdll.dll",CallingConvention=CallingConvention.Cdecl)]publicstaticexternvoidcOSG(IntPtrmhWnd);//////调用privatevoidForm1_Load(objectsender,EventArgse){Class1.cOSG(this.panel1.Handle);}privatevoidbutton1_Click(objectsender,EventArgse){ThreadaThread=newThread(newThreadStart(Class1.InitOSG));aThread.Start();}运行时出现在System.AccessViolationException中第一次偶然出现的“mscorlib.dll”类型的异常“System.AccessViolationException”类型的未经处理的异常出现在mscorlib.dll中。其他信息:尝试读取或写入受保护的内存。这通常指示其他内存已损坏。有什么问题呢??????
解决方案
解决方案二:
没有大神指教么
解决方案三:
//c++函数强制声明[DllImport("mi.dll",EntryPoint="API_OpenComm")]publicstaticexternIntPtrAPI_OpenComm(intnCom,intnBaudrate);[DllImport("mi.dll",EntryPoint="API_CloseComm")]publicstaticexternboolAPI_CloseComm(IntPtrcommHandle);[DllImport("mi.dll",EntryPoint="API_ControlBuzzer")]publicstaticexternintAPI_ControlBuzzer(IntPtrcommHandle,intDeviceAddress,bytefreq,byteduration,refbytebuffer);[DllImport("mi.dll",EntryPoint="API_MF_Read")]publicstaticexternintAPI_MF_Read(IntPtrcommHandle,intDeviceAddress,bytemode,byteblk_add,bytenum_blk,refbytesnr,refbytebuffer);privatevoidForm1_Load(objectsender,EventArgse){Program.Port=API_OpenComm(4,9600);if(Program.Port.ToInt32()==0){labelControl2.Visible=false;labelControl3.Visible=true;buttonX1.Visible=true;return;}bytepBuf=newbyte();Program.G_start=API_ControlBuzzer(Program.Port,0,3,1,refpBuf);if(Program.G_start==0){labelControl2.Visible=true;labelControl3.Visible=false;buttonX1.Visible=false;}//启动时间轴timer1.Interval=1000;timer1.Start();}
解决方案四:
引用2楼zyl_leilei的回复:
//c++函数强制声明[DllImport("mi.dll",EntryPoint="API_OpenComm")]publicstaticexternIntPtrAPI_OpenComm(intnCom,intnBaudrate);[DllImport("mi.dll",EntryPoint="API_CloseComm")]publicstaticexternboolAPI_CloseComm(IntPtrcommHandle);[DllImport("mi.dll",EntryPoint="API_ControlBuzzer")]publicstaticexternintAPI_ControlBuzzer(IntPtrcommHandle,intDeviceAddress,bytefreq,byteduration,refbytebuffer);[DllImport("mi.dll",EntryPoint="API_MF_Read")]publicstaticexternintAPI_MF_Read(IntPtrcommHandle,intDeviceAddress,bytemode,byteblk_add,bytenum_blk,refbytesnr,refbytebuffer);privatevoidForm1_Load(objectsender,EventArgse){Program.Port=API_OpenComm(4,9600);if(Program.Port.ToInt32()==0){labelControl2.Visible=false;labelControl3.Visible=true;buttonX1.Visible=true;return;}bytepBuf=newbyte();Program.G_start=API_ControlBuzzer(Program.Port,0,3,1,refpBuf);if(Program.G_start==0){labelControl2.Visible=true;labelControl3.Visible=false;buttonX1.Visible=false;}//启动时间轴timer1.Interval=1000;timer1.Start();}
我的错在哪了呢该如何修改
解决方案五:
引用3楼u011347730的回复:
Quote: 引用2楼zyl_leilei的回复:
//c++函数强制声明[DllImport("mi.dll",EntryPoint="API_OpenComm")]publicstaticexternIntPtrAPI_OpenComm(intnCom,intnBaudrate);[DllImport("mi.dll",EntryPoint="API_CloseComm")]publicstaticexternboolAPI_CloseComm(IntPtrcommHandle);[DllImport("mi.dll",EntryPoint="API_ControlBuzzer")]publicstaticexternintAPI_ControlBuzzer(IntPtrcommHandle,intDeviceAddress,bytefreq,byteduration,refbytebuffer);[DllImport("mi.dll",EntryPoint="API_MF_Read")]publicstaticexternintAPI_MF_Read(IntPtrcommHandle,intDeviceAddress,bytemode,byteblk_add,bytenum_blk,refbytesnr,refbytebuffer);privatevoidForm1_Load(objectsender,EventArgse){Program.Port=API_OpenComm(4,9600);if(Program.Port.ToInt32()==0){labelControl2.Visible=false;labelControl3.Visible=true;buttonX1.Visible=true;return;}bytepBuf=newbyte();Program.G_start=API_ControlBuzzer(Program.Port,0,3,1,refpBuf);if(Program.G_start==0){labelControl2.Visible=true;labelControl3.Visible=false;buttonX1.Visible=false;}//启动时间轴timer1.Interval=1000;timer1.Start();}我的错在哪了呢该如何修改
为什么要Class1.InitOSG?你上面声明的c++函数跟下面的Load和button1_Click不是在一个类里面吗?不行你调试一下看看是哪句代码出了问题。。。