Kernel32VirtualAlloc(UIntPtr, UIntPtr, MemoryAllocationType, MemoryProtectionOptions) Method

Reserves, commits, or changes the state of a region of pages in the virtual address space of the calling process.

Memory allocated by this function is automatically initialized to zero.

To allocate memory in the address space of another process, use the VirtualAllocEx(IntPtr, IntPtr, IntPtr, MemoryAllocationType, MemoryProtectionOptions) function.

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 UIntPtr VirtualAlloc(
	UIntPtr address,
	UIntPtr size,
	MemoryAllocationType allocationType,
	MemoryProtectionOptions protection
)

Parameters

address  UIntPtr
The starting address of the region to allocate.

If the memory is being reserved, the specified address is rounded down to the nearest multiple of the allocation granularity.

If the memory is already reserved and is being committed, the address is rounded down to the next page boundary.

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

If this parameter is Zero, the system determines where to allocate the region.

If this address is within an enclave that you have not initialized by calling InitializeEnclave, VirtualAlloc(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  UIntPtr
The size of the region, in bytes.

If the address parameter is Zero, this value is rounded up to the next page boundary. Otherwise, the allocated pages include all pages containing one or more bytes in the range from address to address+size.

This means that a 2-byte range straddling a page boundary causes both pages to be included in the allocated region.

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

UIntPtr
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