Win32 Memory Management API

This section provides an overview and detailed reference for the Win32 API functions related to memory management in Windows.

Core Memory Allocation Functions

These functions are fundamental for requesting and releasing blocks of memory from the system heap.

Memory Information and Manipulation

Functions for querying memory status, protecting memory regions, and copying memory blocks.

Specific Memory API Details

VirtualAlloc


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. If this parameter is NULL, the system determines where to allocate the region.
dwSize SIZE_T The size of the region to allocate, in bytes.
flAllocationType DWORD The type of memory allocation. This parameter can be one or more of the following values:
MEM_COMMIT, MEM_RESERVE, MEM_RESET, MEM_RESET_UNDO, MEM_LARGE_PAGES, MEM_PHYSICAL, MEM_TOP_DOWN.
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_NOACCESS, PAGE_READONLY, PAGE_READWRITE, PAGE_WRITECOPY, PAGE_TARGETS_INVALID, PAGE_TARGETS_NO_UPDATE.

Return Value:

If the function succeeds, it returns a pointer to the allocated region. Otherwise, it returns NULL.

Note: When using MEM_RESERVE and MEM_COMMIT together, dwSize is rounded up to the next page boundary.

VirtualFree


BOOL VirtualFree(
  LPVOID lpAddress,
  SIZE_T dwSize,
  DWORD  dwFreeType
);
            

Parameters:

Parameter Type Description
lpAddress LPVOID A pointer to the starting address of the region of pages to be freed.
dwSize SIZE_T The size of the region to free, in bytes. This parameter must be 0 if dwFreeType is MEM_RELEASE.
dwFreeType DWORD The type of operation to perform on the specified memory region. This parameter can be one of the following values:
MEM_DECOMMIT, MEM_RELEASE.

Return Value:

If the function succeeds, it returns TRUE. Otherwise, it returns FALSE.

HeapAlloc


LPVOID HeapAlloc(
  HANDLE hHeap,
  DWORD  dwFlags,
  SIZE_T dwBytes
);
            

Parameters:

Parameter Type Description
hHeap HANDLE A handle to the heap that was created by a call to the HeapCreate function.
dwFlags DWORD Flags that control the heap allocation.
HEAP_GENERATE_EXCEPTIONS, HEAP_NO_SERIALIZE, HEAP_ZERO_MEMORY.
dwBytes SIZE_T The number of bytes to allocate from the heap.

Return Value:

If the function succeeds, it returns a pointer to the allocated memory. If the function fails, it returns NULL.

VirtualQuery


SIZE_T VirtualQuery(
  LPCVOID                   lpAddress,
  PMEMORY_BASIC_INFORMATION lpBuffer,
  SIZE_T                    dwLength
);
            

Parameters:

Parameter Type Description
lpAddress LPCVOID A pointer to the starting address of the region of pages to be queried.
lpBuffer PMEMORY_BASIC_INFORMATION A pointer to a MEMORY_BASIC_INFORMATION structure that receives information about the region of pages.
dwLength SIZE_T The size of the buffer pointed to by lpBuffer, in bytes.

Return Value:

If the function succeeds, the return value is the number of bytes of information written to the buffer. If the function fails, the return value is 0.