shendu74110
2020-09-07 20:50
采纳率: 0%
浏览 179

尝试读取或写入受保护的内存。这通常指示其他内存已损坏?

C#调用C++的dll
报错:
应用程序处于中断模式, 尝试读取或写入受保护的内存。这通常指示其他内存已损坏;

C++原型:

   void   GetTmpData(int type,BYTE * pbuff,float *maxtmp,int *maxx,int *maxy,float *mintmp,int *minx,int *miny,float *centertmp,float *tmparr,float *alltmp);

C#导入:

   [DllImport(@"XthermDll.dll" ,  CharSet = CharSet.Ansi),]
        public static extern void GetTmpData(int type, byte[] pbuff, ref float maxtmp, ref int  maxx,ref int maxy,ref float mintmp,ref int  minx,ref int  miny,ref float centertmp,ref float tmparr,ref float alltmp);

调用时:

    byte[] b = new byte[Buffer.Length];
                    float maxtmp = 0f;
                    int maxx = 0;
                    int maxy = 0;
                    float mintmp = 0f;
                    int minx = 0;
                    int miny = 0;
                    float centertmp = 0f;
                    float tmparr = 0f;
                    float alltmp = 0f;
                    GetTmpData(0, b, ref maxtmp, ref maxx, ref maxy, ref mintmp, ref minx, ref miny, ref centertmp, ref tmparr, ref alltmp);

各位大佬帮忙看看, 啥问题?感谢

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • threenewbee 2020-09-07 22:33
    C#的byte[]和C++的BYTE *是不同的,
    要用
    IntPtr b = Marshal.AllocHGlobal(Buffer.Length);
    
    [DllImport(@"XthermDll.dll" ,  CharSet = CharSet.Ansi),]
    public static extern void GetTmpData(int type, IntPtr pbuff, ref float maxtmp, ref int  maxx,ref int maxy,ref float mintmp,ref int  minx,ref int  miny,ref float centertmp,ref float tmparr,ref float alltmp);
    byte[] byte1 = new byte[Buffer.Length];
    System.Runtime.InteropServices.Marshal.Copy(b, byte1, 0, size);
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题