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 targetMarshal. freehglobal (intptr );//Release the allocated unmanaged memory.
The definition and usage of the C # struct pointer will introduce you here, hoping to help you understand and learn the C # struct pointer.
Reprinted from: http://developer.51cto.com/art/200908/143813.htm