VirtualProtectEx

  BOOL VirtualProtectEx(hProcess, lpAddress, dwSize, dwNewProtect, lpdwOldProtect)    
  HANDLE hProcess;    
  LPVOID lpAddress;    
  DWORD dwSize;    
  DWORD dwNewProtect;    
  PDWORD lpdwOldProtect;    

The protection on a region of committed pages within the virtual address space of the specified process can be changed with the VirtualProtectEx function.

Parameters

hProcess

Supplies a handle to the process whose memory is being protected. The handle must have been created with PROCESS_VM_OPERATION access.

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 was successful, or FALSE if an error occurred. To obtain extended error information, use the GetLastError function.

Comments

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 error value is returned and none of the pages in the specified region is modified.

See Also

VirtualProtect, VirtualQueryEx