Win32 API 日本語リファレンス
ホームSystem.Memory › HeapSetInformation

HeapSetInformation

関数
ヒープの動作に関する情報を設定する。
DLLKERNEL32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

// KERNEL32.dll
#include <windows.h>

BOOL HeapSetInformation(
    HANDLE HeapHandle,   // optional
    HEAP_INFORMATION_CLASS HeapInformationClass,
    void* HeapInformation,   // optional
    UINT_PTR HeapInformationLength
);

パラメーター

名前方向説明
HeapHandleHANDLEinoptional情報を設定する対象のヒープへのハンドル。このハンドルは HeapCreate 関数または GetProcessHeap 関数によって返されます。
HeapInformationClassHEAP_INFORMATION_CLASSin

設定する情報のクラス。このパラメーターには、 HEAP_INFORMATION_CLASS 列挙型の次の値のいずれかを指定できます。

意味
HeapCompatibilityInformation
0
ヒープ機能を有効にします。サポートされるのは ローフラグメンテーションヒープ (LFH) のみです。 ただし、システムはメモリ割り当て要求を処理するために必要に応じて LFH を使用するため、 アプリケーションが LFH を有効にする必要はありません。

Windows XP および Windows Server 2003: LFH は既定では有効になっていません。指定したヒープで LFH を有効にするには、 HeapInformation パラメーターが指す変数を 2 に設定します。ヒープで LFH を有効にした後は、 無効にすることはできません。

LFH は、HEAP_NO_SERIALIZE で作成されたヒープや固定サイズで作成されたヒープでは有効にできません。 また、 Debugging Tools for WindowsMicrosoft Application Verifier のヒープデバッグツールを使用している場合も、LFH を有効にできません。

プロセスが任意のデバッガーの下で実行されると、そのプロセス内のすべてのヒープに対して特定のヒープデバッグオプションが自動的に有効になります。 これらのヒープデバッグオプションは LFH の使用を妨げます。デバッガーの下で実行する際にローフラグメンテーションヒープを有効にするには、 _NO_DEBUG_HEAP 環境変数を 1 に設定します。

HeapEnableTerminationOnCorruption
1
破損時終了 (terminate-on-corruption) 機能を有効にします。ヒープマネージャーがプロセスで使用されているいずれかのヒープでエラーを検出すると、 Windows エラー報告サービスを呼び出してプロセスを終了します。

プロセスがこの機能を有効にした後は、無効にすることはできません。

Windows Server 2003 および Windows XP: この値は Windows Vista および Windows XP with SP3 までサポートされません。 関数は成功しますが、HeapEnableTerminationOnCorruption 値は 無視されます。

HeapOptimizeResources
3
HeapSetInformation が HeapHandle を NULL に設定して呼び出された場合、ローフラグメンテーションヒープ (LFH) を持つプロセス内のすべてのヒープのキャッシュが最適化され、可能であればメモリのコミットが解除されます。

HeapHandle にヒープポインターが指定された場合は、そのヒープのみが最適化されます。

HeapInformation に渡される HEAP_OPTIMIZE_RESOURCES_INFORMATION 構造体は適切に初期化されている必要があることに注意してください。

注意 この値は Windows 8.1 で追加されました。

HeapInformationvoid*inoptional

ヒープ情報バッファー。このデータの形式は HeapInformationClass パラメーターの値によって異なります。

HeapInformationClass パラメーターが HeapCompatibilityInformation の場合、HeapInformation パラメーターは ULONG 変数へのポインターです。

HeapInformationClass パラメーターが HeapEnableTerminationOnCorruption の場合、HeapInformation パラメーターは NULL にし、HeapInformationLength は 0 にする必要があります。

HeapInformationLengthUINT_PTRinHeapInformation バッファーのサイズ (バイト単位)。

戻り値の型: BOOL

公式ドキュメント

指定されたヒープの機能を有効にします。

戻り値

関数が成功すると、戻り値は 0 以外になります。

関数が失敗すると、戻り値は 0 (ゼロ) になります。拡張エラー情報を取得するには、 GetLastError を呼び出します。

解説(Remarks)

ヒープの現在の設定を取得するには、 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;
}
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// KERNEL32.dll
#include <windows.h>

