编写了一段程序,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]);
但是运行时报错:尝试读取或写入受保护的内存这通常指示其他内存已损坏。
是什么问题啊,哪位高手能帮忙解释下,谢啦
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]);
但是运行时报错:尝试读取或写入受保护的内存这通常指示其他内存已损坏。
是什么问题啊,哪位高手能帮忙解释下,谢啦
[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]);
否则就显示错误类型不对应,但是这样改还是不对
使用C++/CLR进行封装;
指针使用pin_ptr<T>进行处理;然后将该Assambly引入到C#的项目中,可能比较方便点
对C#中提供的属性看得少
C++/CLR需要看一下相关的书籍。
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);
去掉ref试试,data本来就是int*了,原来我给C++传过字符串数组,直接传即可