【飞秋】关于结构体和结构体指针的P-INVOKE
這篇講關于結構體和結構體指針的P-INVOKE,關鍵有4個P-INVOKE類型,結構體作為輸入輸出參數。結構體指針作為輸入輸出參數。還有結構體內的成員類型分為:數組,指針,指針數組,結構體,結構體指針,結構體數組,結構體指針數組。當然還有類繼承(這里只介紹了單繼承)。
其中有一個比較費解的是結構體作為返回值的P-INVOKE的奇怪現象,下一篇結合反匯編講解。
?即時通訊軟件
第一:C++結構體和C#結構體對應關系,看下面。這里提到一點C# 聲明結構體中的成員是數組的必須像下面那樣聲明:使用[MarshalAs(UnmanagedType.ByValArray, SizeConst = N)]
?
C++代碼不多,全部貼到這里:
1 struct Base
2 {
3 int BaseInt;
4 };
5
6? struct Test : Base
7 {
8 int TestIntArray[2];
9 //
10?? int * TestIntPointer;
11 int * TestIntPointerArray[2];
12 //
13?? Base TestBase;
14 Base * TestBasePoint;
15 Base TestBaseArray[2];
16 Base * TestBasePointerArray[2];
17 };
?
再來看C#的結構體聲明:
1 [StructLayout(LayoutKind.Sequential)]
2 public struct Base
3 {
4 public int BaseInt;
5 }
6
7 [StructLayout(LayoutKind.Sequential)]
8 public struct Test
9 {
10 public Base _base;//把繼承的基類放在第一個元素的位置。
11 //
12?? [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
13 public int[] TestIntArray;
14 //
15?? public IntPtr TestIntPointer;
16 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
17 public IntPtr[] TestIntPointerArray;
18 //
19?? public Base TestBase;
20 public IntPtr TestBasePoint;
21 //
22?? [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
23 public Base[] TestBaseArray;
24 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
25 public IntPtr[] TestBasePointerArray;
26 }
27?
?
第二。C++導出函數和C# P-INVOKE函數的對應。
C++:
1 static Test _test;
2 void SetTest(Test test)
3 {
4 _test = test;
5 PrintTest();
6 }
7
8 void SetTestPointer(Test * lptest)
9 {
10 _test = * lptest;
11 PrintTest();
12 }
13
14 Test GetTest()
15 {
16 return _test;
17 }
18
19 Test * GetTestPointer()
20 {
21 return &_test;
22 }
?
C#:?? 注意結構體作為返回值的P-INVOKE聲明是不是很奇怪。不過運行很正常。下一篇結合反匯編說。
?
1 [DllImport("TestDll")]
2 public static extern void SetTest(Test test);
3
4 [DllImport("TestDll")]
5 public static extern void SetTestPointer(IntPtr lptest);
6
7 [DllImport("TestDll")]
8 public static extern void GetTest(IntPtr lptest); //注意聲明。
9
10 [DllImport("TestDll")]
11 public static extern IntPtr GetTestPointer();
?
第三:看下C#如何調用,這里用到了Marshal.AllocHGlobal 方法,和Alloc功能基本一樣,會造成內存泄露,使用完了記住使用Marshal.FreeHGlobal函數釋放申請的內存。
?
1 private Test _test = new Test();
2
3 public void Run()
4 {
5 InitTest();
6 //#########################
7 SetTest(_test);
8 Console.WriteLine("-------------------------------------------------------------/n");
9 //#########################
10 _test._base.BaseInt = 9999;
11 //Marshal.AllocHGlobal 和WIN32 API, Alloc功能基本一樣,
12 //這個方法不要多用,可能造成內存泄露。
13 //記住使用Marshal.FreeHGlobal函數釋放申請的內存
14 IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Test)));
15 Marshal.StructureToPtr(_test,p,false);
16 SetTestPointer(p);
17 Console.WriteLine("-------------------------------------------------------------/n");
18 //#########################
19 IntPtr pp = GetTestPointer();
20 Test temp = (Test)Marshal.PtrToStructure(pp, typeof(Test));
21 PrintTest(temp);
22 Console.WriteLine("-------------------------------------------------------------/n");
23
24 //#########################
25 IntPtr pp2 = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Test)));
26 GetTest(pp2);
27 Test temp2 = (Test)Marshal.PtrToStructure(pp2, typeof(Test));
28 PrintTest(temp2);
29
30 }
?
總結一下:Marshal.StructureToPtr從托管類復制數據到未托管的內存中,Marshal.PtrToStructure恰好相反。Marshal.AllocHGlobal申請非托管內存,Marshal.FreeHGlobal函數釋放非托管內存。使用 Marshal.Read系列讀寫指針,使用Marshal.ReadIntPtr來讀寫二級指針。
?
關注技術文章飛秋:http://www.freeeim.com/,24小時專業(yè)轉載。
總結
以上是生活随笔為你收集整理的【飞秋】关于结构体和结构体指针的P-INVOKE的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [计算机视觉:算法与应用]学习笔记一:图
- 下一篇: [Medical Image Proce