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

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 有卷积神经网络识别害虫的项目吗
  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件