HeapSetInformation 함수(heapapi.h)

지정된 힙에 대한 기능을 사용하도록 설정합니다.

구문

BOOL HeapSetInformation(
  [in, optional] HANDLE                 HeapHandle,
  [in]           HEAP_INFORMATION_CLASS HeapInformationClass,
  [in]           PVOID                  HeapInformation,
  [in]           SIZE_T                 HeapInformationLength
);

매개 변수

[in, optional] HeapHandle

정보를 설정할 힙에 대한 핸들입니다. 이 핸들은 HeapCreate 또는 GetProcessHeap 함수에서 반환됩니다.

[in] HeapInformationClass

설정할 정보의 클래스입니다. 이 매개 변수는 HEAP_INFORMATION_CLASS 열거형 형식의 다음 값 중 하나일 수 있습니다.

의미
HeapCompatibilityInformation
0
힙 기능을 사용하도록 설정합니다. LFH( 하위 조각화 힙 )만 지원됩니다. 그러나 시스템에서 메모리 할당 요청을 서비스하는 데 필요에 따라 LFH를 사용하므로 애플리케이션에서 LFH를 사용하도록 설정할 필요는 없습니다.

Windows XP 및 Windows Server 2003: LFH는 기본적으로 사용하도록 설정되지 않습니다. 지정된 힙에 대해 LFH를 사용하도록 설정하려면 HeapInformation 매개 변수가 가리키는 변수를 2로 설정합니다. 힙에 대해 LFH를 사용하도록 설정한 후에는 사용하지 않도록 설정할 수 없습니다.

LFH는 HEAP_NO_SERIALIZE 사용하여 만든 힙 또는 고정 크기로 만든 힙에 대해 사용하도록 설정할 수 없습니다. Windows용 디버깅 도구 또는 Microsoft 애플리케이션 검증 도구에서 힙 디버깅 도구를 사용하는 경우에도 LFH를 사용하도록 설정할 수 없습니다.

모든 디버거에서 프로세스가 실행되면 프로세스의 모든 힙에 대해 특정 힙 디버그 옵션이 자동으로 사용하도록 설정됩니다. 이러한 힙 디버그 옵션은 LFH를 사용하지 않습니다. 디버거에서 실행할 때 하위 조각화 힙을 사용하도록 설정하려면 _NO_DEBUG_HEAP 환경 변수를 1로 설정합니다.

HeapEnableTerminationOnCorruption
1
손상 시 종료 기능을 사용하도록 설정합니다. 힙 관리자가 프로세스에서 사용하는 모든 힙에서 오류를 감지하면 Windows 오류 보고 서비스를 호출하고 프로세스를 종료합니다.

프로세스에서 이 기능을 사용하도록 설정한 후에는 사용하지 않도록 설정할 수 없습니다.

Windows Server 2003 및 Windows XP: 이 값은 Windows Vista 및 Windows XP SP3까지 지원되지 않습니다. 함수는 성공하지만 HeapEnableTerminationOnCorruption 값은 무시됩니다.

HeapOptimizeResources
3
HeapHandle을 NULL로 설정하여 HeapSetInformation을 호출하는 경우 LFH(하위 조각화 힙)가 있는 프로세스의 모든 힙은 캐시가 최적화되고 가능하면 메모리가 커밋 해제됩니다.

HeapHandle에서 힙 포인터를 제공하는 경우 해당 힙만 최적화됩니다.

HeapInformation에 전달된 HEAP_OPTIMIZE_RESOURCES_INFORMATION 구조체를 올바르게 초기화해야 합니다.

참고 이 값은 Windows 8.1 추가되었습니다.

[in] HeapInformation

힙 정보 버퍼입니다. 이 데이터의 형식은 HeapInformationClass 매개 변수의 값에 따라 달라집니다.

HeapInformationClass 매개 변수가 HeapCompatibilityInformation인 경우 HeapInformation 매개 변수는 ULONG 변수에 대한 포인터입니다.

HeapInformationClass 매개 변수가 HeapEnableTerminationOnCorruption이면 HeapInformation 매개 변수는 NULL이고 HeapInformationLength는 0이어야 합니다.

[in] HeapInformationLength

HeapInformation 버퍼의 크기(바이트)입니다.

반환 값

함수가 성공하면 반환 값이 0이 아닙니다.

함수가 실패하면 반환 값은 0입니다. 확장 오류 정보를 가져오려면 GetLastError를 호출합니다.

설명

힙에 대한 현재 설정을 검색하려면 HeapQueryInformation 함수를 사용합니다.

HeapEnableTerminateOnCorruption 옵션을 설정하면 손상된 힙을 활용하는 보안 익스플로잇에 대한 애플리케이션의 노출이 줄어들기 때문에 강력히 권장됩니다.

예제

다음 예제에서는 하위 조각화 힙을 사용하도록 설정하는 방법을 보여 줍니다.

#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-fragmentation 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;
}

요구 사항

요구 사항
지원되는 최소 클라이언트 Windows XP [데스크톱 앱 | UWP 앱]
지원되는 최소 서버 Windows Server 2003 [데스크톱 앱 | UWP 앱]
대상 플랫폼 Windows
헤더 heapapi.h(Windows.h 포함)
라이브러리 Kernel32.lib
DLL Kernel32.dll

참고 항목

GetProcessHeap

힙 함수

HeapCreate

HeapQueryInformation

메모리 관리 함수