C#向C++封送結構體數組
在使用第三方的非托管API時,我們經常會遇到參數為指針或指針的指針這種情況,一般我們會用IntPtr指向我們需要傳遞的參數地址;但是當遇到這種一個導出函數時,我們如何正確的使用IntPtr呢,
extern "C" __declspec(dllexport) int GetClass(Class pClass[50]) ;
由于這種情況也經常可能遇到,所以我制作了2個示例程序來演示下如何處理這種非托管函數的調用!首先創建一個C++ 的DLL 設置一個如上的導出函數
1 #include <Windows.h>
2 #include <stdio.h>
3
4 typedef struct Student
5 {
6 char name[20];
7 int age;
8 double scores[32];
9 }Student;
10
11 typedef struct Class
12 {
13 int number;
14 Student students[126];
15 }Class;
16
17 extern "C" __declspec(dllexport) int GetClass(Class pClass[50])
18 {
19 for(int i=0;i<50;i++)
20 {
21 pClass[i].number=i;
22 for(int j=0;j<126;j++)
23 {
24 memset(pClass[i].students[j].name,0,20);
25 sprintf(pClass[i].students[j].name,"name_%d_%d",i,j);
26 pClass[i].students[j].age=j%2==0?15:20;
27 }
28 }
29 return 0;
30 }
2 #include <stdio.h>
3
4 typedef struct Student
5 {
6 char name[20];
7 int age;
8 double scores[32];
9 }Student;
10
11 typedef struct Class
12 {
13 int number;
14 Student students[126];
15 }Class;
16
17 extern "C" __declspec(dllexport) int GetClass(Class pClass[50])
18 {
19 for(int i=0;i<50;i++)
20 {
21 pClass[i].number=i;
22 for(int j=0;j<126;j++)
23 {
24 memset(pClass[i].students[j].name,0,20);
25 sprintf(pClass[i].students[j].name,"name_%d_%d",i,j);
26 pClass[i].students[j].age=j%2==0?15:20;
27 }
28 }
29 return 0;
30 }
上面DLL 的導出函數要求傳遞的參數為它自定義的Class結構體數組, 那么我們在C#調用它時也要自定義對應的結構體了,我們可以定義為如下:
1 [StructLayout(LayoutKind.Sequential)]
2 struct Student
3 {
4 [MarshalAs(UnmanagedType.ByValTStr,SizeConst=20)]
5 public string name;
6 public int age;
7 [MarshalAs(UnmanagedType.ByValArray,SizeConst=32)]
8 public double[] scores;
9 }
10 [StructLayout(LayoutKind.Sequential)]
11 struct Class
12 {
13 public int number;
14 [MarshalAs(UnmanagedType.ByValArray,SizeConst=126)]
15 public Student[] students;
16
17 }
2 struct Student
3 {
4 [MarshalAs(UnmanagedType.ByValTStr,SizeConst=20)]
5 public string name;
6 public int age;
7 [MarshalAs(UnmanagedType.ByValArray,SizeConst=32)]
8 public double[] scores;
9 }
10 [StructLayout(LayoutKind.Sequential)]
11 struct Class
12 {
13 public int number;
14 [MarshalAs(UnmanagedType.ByValArray,SizeConst=126)]
15 public Student[] students;
16
17 }
需要注意的是,這2個結構體中的數組大小一定要跟C++中的限定一樣大小哦,接下來如何使用這個API來正確的獲取數據呢,大多數人可能想到像這樣的處理方式
Class myclass = new Class();
IntPtr ptr=Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Class)));
GetClass(ptr);
Marshal.FreeHGlobal(ptr);
IntPtr ptr=Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Class)));
GetClass(ptr);
Marshal.FreeHGlobal(ptr);
沒錯,這樣的處理是沒問題的,但是我們的API的參數是Class數組,這種處理方式只是傳遞一個Class結構體參數,所以這種方式在這里就不太合適了,! 那大家就想到先Class[] myclass = new Class[MaxClass]; 然后在用Marshal.AllocHGlobal 來獲取myclass 數據的指針,其實這樣也是錯的, 因為 Class結構中包含了,不能直接封送的Student結構,所以無論如何上面的想法是錯誤的!
那要怎么辦呢,其實很簡單,就是先分配一段非托管內存,并調用API后,再將非托管內容數據讀取到托管結構體數據中!
示例演示代碼如下:
1 static void Main(string[] args)
2 {
3 int size = Marshal.SizeOf(typeof(Class)) * 50;
4 byte[] bytes = new byte[size];
5 IntPtr pBuff = Marshal.AllocHGlobal(size);
6 Class[] pClass = new Class[50];
7 GetClass(pBuff);
8 for (int i = 0; i < 50; i++)
9 {
10 IntPtr pPonitor = new IntPtr(pBuff.ToInt64() + Marshal.SizeOf(typeof(Class)) * i);
11 pClass[i] = (Class)Marshal.PtrToStructure(pPonitor, typeof(Class));
12 }
13 Marshal.FreeHGlobal(pBuff);
14 Console.ReadLine();
15 }
2 {
3 int size = Marshal.SizeOf(typeof(Class)) * 50;
4 byte[] bytes = new byte[size];
5 IntPtr pBuff = Marshal.AllocHGlobal(size);
6 Class[] pClass = new Class[50];
7 GetClass(pBuff);
8 for (int i = 0; i < 50; i++)
9 {
10 IntPtr pPonitor = new IntPtr(pBuff.ToInt64() + Marshal.SizeOf(typeof(Class)) * i);
11 pClass[i] = (Class)Marshal.PtrToStructure(pPonitor, typeof(Class));
12 }
13 Marshal.FreeHGlobal(pBuff);
14 Console.ReadLine();
15 }
有興趣的不妨自己測試一下,看看輸出結果是否正確!
全站熱搜