首页 > Windows开发 > 详细

C#访问非托管内存

时间:2015-12-10 18:37:33      阅读:218      评论:0      收藏:0      [点我收藏+]

示例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);

 

C#访问非托管内存

原文:http://www.cnblogs.com/nanfei/p/5036583.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!