c#调c++dll,尝试读取或写入受保护的内存。这通常指示其他内存已损坏。

嘿嘿hah 2014-12-11 09:40:05
调用时 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。不知道是什么情况,网上好多都是c++Long对应c#int,但是我已经转了还是没用,这代码一开始时可以用的,我重装了win7系统后就运行不了了。。。。求大神帮忙
c++:

extern "C" __declspec(dllexport) BOOL __stdcall TCR_OpenReader(long nPort)
{
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));

return g_CardReader.OpenComm(nPort, 9600);
}


c#:

[DllImport(@"D:\TollSoft\WEB\TICCardReader.dll", EntryPoint = "TCR_OpenReader", SetLastError = true,CallingConvention=CallingConvention.Cdecl)]
public extern static bool TCR_OpenReader(int lgPort);


未处理 System.AccessViolationException
Message=尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
Source=ZTP.ReadCardOCX
StackTrace:
在 ZTP.ReadCardOCX.ReadCardCtl.TCR_OpenReader(Int32 lgPort)
在 WindowsFormsApplication1.Form1.button1_Click(Object sender, EventArgs e) 位置 D:\WorkSpace\tollsoft\WEB\ZTP.Server\ZTPReadCardOCX\WindowsFormsApplication1\Form1.cs:行号 25
在 System.Windows.Forms.Control.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 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(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.Run(Form mainForm)
在 WindowsFormsApplication1.Program.Main() 位置 D:\WorkSpace\tollsoft\WEB\ZTP.Server\ZTPReadCardOCX\WindowsFormsApplication1\Program.cs:行号 18
在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
InnerException:

...全文
268 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
嘿嘿hah 2014-12-11
  • 打赏
  • 举报
回复
引用 4 楼 zanfeng 的回复:
1.dll.2.dll文件存在嘛?
2.dll跟那个dll在一个文件夹下
1987andy 2014-12-11
  • 打赏
  • 举报
回复
先排查下地址是否有效,异常提示很明显,这种情况在C还是C#中多是内存地址无效造成写入读取时候不想正常寻址,出现的内存访问违规
足球中国 2014-12-11
  • 打赏
  • 举报
回复
1.dll.2.dll文件存在嘛?
嘿嘿hah 2014-12-11
  • 打赏
  • 举报
回复
顶一下,有没有大神来看看我的问题啊,
嘿嘿hah 2014-12-11
  • 打赏
  • 举报
回复
引用 1 楼 Forty2 的回复:
extern "C" __declspec(dllexport) BOOL __stdcall TCR_OpenReader(long nPort) 与 [DllImport(@"D:\TollSoft\WEB\TICCardReader.dll", CallingConvention=CallingConvention.Cdecl)] 不一致
那是不是改成CallingConvention.StdCall,还是没有用呢
Forty2 2014-12-11
  • 打赏
  • 举报
回复
extern "C" __declspec(dllexport) BOOL __stdcall TCR_OpenReader(long nPort) 与 [DllImport(@"D:\TollSoft\WEB\TICCardReader.dll", CallingConvention=CallingConvention.Cdecl)] 不一致

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