是个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: