Before you can parse the C # struct pointer, you must know how the C # struct body is defined. The struct is also defined in C #.
C # struct-body pointers in C # structure definition:
- [StructLayout (LayoutKind.Sequential)]
- Public struct Vgastat
- {
- public int channelnum; Number of channels
- [MarshalAs (UnmanagedType.ByValArray, SizeConst = 64)]
- Public char[] Version; Version Information
- public uint CPUUsage; CPU Consumption
- Public bool Workstatusok; //Working status
- [MarshalAs (UnmanagedType.ByValArray, SizeConst = 4)]
- Public tagcheckarg[] channelstatistic; //Channel information
- }
Once you have defined a struct, you can convert the received C # struct pointer to a defined struct body object.
- Vgastat entries = (vgastat) marshal.ptrtostructure (Iptr, typeof (Vgastat));
- The iptr is the unmanaged struct pointer received.
Conversely, structs can also be marshaled to unmanaged memory.
If the VGA is a struct that is instantiated and assigned after the definition.
- IntPtr IntPtr = Marshal.allochglobal (marshal.sizeof (VGA));
- Marshal.structuretoptr (VGA, IntPtr, true);
- Send the IntPtr pointer here to the destination party
- Marshal.freehglobal (IntPtr); //Releases the allocated unmanaged memory.
The definition of C # struct pointer and the related content of its use that's what I'm going to introduce you to, and hopefully it helps to understand and learn about C # struct pointers.
Convert String to Intptr:intptr System.Runtime.InteropServices.Marshal.StringToCoTaskMemAuto (string)
Convert IntPtr to String:string System.Runtime.InteropServices.MarshalPtrToStringAuto (INTPTR)
Http://msdn.microsoft.com/zh-cn/library/system.runtime.interopservices.marshal%28v=vs.110%29.aspx
The definition and use of C # struct pointers (IntPtr usage)