C # Use of struct and pointer,

Source: Internet
Author: User

C # Use of struct and pointer,

// Struct Definition

[StructLayoutAttribute (LayoutKind. Sequential, CharSet = CharSet. Ansi, Pack = 1)]
Public struct VGAStat
{
Public int length;
[Financialas (UnmanagedType. ByValArray, SizeConst = 6)]
Public int [] number;
}


Public struct VGAStat1
{
Public string str;
Public IntPtr intptr1; // pointer Definition
}

Private void Form1_Load (object sender, EventArgs e)
{

/******** Initialize VGAStat *****************/
VGAStat pp = new VGAStat ();
Pp. length = 123;
Pp. number = new int [6];
For (int I = 0; I <6; I ++)
{
Pp. number [I] = I + 1;
}

/*************************************** ***********/

IntPtr intpt = Marshal. AllocHGlobal (Marshal. SizeOf (pp); // allocate memory to intpt
Marshal. StructureToPtr (pp, intpt, true); Messages data to intpt

 

/******** Initialize VGAStat1 *****************/

VGAStat1 vga = new VGAStat1 ();
Vga. intptr1 = intpt;

/*************************************** ***********/

 

VGAStat entries = (VGAStat) Marshal. PtrToStructure (vga. intptr1, typeof (VGAStat ));

 

*****************/
String Number = string. Empty;

Int ReadLength = entries. length; // ReadLength = 132.
For (int I = 0; I <6; I ++)
{
Number = Number + entries. number [I]. ToString (); // Number = "123456"
}

/*************************************** ***********/

Related Article

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.