VirtualProtect

  BOOL VirtualProtect(lpAddress, dwSize, dwNewProtect, lpdwOldProtect)    
  LPVOID lpAddress;    
  DWORD dwSize;    
  DWORD dwNewProtect;    
  PDWORD lpdwOldProtect;    

The protection on a region of committed pages within the virtual address space of the calling process can be changed with the VirtualProtect function:

Parameters

lpAddress

A pointer that specifies the base address of the region of pages whose protection attributes are to be changed.

dwSize

A number that specifies the size in bytes of region of pages whose protection attributes are to be changed. It will be rounded up to the next host-page-size boundary. If this value is zero and the lpAddress parameter is the starting address of the allocated region, the complete range of pages allocated together is freed or decommitted.

dwNewProtect

The new protection desired for the specified region of pages.

Value Meaning

PAGE_NOACCESS  
  No access to the committed region of pages is allowed. An attempt to read, write, or execute the committed region results in an access violation (a GP fault).
PAGE_READONLY  
  Read access to the committed region of pages is allowed. An attempt to write or execute the committed region results in an access violation.
PAGE_READWRITE  
  Read and write access to the committed region of pages is allowed.

lpdwOldProtect

A pointer to a variable that will receive the old protection of the first page within the specified region of pages. If this parameter is NULL or does not point to a valid variable, the function will fail.

Return Value

The return value is TRUE if the function successfully queries the specified memory. Otherwise, it is FALSE in which case extended error information is available from the GetLastError function.

Setting the protection on a range of pages causes the old protection value to be replaced by a new protection value. The protection value can only be set on committed pages.

As each page is considered for protecting, its state is determined. If the state of the page is not committed or the page is reserved and cannot be auto-committed, then an appropriate status value is returned and none of the pages in the specified region is modified.