c#调用c++的DLL方法,提示“出现尝试读取或写入受保护的内存。这通常指示其他内存已损坏”

幻l听 2017-11-07 03:53:56
是个IC读卡器读卡号的c++的demo,下面粘贴代码:
void CCTabOpration::OnRead() 
{
CString strSer = pReader->GetCardStrSerial();
OutputDebugString(strSer);
//pMem->SetWindowText("\n"+strSer);
}

CString CAceReader::GetCardStrSerial()
{
CString strre;
BYTE buf[10];
BYTE snr[16];
GET_SNR(hComm, DeviceAddress, 0x26, 0x00, snr,buf);
strre.Format("%02x%02x%02x%02x",buf[0],buf[1],buf[2],buf[3]);
return strre;
}

然后粘贴我写道c#端的代码,先写了个类,然后点击按钮调用该方法:
    class Class1
{
[DllImport("mi.dll", EntryPoint = "GET_SNR", SetLastError = true,
CharSet = CharSet.Auto, ExactSpelling = false,
CallingConvention = CallingConvention.StdCall)]
public static extern int GET_SNR(IntPtr commHandle,
int DeviceAddress,
byte mode,
byte RDM_halt,
byte[] snr,
byte[] value);

}
按钮方法:
private void button1_Click(object sender, EventArgs e)
{
IntPtr hComm = IntPtr.Zero;

byte[] a = new byte[128];
byte[] b = new byte[16];
byte aa = 0x26;
byte bb = 0x00;
Class1.GET_SNR(hComm, 0, aa, bb, b, a); //GET_SNR方法的前两个参数的值是在c++中打断点知道的前俩参数都是0
Console.WriteLine(a[1]);

}

然后就报错了,就是调方法的那一行,错误日志:
未处理System.AccessViolationException
_HResult=-2147467261
_message=尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
HResult=-2147467261
IsTransient=false
Message=尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
Source=WindowsFormsApplication14
StackTrace:
在 WindowsFormsApplication14.Class1.GET_SNR(IntPtr commHandle, Int32 DeviceAddress, Byte mode, Byte RDM_halt, Byte[] snr, Byte[] value)
在 WindowsFormsApplication14.Form1.button1_Click(Object sender, EventArgs e) 位置 c:\123\WindowsFormsApplication14\WindowsFormsApplication14\Form1.cs:行号 28
在 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)
在 WindowsFormsApplication14.Program.Main() 位置 c:\123\WindowsFormsApplication14\WindowsFormsApplication14\Program.cs:行号 19
在 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.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
InnerException:
...全文
1248 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
拜一刀 2017-11-07
  • 打赏
  • 举报
回复
不记得是不是一样的错误了,反正以前有拿c++的dll过来,结果数据类型搞错了,c++和.net个别数据类型名字一样,实际长度不一样
xian_wwq 2017-11-07
  • 打赏
  • 举报
回复
AccessViolationException错误大多都是因为分配的空间不够, 有越界操作引起的 确认下c++ dll是unicode还是多字节的 c#调用的时候必须对应起来 CharSet = CharSet.Auto可能会导致异常

110,567

社区成员

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

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

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