您的位置:首页 > 编程语言 > C#

C#访问非托管内存

2015-12-10 16:56 471 查看
示例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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: