日期:2014-05-18  浏览次数:20746 次

C#调用C++dll含有指针的问题
编写了一段程序,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]);
但是运行时报错:尝试读取或写入受保护的内存这通常指示其他内存已损坏。
是什么问题啊,哪位高手能帮忙解释下,谢啦

------解决方案--------------------
int* 用 ref int 试试
------解决方案--------------------
遇到过类似情况,这样处理的:
使用C++/CLR进行封装;
指针使用pin_ptr<T>进行处理;

然后将该Assambly引入到C#的项目中,可能比较方便点
对C#中提供的属性看得少
------解决方案--------------------
探讨
引用:

遇到过类似情况,这样处理的:
使用C++/CLR进行封装;
指针使用pin_ptr<T>进行处理;

然后将该Assambly引入到C#的项目中,可能比较方便点
对C#中提供的属性看得少

可以说的详细点吗?我是新手。。。谢啦

------解决方案--------------------
探讨

我试过了,但是还是出一样的错误,如果那样改的话是不是得这样
[DllImport("ITU_raoshe_DLL_1.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern float ITU_raoshe_DLL(ref int[,] grid, int row, int column, flo……