Kernel32.dll Functions

VirtualAlloc

Allocates a region of memory in the virtual address space of the calling process.

Syntax

LPVOID VirtualAlloc(
  LPVOID lpAddress,
  SIZE_T dwSize,
  DWORD  flAllocationType,
  DWORD  flProtect
);

Parameters

Parameter Type Description
lpAddress LPVOID The starting address of the region to allocate. This parameter can be NULL. If NULL, the system determines where to allocate the region.
dwSize SIZE_T The size of the region of memory to allocate, in bytes.
flAllocationType DWORD The type of memory allocation. This parameter can be one of the following values:
  • MEM_COMMIT: Allocates physical memory for the specified reserved memory pages.
  • MEM_RESERVE: Reserves a range of the process's virtual address space without any actual allocation of physical storage.
  • MEM_RESET: Indicates that data in the memory region specified by lpAddress and dwSize is no longer of interest.
  • MEM_RESET_UNDO: Indicates that the data in the memory region is of interest again.
flProtect DWORD The memory protection for the region of pages to be allocated. This parameter can be one of the following values:
  • PAGE_EXECUTE
  • PAGE_EXECUTE_READ
  • PAGE_EXECUTE_READWRITE
  • PAGE_EXECUTE_WRITECOPY
  • PAGE_READONLY
  • PAGE_READWRITE
  • PAGE_WRITECOPY

Return Value

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 NULL. To get extended error information, call GetLastError.

Remarks

This function allows you to allocate memory with specific protection attributes. It's often used for allocating executable code or memory that requires specific read/write/execute permissions.

See Also