Win32 API Memory Functions

This section provides detailed information on Windows API functions related to memory management.

VirtualAlloc

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

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

Parameters

  • lpAddress: The starting address of the region of memory to allocate.
  • dwSize: The size of the region of memory to allocate, in bytes.
  • flAllocationType: The type of memory allocation.
  • flProtect: The memory protection for the region of pages to be allocated.

Return Value

If the function succeeds, the return value is the starting address of the allocated region. If the function fails, the return value is NULL.

Remarks

  • For more details, see the official Microsoft documentation.

VirtualAllocEx

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

LPVOID VirtualAllocEx(
  HANDLE hProcess,
  LPVOID lpAddress,
  SIZE_T dwSize,
  DWORD  flAllocationType,
  DWORD  flProtect
);

Parameters

  • hProcess: A handle to the process in which the memory is to be allocated.
  • lpAddress: The starting address of the region of memory to allocate.
  • dwSize: The size of the region of memory to allocate, in bytes.
  • flAllocationType: The type of memory allocation.
  • flProtect: The memory protection for the region of pages to be allocated.

Return Value

If the function succeeds, the return value is the starting address of the allocated region. If the function fails, the return value is NULL.

Remarks

  • This function is identical to VirtualAlloc except that it allows you to specify the process in which memory is to be allocated.

VirtualFree

Releases, decommits, or unlocks a region of pages in the virtual address space of the calling process.

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

Parameters

  • lpAddress: A pointer to the starting address of the region of pages to be freed.
  • dwSize: The size of the region of pages to be freed.
  • dwFreeType: The type of operation to perform on the specified region of pages.

Return Value

If the function succeeds, the return value is TRUE. If the function fails, the return value is FALSE.

VirtualFreeEx

Releases, decommits, or unlocks a region of pages in the virtual address space of a specified process.

BOOL VirtualFreeEx(
  HANDLE hProcess,
  LPVOID lpAddress,
  SIZE_T dwSize,
  DWORD  dwFreeType
);

Parameters

  • hProcess: A handle to the process in which the memory is to be freed.
  • lpAddress: A pointer to the starting address of the region of pages to be freed.
  • dwSize: The size of the region of pages to be freed.
  • dwFreeType: The type of operation to perform on the specified region of pages.

Return Value

If the function succeeds, the return value is TRUE. If the function fails, the return value is FALSE.

VirtualProtect

Changes the protection on a specified region of committed pages of virtual memory in the calling process and enumerates the changes to the region's protection.

BOOL VirtualProtect(
  LPVOID lpAddress,
  SIZE_T dwSize,
  DWORD  flNewProtect,
  PDWORD lpflOldProtect
);

Parameters

  • lpAddress: A pointer to the base address of the region of pages to be protected.
  • dwSize: The size, in bytes, of the region of pages to be protected.
  • flNewProtect: The new protection options for the region of memory.
  • lpflOldProtect: A pointer to a variable that receives the previous access protection level of the region.

Return Value

If the function succeeds, the return value is TRUE. If the function fails, the return value is FALSE.

VirtualProtectEx

Changes the protection on a specified region of committed pages of virtual memory in another process and enumerates the changes to the region's protection.

BOOL VirtualProtectEx(
  HANDLE hProcess,
  LPVOID lpAddress,
  SIZE_T dwSize,
  DWORD  flNewProtect,
  PDWORD lpflOldProtect
);

Parameters

  • hProcess: A handle to the process whose memory protection is to be changed.
  • lpAddress: A pointer to the base address of the region of pages to be protected.
  • dwSize: The size, in bytes, of the region of pages to be protected.
  • flNewProtect: The new protection options for the region of memory.
  • lpflOldProtect: A pointer to a variable that receives the previous access protection level of the region.

Return Value

If the function succeeds, the return value is TRUE. If the function fails, the return value is FALSE.

VirtualQuery

Retrieves information about a region of pages that have been committed or reserved in the virtual address space of the calling process.

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

Parameters

  • lpAddress: A pointer to the starting address of the region of memory to be queried.
  • lpBuffer: A pointer to a MEMORY_BASIC_INFORMATION structure that receives information about the region of pages.
  • dwLength: 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 stored in the buffer. If the function fails, the return value is 0. To get extended error information, call GetLastError.

VirtualQueryEx

Retrieves information about a region of pages that have been committed or reserved in the virtual address space of a specified process.

SIZE_T VirtualQueryEx(
  HANDLE                    hProcess,
  LPCVOID                   lpAddress,
  PMEMORY_BASIC_INFORMATION lpBuffer,
  SIZE_T                    dwLength
);

Parameters

  • hProcess: A handle to the process whose memory information is queried.
  • lpAddress: A pointer to the starting address of the region of memory to be queried.
  • lpBuffer: A pointer to a MEMORY_BASIC_INFORMATION structure that receives information about the region of pages.
  • dwLength: 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 stored in the buffer. If the function fails, the return value is 0. To get extended error information, call GetLastError.

GlobalAlloc

Allocates the specified number of bytes from the heap.

HGLOBAL GlobalAlloc(
  UINT  uFlags,
  SIZE_T dwBytes
);

Parameters

  • uFlags: The allocation type.
  • dwBytes: The number of bytes to allocate.

Return Value

If the function succeeds, the return value is a handle to the newly allocated memory object. If the function fails, the return value is NULL.

GlobalFree

Releases the specified global memory object.

HGLOBAL GlobalFree(
  HGLOBAL hMem
);

Parameters

  • hMem: A handle to the global memory object to be freed.

Return Value

If the function succeeds, the return value is NULL. If the function fails, the return value is a handle to the memory object that was not freed.

LocalAlloc

Allocates the specified number of bytes from local memory.

HLOCAL LocalAlloc(
  UINT  uFlags,
  SIZE_T uBytes
);

Parameters

  • uFlags: The allocation type.
  • uBytes: The number of bytes to allocate.

Return Value

If the function succeeds, the return value is a handle to the newly allocated memory object. If the function fails, the return value is NULL.

LocalFree

Frees the specified local memory object.

HLOCAL LocalFree(
  HLOCAL hMem
);

Parameters

  • hMem: A handle to the local memory object to be freed.

Return Value

If the function succeeds, the return value is NULL. If the function fails, the return value is a handle to the memory object that was not freed.