问题描述
调用时尝试读取或写入受保护的内存。这通常指示其他内存已损坏。不知道是什么情况,网上好多都是c++Long对应c#int,但是我已经转了还是没用,这代码一开始时可以用的,我重装了win7系统后就运行不了了。。。。求大神帮忙c++:extern"C"__declspec(dllexport)BOOL__stdcallTCR_OpenReader(longnPort){if(1==g_iProtocol){g_CardReader.CreateInterface("1.dll");}else{g_CardReader.CreateInterface("2.dll");}g_iCardType=0;memset(&g_ICCardInfo.T,0x0,sizeof(ICCARDTOLLINFO));memset(&g_ETCCardInfo,0x0,sizeof(ETCCARDINFO));returng_CardReader.OpenComm(nPort,9600);}
c#:[DllImport(@"D:TollSoftWEBTICCardReader.dll",EntryPoint="TCR_OpenReader",SetLastError=true,CallingConvention=CallingConvention.Cdecl)]publicexternstaticboolTCR_OpenReader(intlgPort);
未处理System.AccessViolationExceptionMessage=尝试读取或写入受保护的内存。这通常指示其他内存已损坏。Source=ZTP.ReadCardOCXStackTrace:在ZTP.ReadCardOCX.ReadCardCtl.TCR_OpenReader(Int32lgPort)在WindowsFormsApplication1.Form1.button1_Click(Objectsender,EventArgse)位置D:WorkSpacetollsoftWEBZTP.ServerZTPReadCardOCXWindowsFormsApplication1Form1.cs:行号25在System.Windows.Forms.Control.OnClick(EventArgse)在System.Windows.Forms.Button.OnClick(EventArgse)在System.Windows.Forms.Button.OnMouseUp(MouseEventArgsmevent)在System.Windows.Forms.Control.WmMouseUp(Message&m,MouseButtonsbutton,Int32clicks)在System.Windows.Forms.Control.WndProc(Message&m)在System.Windows.Forms.ButtonBase.WndProc(Message&m)在System.Windows.Forms.Button.WndProc(Message&m)在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtrhWnd,Int32msg,IntPtrwparam,IntPtrlparam)在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&msg)在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtrdwComponentID,Int32reason,Int32pvLoopData)在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32reason,ApplicationContextcontext)在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32reason,ApplicationContextcontext)在System.Windows.Forms.Application.Run(FormmainForm)在WindowsFormsApplication1.Program.Main()位置D:WorkSpacetollsoftWEBZTP.ServerZTPReadCardOCXWindowsFormsApplication1Program.cs:行号18在System.AppDomain._nExecuteAssembly(RuntimeAssemblyassembly,String[]args)在System.AppDomain.ExecuteAssembly(StringassemblyFile,EvidenceassemblySecurity,String[]args)在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()在System.Threading.ThreadHelper.ThreadStart_Context(Objectstate)在System.Threading.ExecutionContext.Run(ExecutionContextexecutionContext,ContextCallbackcallback,Objectstate,BooleanignoreSyncCtx)在System.Threading.ExecutionContext.Run(ExecutionContextexecutionContext,ContextCallbackcallback,Objectstate)在System.Threading.ThreadHelper.ThreadStart()InnerException:
解决方案
解决方案二:
extern"C"__declspec(dllexport)BOOL__stdcallTCR_OpenReader(longnPort)与[DllImport(@"D:TollSoftWEBTICCardReader.dll",CallingConvention=CallingConvention.Cdecl)]不一致
解决方案三:
引用1楼Forty2的回复:
extern"C"__declspec(dllexport)BOOL__stdcallTCR_OpenReader(longnPort)与[DllImport(@"D:TollSoftWEBTICCardReader.dll",CallingConvention=CallingConvention.Cdecl)]不一致
那是不是改成CallingConvention.StdCall,还是没有用呢
解决方案四:
顶一下,有没有大神来看看我的问题啊,
解决方案五:
1.dll.2.dll文件存在嘛?
解决方案六:
先排查下地址是否有效,异常提示很明显,这种情况在C还是C#中多是内存地址无效造成写入读取时候不想正常寻址,出现的内存访问违规
解决方案七:
引用4楼zanfeng的回复:
1.dll.2.dll文件存在嘛?
2.dll跟那个dll在一个文件夹下