Kernel32VirtualAllocEx(IntPtr, IntPtr, IntPtr, MemoryAllocationType, MemoryProtectionOptions) Method

Reserves, commits, or changes the state of a region of memory within the virtual address space of a specified process.

The function initializes the memory it allocates to zero.

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", ExactSpelling = true, SetLastError = true)]
public static IntPtr VirtualAllocEx(
	IntPtr hProcess,
	IntPtr address,
	IntPtr size,
	MemoryAllocationType allocationType,
	MemoryProtectionOptions protection
)

Parameters

hProcess  IntPtr
The handle to a process.

The function allocates memory within the virtual address space of this process.

The handle must have the VirtualMemoryOperation access right

address  IntPtr
The pointer that specifies a desired starting address for the region of pages that you want to allocate.

If you are reserving memory, the function rounds this address down to the nearest multiple of the allocation granularity.

If you are committing memory that is already reserved, the function rounds this address down to the nearest page boundary.

To determine the size of a page and the allocation granularity on the host computer, use the GetSystemInfo function.

If address is Zero, the function determines where to allocate the region.

If this address is within an enclave that you have not initialized by calling InitializeEnclave function, VirtualAllocEx(IntPtr, IntPtr, IntPtr, MemoryAllocationType, MemoryProtectionOptions) allocates a page of zeros for the enclave at that address.

The page must be previously uncommitted, and will not be measured with the EEXTEND instruction of the Intel Software Guard Extensions programming model.

If the address in within an enclave that you initialized, then the allocation operation fails with the ERROR_INVALID_ADDRESS error.

size  IntPtr
The size of the region of memory to allocate, in bytes.

If address is Zero, the function rounds size up to the next page boundary.

If address is not Zero, the function allocates all pages that contain one or more bytes in the range from address to address+size. This means, for example, that a 2-byte range that straddles a page boundary causes the function to allocate both pages.

allocationType  MemoryAllocationType
The type of memory allocation.
protection  MemoryProtectionOptions
The memory protection for the region of pages to be allocated.

If address parameter specifies an address within an enclave, protection cannot be any of the following values:

NoAccess, Guard, NoCache, WriteCombine

Return Value

IntPtr
If the function succeeds, the return value is the base address of the allocated region of pages.

If the function fails, the return value is Zero.

To get extended error information, call GetLastWin32Error.

Remarks

See Also