BOOL HeapSetInformation(
    HANDLE HeapHandle,   // optional
    HEAP_INFORMATION_CLASS HeapInformationClass,
    void* HeapInformation,   // optional
    UINT_PTR HeapInformationLength
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool HeapSetInformation(
    IntPtr HeapHandle,   // HANDLE optional
    int HeapInformationClass,   // HEAP_INFORMATION_CLASS
    IntPtr HeapInformation,   // void* optional
    UIntPtr HeapInformationLength   // UINT_PTR
);
<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function HeapSetInformation(
    HeapHandle As IntPtr,   ' HANDLE optional
    HeapInformationClass As Integer,   ' HEAP_INFORMATION_CLASS
    HeapInformation As IntPtr,   ' void* optional
    HeapInformationLength As UIntPtr   ' UINT_PTR
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' HeapHandle : HANDLE optional
' HeapInformationClass : HEAP_INFORMATION_CLASS
' HeapInformation : void* optional
' HeapInformationLength : UINT_PTR
Declare PtrSafe Function HeapSetInformation Lib "kernel32" ( _
    ByVal HeapHandle As LongPtr, _
    ByVal HeapInformationClass As Long, _
    ByVal HeapInformation As LongPtr, _
    ByVal HeapInformationLength As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

HeapSetInformation = ctypes.windll.kernel32.HeapSetInformation
HeapSetInformation.restype = wintypes.BOOL
HeapSetInformation.argtypes = [
    wintypes.HANDLE,  # HeapHandle : HANDLE optional
    ctypes.c_int,  # HeapInformationClass : HEAP_INFORMATION_CLASS
    ctypes.POINTER(None),  # HeapInformation : void* optional
    ctypes.c_size_t,  # HeapInformationLength : UINT_PTR
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
HeapSetInformation = Fiddle::Function.new(
  lib['HeapSetInformation'],
  [
    Fiddle::TYPE_VOIDP,  # HeapHandle : HANDLE optional
    Fiddle::TYPE_INT,  # HeapInformationClass : HEAP_INFORMATION_CLASS
    Fiddle::TYPE_VOIDP,  # HeapInformation : void* optional
    Fiddle::TYPE_UINTPTR_T,  # HeapInformationLength : UINT_PTR
  ],
  Fiddle::TYPE_INT)
#[link(name = "kernel32")]
extern "system" {
    fn HeapSetInformation(
        HeapHandle: *mut core::ffi::c_void,  // HANDLE optional
        HeapInformationClass: i32,  // HEAP_INFORMATION_CLASS
        HeapInformation: *mut (),  // void* optional
        HeapInformationLength: usize  // UINT_PTR
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", SetLastError = true)]
public static extern bool HeapSetInformation(IntPtr HeapHandle, int HeapInformationClass, IntPtr HeapInformation, UIntPtr HeapInformationLength);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_HeapSetInformation' -Namespace Win32 -PassThru
# $api::HeapSetInformation(HeapHandle, HeapInformationClass, HeapInformation, HeapInformationLength)
#uselib "KERNEL32.dll"
#func global HeapSetInformation "HeapSetInformation" sptr, sptr, sptr, sptr
; HeapSetInformation HeapHandle, HeapInformationClass, HeapInformation, HeapInformationLength   ; 戻り値は stat
; HeapHandle : HANDLE optional -> "sptr"
; HeapInformationClass : HEAP_INFORMATION_CLASS -> "sptr"
; HeapInformation : void* optional -> "sptr"
; HeapInformationLength : UINT_PTR -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "KERNEL32.dll"
#cfunc global HeapSetInformation "HeapSetInformation" sptr, int, sptr, sptr
; res = HeapSetInformation(HeapHandle, HeapInformationClass, HeapInformation, HeapInformationLength)
; HeapHandle : HANDLE optional -> "sptr"
; HeapInformationClass : HEAP_INFORMATION_CLASS -> "int"
; HeapInformation : void* optional -> "sptr"
; HeapInformationLength : UINT_PTR -> "sptr"
; BOOL HeapSetInformation(HANDLE HeapHandle, HEAP_INFORMATION_CLASS HeapInformationClass, void* HeapInformation, UINT_PTR HeapInformationLength)
#uselib "KERNEL32.dll"
#cfunc global HeapSetInformation "HeapSetInformation" intptr, int, intptr, intptr
; res = HeapSetInformation(HeapHandle, HeapInformationClass, HeapInformation, HeapInformationLength)
; HeapHandle : HANDLE optional -> "intptr"
; HeapInformationClass : HEAP_INFORMATION_CLASS -> "int"
; HeapInformation : void* optional -> "intptr"
; HeapInformationLength : UINT_PTR -> "intptr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procHeapSetInformation = kernel32.NewProc("HeapSetInformation")
)

// HeapHandle (HANDLE optional), HeapInformationClass (HEAP_INFORMATION_CLASS), HeapInformation (void* optional), HeapInformationLength (UINT_PTR)
r1, _, err := procHeapSetInformation.Call(
	uintptr(HeapHandle),
	uintptr(HeapInformationClass),
	uintptr(HeapInformation),
	uintptr(HeapInformationLength),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function HeapSetInformation(
  HeapHandle: THandle;   // HANDLE optional
  HeapInformationClass: Integer;   // HEAP_INFORMATION_CLASS
  HeapInformation: Pointer;   // void* optional
  HeapInformationLength: NativeUInt   // UINT_PTR
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'HeapSetInformation';
result := DllCall("KERNEL32\HeapSetInformation"
    , "Ptr", HeapHandle   ; HANDLE optional
    , "Int", HeapInformationClass   ; HEAP_INFORMATION_CLASS
    , "Ptr", HeapInformation   ; void* optional
    , "UPtr", HeapInformationLength   ; UINT_PTR
    , "Int")   ; return: BOOL
●HeapSetInformation(HeapHandle, HeapInformationClass, HeapInformation, HeapInformationLength) = DLL("KERNEL32.dll", "bool HeapSetInformation(void*, int, void*, int)")
# 呼び出し: HeapSetInformation(HeapHandle, HeapInformationClass, HeapInformation, HeapInformationLength)
# HeapHandle : HANDLE optional -> "void*"
# HeapInformationClass : HEAP_INFORMATION_CLASS -> "int"
# HeapInformation : void* optional -> "void*"
# HeapInformationLength : UINT_PTR -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "kernel32" fn HeapSetInformation(
    HeapHandle: ?*anyopaque, // HANDLE optional
    HeapInformationClass: i32, // HEAP_INFORMATION_CLASS
    HeapInformation: ?*anyopaque, // void* optional
    HeapInformationLength: usize // UINT_PTR
) callconv(std.os.windows.WINAPI) i32;
proc HeapSetInformation(
    HeapHandle: pointer,  # HANDLE optional
    HeapInformationClass: int32,  # HEAP_INFORMATION_CLASS
    HeapInformation: pointer,  # void* optional
    HeapInformationLength: uint  # UINT_PTR
): int32 {.importc: "HeapSetInformation", stdcall, dynlib: "KERNEL32.dll".}
pragma(lib, "kernel32");
extern(Windows)
int HeapSetInformation(
    void* HeapHandle,   // HANDLE optional
    int HeapInformationClass,   // HEAP_INFORMATION_CLASS
    void* HeapInformation,   // void* optional
    size_t HeapInformationLength   // UINT_PTR
);
ccall((:HeapSetInformation, "KERNEL32.dll"), stdcall, Int32,
      (Ptr{Cvoid}, Int32, Ptr{Cvoid}, Csize_t),
      HeapHandle, HeapInformationClass, HeapInformation, HeapInformationLength)
# HeapHandle : HANDLE optional -> Ptr{Cvoid}
# HeapInformationClass : HEAP_INFORMATION_CLASS -> Int32
# HeapInformation : void* optional -> Ptr{Cvoid}
# HeapInformationLength : UINT_PTR -> Csize_t
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t HeapSetInformation(
    void* HeapHandle,
    int32_t HeapInformationClass,
    void* HeapInformation,
    uintptr_t HeapInformationLength);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.HeapSetInformation(HeapHandle, HeapInformationClass, HeapInformation, HeapInformationLength)
-- HeapHandle : HANDLE optional
-- HeapInformationClass : HEAP_INFORMATION_CLASS
-- HeapInformation : void* optional
-- HeapInformationLength : UINT_PTR
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const HeapSetInformation = lib.func('__stdcall', 'HeapSetInformation', 'int32_t', ['void *', 'int32_t', 'void *', 'uintptr_t']);
// HeapSetInformation(HeapHandle, HeapInformationClass, HeapInformation, HeapInformationLength)
// HeapHandle : HANDLE optional -> 'void *'
// HeapInformationClass : HEAP_INFORMATION_CLASS -> 'int32_t'
// HeapInformation : void* optional -> 'void *'
// HeapInformationLength : UINT_PTR -> 'uintptr_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("KERNEL32.dll", {
  HeapSetInformation: { parameters: ["pointer", "i32", "pointer", "usize"], result: "i32" },
});
// lib.symbols.HeapSetInformation(HeapHandle, HeapInformationClass, HeapInformation, HeapInformationLength)
// HeapHandle : HANDLE optional -> "pointer"
// HeapInformationClass : HEAP_INFORMATION_CLASS -> "i32"
// HeapInformation : void* optional -> "pointer"
// HeapInformationLength : UINT_PTR -> "usize"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t HeapSetInformation(
    void* HeapHandle,
    int32_t HeapInformationClass,
    void* HeapInformation,
    size_t HeapInformationLength);
C, "KERNEL32.dll");
// $ffi->HeapSetInformation(HeapHandle, HeapInformationClass, HeapInformation, HeapInformationLength);
// HeapHandle : HANDLE optional
// HeapInformationClass : HEAP_INFORMATION_CLASS
// HeapInformation : void* optional
// HeapInformationLength : UINT_PTR
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。
import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

public interface Kernel32 extends StdCallLibrary {
    Kernel32 INSTANCE = Native.load("kernel32", Kernel32.class);
    boolean HeapSetInformation(
        Pointer HeapHandle,   // HANDLE optional
        int HeapInformationClass,   // HEAP_INFORMATION_CLASS
        Pointer HeapInformation,   // void* optional
        long HeapInformationLength   // UINT_PTR
    );
}
@[Link("kernel32")]
lib LibKERNEL32
  fun HeapSetInformation = HeapSetInformation(
    HeapHandle : Void*,   # HANDLE optional
    HeapInformationClass : Int32,   # HEAP_INFORMATION_CLASS
    HeapInformation : Void*,   # void* optional
    HeapInformationLength : LibC::SizeT   # UINT_PTR
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef HeapSetInformationNative = Int32 Function(Pointer<Void>, Int32, Pointer<Void>, UintPtr);
typedef HeapSetInformationDart = int Function(Pointer<Void>, int, Pointer<Void>, int);
final HeapSetInformation = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<HeapSetInformationNative, HeapSetInformationDart>('HeapSetInformation');
// HeapHandle : HANDLE optional -> Pointer<Void>
// HeapInformationClass : HEAP_INFORMATION_CLASS -> Int32
// HeapInformation : void* optional -> Pointer<Void>
// HeapInformationLength : UINT_PTR -> UintPtr
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function HeapSetInformation(
  HeapHandle: THandle;   // HANDLE optional
  HeapInformationClass: Integer;   // HEAP_INFORMATION_CLASS
  HeapInformation: Pointer;   // void* optional
  HeapInformationLength: NativeUInt   // UINT_PTR
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'HeapSetInformation';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "HeapSetInformation"
  c_HeapSetInformation :: Ptr () -> Int32 -> Ptr () -> CUIntPtr -> IO CInt
-- HeapHandle : HANDLE optional -> Ptr ()
-- HeapInformationClass : HEAP_INFORMATION_CLASS -> Int32
-- HeapInformation : void* optional -> Ptr ()
-- HeapInformationLength : UINT_PTR -> CUIntPtr
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let heapsetinformation =
  foreign "HeapSetInformation"
    ((ptr void) @-> int32_t @-> (ptr void) @-> size_t @-> returning int32_t)
(* HeapHandle : HANDLE optional -> (ptr void) *)
(* HeapInformationClass : HEAP_INFORMATION_CLASS -> int32_t *)
(* HeapInformation : void* optional -> (ptr void) *)
(* HeapInformationLength : UINT_PTR -> size_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)

(cffi:defcfun ("HeapSetInformation" heap-set-information :convention :stdcall) :int32
  (heap-handle :pointer)   ; HANDLE optional
  (heap-information-class :int32)   ; HEAP_INFORMATION_CLASS
  (heap-information :pointer)   ; void* optional
  (heap-information-length :uint64))   ; UINT_PTR
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $HeapSetInformation = Win32::API::More->new('KERNEL32',
    'BOOL HeapSetInformation(HANDLE HeapHandle, int HeapInformationClass, LPVOID HeapInformation, WPARAM HeapInformationLength)');
# my $ret = $HeapSetInformation->Call($HeapHandle, $HeapInformationClass, $HeapInformation, $HeapInformationLength);
# HeapHandle : HANDLE optional -> HANDLE
# HeapInformationClass : HEAP_INFORMATION_CLASS -> int
# HeapInformation : void* optional -> LPVOID
# HeapInformationLength : UINT_PTR -> WPARAM
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目
使用する型