幻l听 2017-11-07 07:37 采纳率: 0%
浏览 3600

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

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

  • 写回答

1条回答 默认 最新

  • 天飘 2018-01-22 05:53
    关注

    你调用的类存放位置不对.或者是不是C#封装的类

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题