Before parsing the C # struct pointer, you must know how the C # struct is defined. This struct is also defined in C.

Source: Internet
Author: User

Before parsing the C # struct pointer, you must know how the C # struct is defined. This struct is also defined in C.

C # struct pointer-C # struct definition:

[Structlayout (layoutkind. Sequential)]

Public struct vgastat

{

Public int channelnum; // number of channels


[Financialas (unmanagedtype. byvalarray, sizeconst = 64)]

Public char [] version; // version information

Public uint cpuusage; // CPU usage

Public bool workstatusok; // working status


[Financialas (unmanagedtype. byvalarray, sizeconst = 4)]

Public tagcheckarg [] channelstatistic; // channel information

}

After defining the struct, you can convert the received C # struct pointer to the defined struct object.

 

Vgastat entries = (vgastat) Marshal. ptrtostructure (iptr, typeof (vgastat ));

// Iptr is the received unmanaged struct pointer.

On the contrary, you can assign values to the struct and mail it to the unmanaged memory.

Assume that VGA is the struct that is instantiated and assigned a value after definition.

Intptr = marshal. allochglobal (marshal. sizeof (VGA ));

Marshal. structuretoptr (VGA, intptr, true );

// Send the intptr pointer to the target party here

Marshal. freehglobal (intptr); // release the allocated unmanaged memory.

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.