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"
}
/*************************************** ***********/