如何使用Dllimport将对象数组从c#传递给c ++?

时间:2016-12-22 18:43:23

标签: c# dllimport

我正在研究需要通过引用将对象数组从c#传递给c ++的项目,当我通过引用传递一个对象时,它成功返回新值但是当我传递一个数组时它返回旧值,那么什么我的代码有问题吗?

C ++代码

extern "C" __declspec(dllexport) int solve(Cell*(&xx)[5][5]) {
xx[0][0]->side = 55;
return xx[0][0]->side;
}

C#代码

internal static class UnsafeMethods
{
[DllImport(@"E:\Cs\4th Year\HPC\Parallel_calculations\Debug\Parallel_calculations.dll", EntryPoint = "solve", CallingConvention = CallingConvention.Cdecl)]
public static extern int solve([MarshalAs(UnmanagedType.SafeArray)] ref Cell[,] x);
}

 Cell[,] arr = new Cell[5, 5];
        for (int i = 0; i < arr.GetLength(0); i++)
        {
            for (int y = 0; y < arr.GetLength(1); y++)
            {
                arr[i, y] = new Cell(0, 0, 0, false, false, false, 50);
            }
        }
        arr[0, 0].side = 4;
        int x = UnsafeMethods.solve(ref arr);
        Console.WriteLine(x + " " + arr[0, 0].side);

x是新值,但是arr [0,0] .side返回旧值

0 个答案:

没有答案