c# 调用C/C++ DLL,传入返回结构体以及指针数组(指针指向自定义的结构体)

时间:2025-03-15 07:51:36

1 定义结构体

1-1 C++代码:

定义结构体

struct DetectInfo {
	int x; //!< x coordinate of the top-left corner
	int y; //!< y coordinate of the top-left corner
	int width; //!< width of the rectangle
	int height; //!< height of the rectangle;
	float conf;
	int class_id;
};

1-2 C# 代码:

 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
 public struct DetectInfo
 {
     // [MarshalAs()]
     public int x; //!< x coordinate of the top-left corner
     public int y;  //!< y coordinate of the top-left corner
     public int width; //!< width of the rectangle
     public int height; //!< height of the rectangle;
     public float conf; // confidence score 
     public int class_id; // class id
 }

2 返回结构体

2-1 C++代码:

头文件声明

extern "C" APPEARANCEDETECTALG_EXPORTS struct DetectInfo simpleStruct();

函数实现

ALG_EXPORTS struct DetectInfo simpleStruct() {
	DetectInfo dInfo;
	dInfo.x = 1;
	dInfo.y = 2;
	dInfo.width = 3;
	dInfo.height = 4;
	dInfo.conf = 0.56;
	dInfo.class_id = 7;

	return dInfo;
}

2-2 C# 代码:

[DllImport("", CallingConvention = CallingConvention.Cdecl)]
extern static DetectInfo simpleStruct();

 // 返回 结构体
DetectInfo ddInfo = simpleStruct();
Console.WriteLine("{0}, {1}, {2}, {3}, {4}, {5}", ddInfo.x, ddInfo.y, ddInfo.width, ddInfo.height, ddInfo.conf, ddInfo.class_id);

Console.ReadLine();

3 传入返回指针数组

3-1 C++代码:

头文件声明

extern "C" ALG_EXPORTS int simpleStructList(int num, DetectInfo* dInfoList);

函数实现

ALG_EXPORTS int simpleStructList(int num, DetectInfo* dInfoList) {
	DetectInfo dInfo;
	for (int i = 0; i < 10; ++i)
	{
		dInfo.x = i;
		dInfo.y = i;
		dInfo.width = i;
		dInfo.height = i;
		dInfo.conf = 0.56 / i;
		dInfo.class_id = i;
		*(dInfoList + i) = dInfo;
	}
	return 0;
}

3-2 C# 代码:

[DllImport("", CallingConvention = CallingConvention.Cdecl)]
extern static int simpleStructList(int num, [In, Out] DetectInfo[] dInfoList);


// 传入和返回 结构体
const int nCount = 100;
DetectInfo[] dInfoList = new DetectInfo[nCount];

int rst = simpleStructList(nCount, dInfoList);

for (int i = 0; i < rst; i++)
{
    Console.WriteLine("{0}, {1}, {2}, {3}, {4}, {5}", dInfoList[i].x, dInfoList[i].y, dInfoList[i].width, dInfoList[i].height, 
        dInfoList[i].conf, dInfoList[i].class_id);
}
Console.ReadLine();