Kernel32VirtualProtectEx(IntPtr, UIntPtr, UIntPtr, MemoryProtectionOptions, MemoryProtectionOptions) Method

Changes the protection on a region of committed pages in the virtual address space of a specified process.

Definition

Namespace: DevCase.Win32.NativeMethods
Assembly: DevCase.net48 (in DevCase.net48.dll) Version: 6.0.0.0 (6.0)
XMLNS for XAML: Not mapped to an xmlns.
[DllImportAttribute("Kernel32.dll", SetLastError = true)]
public static bool VirtualProtectEx(
	IntPtr hProcess,
	UIntPtr address,
	UIntPtr size,
	MemoryProtectionOptions newProtect,
	out MemoryProtectionOptions refOldProtect
)

Parameters

hProcess  IntPtr
A handle to the process whose memory protection is to be changed. The handle must have the VirtualMemoryOperation access right.
address  UIntPtr
A pointer an address that describes the starting page 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(IntPtr, IntPtr, MemoryAllocationType, MemoryProtectionOptions) or VirtualAllocEx(IntPtr, IntPtr, IntPtr, MemoryAllocationType, MemoryProtectionOptions) function using Reserve flag.

The pages cannot span adjacent reserved regions that were allocated by separate calls to VirtualAlloc(IntPtr, IntPtr, MemoryAllocationType, MemoryProtectionOptions) or VirtualAllocEx(IntPtr, IntPtr, IntPtr, MemoryAllocationType, MemoryProtectionOptions) function using Reserve flag.

size  UIntPtr
The size of the region whose access protection attributes are to be changed, in bytes.

The region of affected pages includes all pages containing one or more bytes in the range from the address parameter to (address+size).

This means that a 2-byte range straddling a page boundary causes the protection attributes of both pages to be changed.

newProtect  MemoryProtectionOptions
The memory protection option.
refOldProtect  MemoryProtectionOptions
A pointer to a variable that receives the previous memory protection option value of the first page in the specified region of pages.

If this parameter is or does not point to a valid variable, the function fails.

Return Value

Boolean
If the function succeeds, the return value is .

If the function fails, the return value is .

To get extended error information, call GetLastWin32Error.

Remarks

See Also