编写了一段程序,C++里这么写的float ITU_raoshe_DLL(int* grid, int row, int column, float X1, float Y1, float X2, float Y2, float f, float ht, float hr);
C#调用这么写的[DllImport("ITU_raoshe_DLL_1.dll", CallingConvention = CallingConvention.Cdecl)]
        public  static extern float ITU_raoshe_DLL(Intptr grid, int row, int column, float X1, float Y1, float X2, float Y2, float f, float ht, float hr);
调用函数:int[,] data = new int[Column, Row];
         IntPtr pointer = Marshal.UnsafeAddrOfPinnedArrayElement(data, 0);
        result[i, j] = CPPDLL.ITU_raoshe_DLL(pointer, Row, Column, X[j], Y[j], X[i], Y[i], f1[j], H[j], H[i]);
但是运行时报错:尝试读取或写入受保护的内存这通常指示其他内存已损坏。
是什么问题啊,哪位高手能帮忙解释下,谢啦

解决方案 »

  1.   

    int* 用 ref int 试试
      

  2.   

    我试过了,但是还是出一样的错误,如果那样改的话是不是得这样
    [DllImport("ITU_raoshe_DLL_1.dll", CallingConvention = CallingConvention.Cdecl)]
       public static extern float ITU_raoshe_DLL(ref int[,] grid, int row, int column, float X1, float Y1, float X2, float Y2, float f, float ht, float hr);
    result[i, j] = CPPDLL.ITU_raoshe_DLL(ref data, Row, Column, X[j], Y[j], X[i], Y[i], f1[j], H[j], H[i]);
    否则就显示错误类型不对应,但是这样改还是不对
      

  3.   

    遇到过类似情况,这样处理的:
    使用C++/CLR进行封装;
    指针使用pin_ptr<T>进行处理;然后将该Assambly引入到C#的项目中,可能比较方便点
    对C#中提供的属性看得少
      

  4.   

    指针的问题搜索一下pin_ptr<T>怎么使用就可以了;
    C++/CLR需要看一下相关的书籍。
      

  5.   

    array<System::Byte>^ data;
    array<System::Byte>^ value;
    ......
    pin_ptr<System::Byte> src = &data[0];             
    pin_ptr<System::Byte> dst = &value[0]; 
    ....
    memcpy((void*)dst, (void*)src, data->Length); 
      

  6.   


    去掉ref试试,data本来就是int*了,原来我给C++传过字符串数组,直接传即可
      

  7.   

    谢谢各位的回答,最后用unsafe了,指针传递没有问题,但是结果还是不对,貌似是DLL内部问题,还在调试中,谢谢大家了!!!