捐助郴维网
感谢您对郴维网的支持,你的支持将是郴维网持续发展的动力!
二维码
×
当前位置:郴维网 >API档案 > 正文
18 2018.02

HeapSetInformation

点击次数:1452 更新时间:2018/2/18 16:36:02  【打印此页

原文链接 -> 传送门

函数功能:

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. 指定待设置的堆信息

2. 该参数可以是下列 HEAP_INFORMATION_CLASS 枚举类型之一:

含义

HeapCompatibilityInformation
(0)

1. 启用堆功能特性。只有低碎片堆提供支持。然而,应用程序启用低碎片堆是非必要的,因为系统使用低碎片堆需要服务内存分配请求

2. Windows XP 和 Windows Server 2003:
  a. 低碎片堆默认处于禁用状态。设置 HeapInformation 参数指向的变量为 2,低碎片堆开启之后就不能被禁用
  b. 以 HEAP_NO_SERIALIZE 参数值创建的堆和以固定大小创建的堆不能启用低碎片堆。如果你正在使用堆调试工具 Windows 调试工具或者微软程序校验机调试工具,低碎片堆也不能开启
  c. 当进程在调试模式下运行,进程中的所有堆的某些堆调试选项自动启用。这些堆调试选项阻止低碎片堆使用。设置 _NO_DEBUG_HEAP 环境变量为 1,以在调试模式下启用低碎片堆

HeapEnableTerminationOnCorruption
(1)

1. 启用堆溢出终止功能特性。如果堆内存管理函数在进程使用的堆中检测到错误,它会调用 Windows 错误报告服务同时结束进程

2. 进程启用该功能后,不能再禁用

3. Windows Server 2003 和 Windows XP:直到 Windows Vista 和 Windows XP SP3 版本才支持该参数值。函数会调用成功,但 HeapEnableTerminationOnCorruption 参数值被忽略

HeapOptimizeResources
(3)

1. 如果指定 HeapHandle 参数为 NULL 调用 HeapSetInformation 函数,那么在低碎片堆进程的所有堆缓存将会被充分利用,内存也尽可能释放

2. 如果指定 HeapHandle 参数为指向堆的指针调用 HeapSetInformation 函数,那么只有 HeapHandle 指向的堆缓存将会被充分利用

3. 注意:传递到 HeapInformation 参数的 HEAP_OPTIMIZE_RESOURCES_INFORMATION 结构必须正确的初始化

4. 注意:该值在 Windows 8.1 版本中添加

HeapInformation

1. 堆信息缓冲区。该数据的格式取决于 HeapInformationClass 参数

2. 如果 HeapInformationClass 参数值是 HeapCompatibilityInformation ,则 HeapInformation 参数为一个指向 ULONG 类型变量指针

3. 如果 HeapInformationClass 参数值是 HeapEnableTerminationOnCorruption ,则 HeapInformation 参数应该为 NULL 且 HeapInformationLength 参数为 0

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);
WinBase.h 在 Windows Server 2008 R2, Windows 7, Windows Server 2008, Windows Vista, Windows Server 2003, and Windows XP 上(包含于 Windows.h)

Library

Kernel32.lib

DLL

Kernel32.dll

 

提示
郴维网为您提供各类专业服务:
软件开发,电脑配件销售,WIFI路由器销售,上门电脑维修,上门安装系统,系统安装,软、硬件安装,电脑除尘清灰,显示器维修,WIFI安装调试,服务器维护,数据恢复,密码破解,网络布线,网络检修,打印机维修,打印机加碳粉,苹果电脑安装系统,苹果电脑安装双系统,监控安装维护,电脑外包,笔记本电脑维修,餐饮、美容行业软件安装 等。。。。。。
点击次数:1452 更新时间:2018/2/18 16:36:02  【打印此页

上一条:mouse_event

下一条:HeapLock

关键词推荐:郴州电脑城 郴州电脑维修公司 维修电脑公司 郴州软件开发 上门电脑维修 上门安装系统 笔记本电脑维修 郴州打印机维修 打印机加碳粉 电脑安装双系统 苹果电脑双系统 液晶显示器维修 联想笔记本维修 联想笔记本维修电话 戴尔笔记本维修电话 郴州戴尔笔记本维修 戴尔笔记本郴州维修点 华硕笔记本维修点 郴州华硕笔记本维修 郴州笔记本上网维修