VirtualProtect (Windows Embedded CE 6.0)
1/6/2010
This function changes the access protection on a region of committed pages in the virtual address space of the calling process.
Syntax
BOOL VirtualProtect(
LPVOID lpAddress,
DWORD dwSize,
DWORD flNewProtect,
PDWORD lpflOldProtect
);
Parameters
lpAddress
[in] 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 be allocated in a single call to the VirtualAlloc function.
The pages cannot span adjacent regions that were allocated by separate calls to VirtualAlloc.
dwSize
[in] 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 plus dwSize. This means that a 2-byte range straddling a page boundary causes the protection attributes of both pages to be changed.
flNewProtect
[in] Specifies the new access protection.You can specify any one of the following flags, along with the PAGE_GUARD and PAGE_NOCACHE protection modifier flags, as necessary.
Value Description 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_GUARD
Pages in the region become guard pages.
An 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 use. 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 valid when used with a page protection other than PAGE_NOACCESS.
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.
lpflOldProtect
[out] 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 Value
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
Header | winbase.h |
Library | coredll.lib |
Windows Embedded CE | Windows CE 1.0 and later |
See Also
Reference
Memory Management Functions
VirtualAlloc