Bootstrap

C#与C++之间dll的二维(多维)数组传递


思维:用结构体作为中间变量,也就是传递结构体,然后在C#中把结构体中的数据放进二维数组中。


C#接收代码的形式:(这里只是思路展示,非可运行代码)

  [DllImport("myDLL.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern void myFunc([In][MarshalAs(UnmanagedType.LPArray, SizeConst = 0)] InStruct[] istruct, [Out] [MarshalAs(UnmanagedType.LPArray, SizeConst = 0)] OutStruct[] outstruct);

        public struct InStruct
        {
            public int x;
            public int y;
            public int z;
        }

        public struct OutStruct
        {
            public int x;
            public int y;
            public int z;
        }

        //把C++通过结构体接收的数据,装在二维数组中
        int[,] data = new int[3,100];
        InStruct[] instruct = new InStruct[100];
        OutStruct[] outstruct = new OutStruct[100];

        //在二维数组data中接收outstruct中的数据

C++中封装的形式:

struct InStruct
{
	int x;
	int y;
	int z;
};
struct OutStruct
{
	int x;
	int y;
	int z;
};
extern "C" __declspec(dllexport) void myFunc(InStruct *instruct, OutStruct *outstruct)
{
	InStruct instruct[100];
	OutStruct outstruct[100];
	
	//使用结构体对象传进来或者传出去数据,可以把二维数组的数据先赋给结构体
}

;