GetProcessHeap
Function
HANDLE GetProcessHeap(void);
Parameters
This function does not take any parameters.
Return Value
If the function succeeds, the return value is a handle to the process's default heap.
If the function fails, the return value is NULL. To get extended error information, call GetLastError.
Remarks
Every process has a default heap that is created automatically. The GetProcessHeap function retrieves a handle to this heap.
Handles to the default heap can be used with heap functions such as HeapAlloc and HeapFree.
Note that the default heap is not necessarily the same as the heap created by HeapCreate. The default heap is automatically managed by the system.
Requirements
Minimum supported client: Windows XP
Minimum supported server: Windows Server 2003
Header: windows.h
Library: Kernel32.lib
DLL: Kernel32.dll
Example Code
#include <windows.h>
#include <iostream>
int main() {
HANDLE hHeap = GetProcessHeap();
if (hHeap == NULL) {
std::cerr << "Failed to get process heap. Error: " << GetLastError() << std::endl;
return 1;
}
std::cout << "Successfully obtained handle to the process heap." << std::endl;
std::cout << "Heap Handle: " << hHeap << std::endl;
// Example of allocating memory from the process heap
SIZE_T allocationSize = 100;
LPVOID memoryBlock = HeapAlloc(hHeap, 0, allocationSize);
if (memoryBlock == NULL) {
std::cerr << "Failed to allocate memory from the process heap. Error: " << GetLastError() << std::endl;
return 1;
}
std::cout << "Allocated " << allocationSize << " bytes from the heap at address: " << memoryBlock << std::endl;
// In a real application, you would use the memoryBlock here.
// For demonstration, we will just free it.
if (HeapFree(hHeap, 0, memoryBlock)) {
std::cout << "Successfully freed memory block." << std::endl;
} else {
std::cerr << "Failed to free memory block. Error: " << GetLastError() << std::endl;
}
return 0;
}