VirtualProtect

This function changes the access protection on a region of committed pages in the virtual address space of the calling process.

BOOL VirtualProtect( 
LPVOID lpAddress, 
DWORD dwSize, 
DWORD flNewProtect, 
PDWORD lpflOldProtect 
); 

Parameters

  • lpAddress
    Pointer to the base address of the region of pages whose access protection attributes are to be changed. All pages in the specified region must have been allocated in a single call to the VirtualAlloc function. The pages cannot span adjacent regions that were allocated by separate calls to VirtualAlloc.

  • dwSize
    Specifies the size, in bytes, of the region whose access protection attributes are to be changed. 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.

  • flNewProtect
    Specifies the new access protection. You can specify any one of the flags listed in the following table, along with the PAGE_GUARD and PAGE_NOCACHE protection modifier flags, as necessary.

    Value Description
    PAGE_READONLY Enables read access to the committed region of pages. An attempt to write to the committed region results in an access violation. If the system differentiates between read-only access and execute access, an attempt to execute code in the committed region results in an access violation.
    PAGE_READWRITE Enables both read and write access to the committed region of pages.
    PAGE_WRITECOPY Gives copy-on-write access to the committed region of pages.
    PAGE_EXECUTE Enables execute access to the committed region of pages. An attempt to read or write to the committed region results in an access violation.
    PAGE_EXECUTE_READ Enables execute and read access to the committed region of pages. An attempt to write to the committed region results in an access violation.
    PAGE_EXECUTE_READWRITE Enables execute, read, and write access to the committed region of pages.
    PAGE_EXECUTE_WRITECOPY Enables execute, read, and write access to the committed region of pages. The pages are shared read-on-write and copy-on-write.
    PAGE_GUARD Pages in the region become guard pages. Any attempt to access a guard page causes the system to raise a STATUS_GUARD_PAGE exception and turn off the guard page status. Guard pages thus act as a one-shot access alarm.

    The PAGE_GUARD flag is a page protection modifier. An application uses it with one of the other page protection flags, with one exception: it cannot be used with PAGE_NOACCESS. When an access attempt leads the system to turn off guard page status, the underlying page protection takes over.

    If a guard page exception occurs during a system service, the service typically returns a failure status indicator.

    PAGE_NOACCESS Disables all access to the committed region of pages. An attempt to read from, write to, or execute in the committed region results in an access violation exception, called a general protection (GP) fault.
    PAGE_NOCACHE Allows no caching of the committed regions of pages. The hardware attributes for the physical memory should be specified as "no cache." This is not recommended for general usage. It is useful for device drivers; for example, mapping a video frame buffer with no caching. This flag is a page protection modifier and is only valid when used with one of the page protections other than PAGE_NOACCESS.
  • lpflOldProtect
    Long pointer to a variable that the function sets to the previous access protection value of the first page in the specified region of pages. If this parameter is NULL or does not point to a valid variable, the function fails.

Return Values

Nonzero indicates success. Zero indicates failure. To get extended error information, call GetLastError.

Remarks

The following code example shows how to use the VirtualProtect function to change the characteristics of pages in virtual memory that were allocated with READ_WRITE access to READ_ONLY.

DWORD dwOldProtect;
DWORD dwSize = (2 * PAGE_SIZE);  //To change a set of two pages, 
//whose size was defined earlier as PAGE_SIZE

if (!VirtualProtect (lpPage,    // Beginning of the set of pages to change
                     dwSize,    // Length, in bytes, of the set of pages 
                                //to change
                     PAGE_READONLY, // What to change it to
                     &dwOldProtect  // Place to store the old setting
                     ))
{// Your error-handling code goes here. 
} 

Requirements

Runs on Versions Defined in Include Link to
Windows CE OS 1.0 and later Winbase.h   Coredll.lib

Note   This API is part of the complete Windows CE OS package as provided by Microsoft. The functionality of a particular platform is determined by the original equipment manufacturer (OEM) and some devices may not support this API.

See Also

GetLastError, VirtualAlloc

 Last updated on Tuesday, July 13, 2004

© 1992-2000 Microsoft Corporation. All rights reserved.