C# 调用C/C++ DLL,传入返回结构体以及指针数组
1.定义结构体
C++代码
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
3
4
5
6
7
8
2
3
4
5
6
7
8
C# 代码
C#
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct DetectInfo
{
// [MarshalAs(UnmanagedType.LPStr)]
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
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
2.返回结构体
C++代码
头文件声明
C++
extern "C" APPEARANCEDETECTALG_EXPORTS struct DetectInfo simpleStruct();
1
函数实现
C++
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;
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
C# 代码
C#
[DllImport("APPEARANCEDETECTALGDLL.dll", 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();
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
3.传入返回指针数组
C++代码
头文件声明
C++
extern "C" ALG_EXPORTS int simpleStructList(int num, DetectInfo* dInfoList);
1
函数实现
C++
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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
C# 代码
C#
[DllImport("ALGDLL.dll", 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();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16