VirtualProtectEx

VirtualProtectEx

VirtualProtectEx函式可以改變在特定進程中記憶體區域的保護屬性。

基本介紹

  • 外文名:VirtualProtectEx
  • 類型:函式
  • 函式原形:BOOL VirtualProtectEx
  • 屬性:保護屬性
基本信息,參數,返回值,說明,

基本信息

VirtualProtectEx 函式
The VirtualProtectEx function changes the protection on a region of committed pages in the virtual address space of a specified process.
這個函式可以改變在特定進程中記憶體區域的保護屬性。
函式原形:
BOOL VirtualProtectEx(
HANDLE hProcess, // 要修改記憶體的進程句柄
LPVOID lpAddress, // 要修改記憶體的起始地址
DWORD dwSize, // 頁區域大小
DWORD flNewProtect, // 新訪問方式
PDWORD lpflOldProtect // 原訪問方式 用於保存改變前的保護屬性 易語言要傳址
);

參數

hProcess
Handle to the process whose memory protection is to be changed. The handle must have PROCESS_VM_OPERATION access. For more information on PROCESS_VM_OPERATION, see OpenProcess.
目的進程的句柄。
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 be within the same reserved region allocated when calling the VirtualAlloc or VirtualAllocEx function using MEM_RESERVE. The pages cannot span adjacent reserved regions that were allocated by separate calls to VirtualAlloc or VirtualAllocEx using MEM_RESERVE.
要修改記憶體的起始地址。
dwSize
Size of the region whose access protection attributes are 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.
要修改記憶體的大小
flNewProtect
Memory protection. This parameter can be one of the memory protection options, along with PAGE_GUARD or PAGE_NOCACHE, as needed.
保護選項,見下。
lpflOldProtect
[out] Pointer to a variable that receives the previous access protection 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.
必須指向一個有效的變數,如果是NULL或者無效的變數,函式將失敗。

返回值

If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
如果成功,返回非零。失敗返回零。

說明

The access protection value can be set only on committed pages. If the state of any page in the specified region is not committed, the function fails and returns without modifying the access protection of any pages in the specified region.
只能在已經提交的頁面上進行操作,否則返回失敗。
The PAGE_GUARD protection modifier establishes guard pages. Guard pages act as one-shot access alarms. For more information, see Creating Guard Pages.
It is best to avoid using VirtualProtectEx to change page protections on memory blocks allocated by GlobalAlloc, HeapAlloc, or LocalAlloc, because multiple memory blocks can exist on a single page. The heap manager assumes that all pages in the heap grant at least read and write access.
Windows Me/98/95: You cannot use VirtualProtectEx on any memory region located in the shared virtual address space (from 0x80000000 through 0xBFFFFFFF).
其中DWORD flNewProtect可選擇:
#define PAGE_NOACCESS 0x01
#define PAGE_READONLY 0x02
#define PAGE_READWRITE 0x04
#define PAGE_WRITECOPY 0x08
#define PAGE_EXECUTE 0x10
#define PAGE_EXECUTE_READ 0x20
#define PAGE_EXECUTE_READWRITE 0x40
#define PAGE_EXECUTE_WRITECOPY 0x80
#define PAGE_GUARD 0x100
#define PAGE_NOCACHE 0x200
#define PAGE_WRITECOMBINE 0x400
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.

相關詞條

熱門詞條

聯絡我們