static int g_n=0;
static int g_y=0;
DWORD dwFlag;
VirtualProtect(&g_n,4,PAGE_READONLY,&dwFlag);
g_y=1;//运行到这里就报错了,VirtualProtect实际的保护内存大小是多少啊?

解决方案 »

  1.   

    DWORD dwFlag;应修改为:
    DWORD dwFlag = 0;dwFlag如果不赋初值,地址不确定。
      

  2.   

    地址空间是按块的, 同一块空间相同属性, 可以用 VirtualQuery 获取具体的大小
      

  3.   

    BOOL WINAPI VirtualProtect(
      __in   LPVOID lpAddress,
      __in   SIZE_T dwSize,
      __in   DWORD flNewProtect,
      __out  PDWORD lpflOldProtect
    );dwSize 
    The size of the region whose access protection attributes are to be changed, in bytes. The region of affected pages includes all pages containing one or more bytes in the range from the lpAddress parameter to (lpAddress+dwSize). This means that a 2-byte range straddling a page boundary causes the protection attributes of both pages to be changed.
      

  4.   

    没错,Windows只是用了分页机制,所以只能按4KB一个页为最小的保护粒度。