The P-INVOKE of struct and struct pointer

Source: Internet
Author: User

This article about struct and struct pointer P-INVOKE, the key has four P-INVOKE types, struct as input and output parameters. Struct pointer as input and output parameters. In addition, the member types in the Structure Body are divided into: array, pointer, pointer array, struct, struct pointer, struct array, and struct pointer array. Of course there are class inheritance (Here we only introduce Single inheritance ).
One of the more puzzling is the structure as the return value of the P-INVOKE of the strange phenomenon, the next article combined with disassembly.

Instant Messaging Software

First, the correspondence between the C ++ struct and the C # struct is shown below. Here, we mention that C # declares that the Members in the struct must be declared as arrays as follows: use [exploralas (unmanagedtype. byvalarray, sizeconst = n)]

 

There are not many c ++ codes, all of which are posted here:

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 };

 

Let's take a look at the structure declaration of 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; // place the inherited base class at the position of the first element.
11 //
12 [financialas (unmanagedtype. byvalarray, sizeconst = 2)]
13 public int [] testintarray;
14 //
15 public intptr testintpointer;
16 [financialas (unmanagedtype. byvalarray, sizeconst = 2)]
17 Public intptr [] testintpointerarray;
18 //
19 public base testbase;
20 public intptr testbasepoint;
21 //
22 [financialas (unmanagedtype. byvalarray, sizeconst = 2)]
23 public base [] testbasearray;
24 [financialas (unmanagedtype. byvalarray, sizeconst = 2)]
25 public intptr [] testbasepointerarray;
26}
27

 

Second. C ++ export function and C # P-INVOKE function.
C ++:

1 static test _ test;
2 void settest (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 #: Pay attention to struct as the return value of the P-INVOKE declaration is not very strange. However, it runs normally. The next article will be combined with the disassembly.

 

1 [dllimport ("testdll")]
2 public static extern void settest (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); // pay attention to the Declaration.
9
10 [dllimport ("testdll")]
11 public static extern intptr gettestpointer ();

 

Third: Let's take a look at how C # Calls. Marshal is used here. the allochglobal method is basically the same as the alloc method, which may cause memory leakage. the freehglobal function releases the applied memory.

 

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 and Win32 APIs, alloc functions are basically the same,
12 // This method should not be used in multiple ways, which may cause memory leakage.
13 // remember to use the marshal. freehglobal function to release the applied memory
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}

 

To sum up, Marshal. structuretoptr copies data from a hosted class to an unmanaged memory, and the opposite is true for Marshal. ptrtostructure. Marshal. allochglobal applies for unmanaged memory, and the Marshal. freehglobal function releases the unmanaged memory. Use marshal. Read series read/write pointers and marshal. readintptr to read and write second-level pointers.

 

Follow the technical Article Fei Qiu: http://www.freeeim.com/, 24 hours professional transfer.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.