HeapSetInformation
関数シグネチャ
// KERNEL32.dll
#include <windows.h>
BOOL HeapSetInformation(
HANDLE HeapHandle, // optional
HEAP_INFORMATION_CLASS HeapInformationClass,
void* HeapInformation, // optional
UINT_PTR HeapInformationLength
);パラメーター
| 名前 | 型 | 方向 | 説明 | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| HeapHandle | HANDLE | inoptional | 情報を設定する対象のヒープへのハンドル。このハンドルは HeapCreate 関数または GetProcessHeap 関数によって返されます。 | ||||||||
| HeapInformationClass | HEAP_INFORMATION_CLASS | in | 設定する情報のクラス。このパラメーターには、 HEAP_INFORMATION_CLASS 列挙型の次の値のいずれかを指定できます。
| ||||||||
| HeapInformation | void* | inoptional | ヒープ情報バッファー。このデータの形式は HeapInformationClass パラメーターの値によって異なります。 HeapInformationClass パラメーターが HeapCompatibilityInformation の場合、HeapInformation パラメーターは ULONG 変数へのポインターです。 HeapInformationClass パラメーターが HeapEnableTerminationOnCorruption の場合、HeapInformation パラメーターは NULL にし、HeapInformationLength は 0 にする必要があります。 | ||||||||
| HeapInformationLength | UINT_PTR | in | HeapInformation バッファーのサイズ (バイト単位)。 |
戻り値の型: 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 公式リファレンス: 英語 (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 // BOOLfunction 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 を使用。関連項目
- f GetProcessHeap — プロセス既定のヒープハンドルを取得する。
- f HeapCreate — プライベートヒープオブジェクトを作成する。
- f HeapQueryInformation — ヒープの動作に関する情報を取得する。