示例1:分配一个新的内存地址给新变量

Point p;
// Initialize unmanged memory to hold the struct.
IntPtr pnt = Marshal.AllocHGlobal(Marshal.SizeOf(p));
// Copy the struct to unmanaged memory.
Marshal.StructureToPtr(p, pnt, false); // Create another point.
Point anotherP; // Set this Point to the value of the
// Point in unmanaged memory.
anotherP = (Point)Marshal.PtrToStructure(pnt, typeof(Point));
Marshal.FreeHGlobal(pnt);

示例2:将一个字符串地址转换为字符串,以及获取字符串的地址:

IntPtr lpFileName;
string filename = Marshal.PtrToStringUni(lpFileName);
lpFileName = Marshal.StringToHGlobalUni(filename);
04-15 18:08