原文链接 -> 传送门
函数功能:
HeapSetInformation 函数用于启用指定堆的功能特性。
API 函数原型:
注释:_In_ 说明该参数是输入的,_opt_ 说明该参数是可选的。
BOOL WINAPI HeapSetInformation( _In_opt_ HANDLE HeapHandle, _In_ HEAP_INFORMATION_CLASS HeapInformationClass, _In_ PVOID HeapInformation, _In_ SIZE_T HeapInformationLength );
参数解析:
|
参数 |
含义 |
||||||||
|
HeapHandle |
指向待设置属性的堆句柄,该句柄由 HeapCreate 函数或者 GetProcessHeap 函数返回获取 |
||||||||
|
HeapInformationClass |
1. 指定待设置的堆信息
|
||||||||
|
HeapInformation |
1. 堆信息缓冲区。该数据的格式取决于 HeapInformationClass 参数 |
||||||||
|
HeapInformationLength |
指定堆信息缓冲区大小,单位字节 |
返回值:
1. 如果函数调用成功,返回值是非 0;
2. 如果函数调用失败,返回值是 0。
若想获得更多的错误信息,请调用 GetLastError 函数。
备注:
1.调用 HeapQueryInformation 函数获取当前的堆设置。
2.强烈推荐设置 HeapEnableTerminationOnCorruption 选项,它利用一个损坏的堆减少了应用程序安全漏洞曝光。
例子:
下面代码是如何启用低碎片堆:
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#define HEAP_LFH 2
int __cdecl _tmain()
{
BOOL bResult;
HANDLE hHeap;
ULONG HeapInformation;
//
// Enable heap terminate-on-corruption.
// A correct application can continue to run even if this call fails,
// so it is safe to ignore the return value and call the function as follows:
// (void)HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
// If the application requires heap terminate-on-corruption to be enabled,
// check the return value and exit on failure as shown in this example.
//
bResult = HeapSetInformation(NULL,
HeapEnableTerminationOnCorruption,
NULL,
0);
if (bResult != FALSE) {
_tprintf(TEXT("Heap terminate-on-corruption has been enabled.\n"));
}
else {
_tprintf(TEXT("Failed to enable heap terminate-on-corruption with LastError %d.\n"),
GetLastError());
return 1;
}
//
// Create a new heap with default parameters.
//
hHeap = HeapCreate(0, 0, 0);
if (hHeap == NULL) {
_tprintf(TEXT("Failed to create a new heap with LastError %d.\n"),
GetLastError());
return 1;
}
//
// Enable the low-fragmenation heap (LFH). Starting with Windows Vista,
// the LFH is enabled by default but this call does not cause an error.
//
HeapInformation = HEAP_LFH;
bResult = HeapSetInformation(hHeap,
HeapCompatibilityInformation,
&HeapInformation,
sizeof(HeapInformation));
if (bResult != FALSE) {
_tprintf(TEXT("The low-fragmentation heap has been enabled.\n"));
}
else {
_tprintf(TEXT("Failed to enable the low-fragmentation heap with LastError %d.\n"),
GetLastError());
return 1;
}
return 0;
}
需求:
|
Minimum supported client |
Windows XP [桌面应用程序 | Windows Store 程序] |
|
Minimum supported server |
Windows 2003 服务器版 [桌面应用程序 | Windows Store 程序] |
|
Header |
HeapApi.h (包含于 Windows.h); |
|
Library |
Kernel32.lib |
|
DLL |
Kernel32.dll |




