现在的位置: 首页 > 综合 > 正文

使用VirtualAlloc()创建虚拟内存,修改可读、可写属性

2013年10月18日 ⁄ 综合 ⁄ 共 585字 ⁄ 字号 评论关闭

#include <windows.h>

void main()
{
 SYSTEM_INFO sf;
 GetSystemInfo(&sf);

 //分配内存,标记为提交、可读可写
 LPVOID lpvBase = VirtualAlloc(
        NULL,                 // system selects address
        4096,     // size of allocation
        MEM_COMMIT,   // allocate reserved pages
        PAGE_READWRITE);       // protection = no access
   if (lpvBase == NULL )
   return;

 //向该内存里面写些东西
 unsigned char *ustr=(unsigned char *)lpvBase;
 ustr[0]=0x89;

 //修改为“只读”属性,验证是否能写入
 DWORD dw;
 VirtualProtect(lpvBase,4096,PAGE_READONLY,&dw);
// ustr[0]=0x44; //失败

 //修改为“不可访问”,验证是否能读出
 VirtualProtect(lpvBase,4096,PAGE_NOACCESS,&dw);
// dw = ustr[0]; //失败

 

 

 return;

抱歉!评论已关闭.