【飛秋】關於結構體和結構體指標的P-INVOKE

來源:互聯網
上載者:User

這篇講關於結構體和結構體指標的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小時專業轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.