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

HeapAlloc

関数
ヒープからメモリブロックを割り当てる。
DLLKERNEL32.dll呼出規約winapi対応OSWindows XP 以降

シグネチャ

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

void* HeapAlloc(
    HANDLE hHeap,
    HEAP_FLAGS dwFlags,
    UINT_PTR dwBytes
);

パラメーター

名前方向
hHeapHANDLEin
dwFlagsHEAP_FLAGSin
dwBytesUINT_PTRin

戻り値の型: void*

各言語での呼び出し定義

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

void* HeapAlloc(
    HANDLE hHeap,
    HEAP_FLAGS dwFlags,
    UINT_PTR dwBytes
);
[DllImport("KERNEL32.dll", ExactSpelling = true)]
static extern IntPtr HeapAlloc(
    IntPtr hHeap,   // HANDLE
    uint dwFlags,   // HEAP_FLAGS
    UIntPtr dwBytes   // UINT_PTR
);
<DllImport("KERNEL32.dll", ExactSpelling:=True)>
Public Shared Function HeapAlloc(
    hHeap As IntPtr,   ' HANDLE
    dwFlags As UInteger,   ' HEAP_FLAGS
    dwBytes As UIntPtr   ' UINT_PTR
) As IntPtr
End Function
' hHeap : HANDLE
' dwFlags : HEAP_FLAGS
' dwBytes : UINT_PTR
Declare PtrSafe Function HeapAlloc Lib "kernel32" ( _
    ByVal hHeap As LongPtr, _
    ByVal dwFlags As Long, _
    ByVal dwBytes As LongPtr) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

HeapAlloc = ctypes.windll.kernel32.HeapAlloc
HeapAlloc.restype = ctypes.c_void_p
HeapAlloc.argtypes = [
    wintypes.HANDLE,  # hHeap : HANDLE
    wintypes.DWORD,  # dwFlags : HEAP_FLAGS
    ctypes.c_size_t,  # dwBytes : UINT_PTR
]
require 'fiddle'
require 'fiddle/import'

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

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procHeapAlloc = kernel32.NewProc("HeapAlloc")
)

// hHeap (HANDLE), dwFlags (HEAP_FLAGS), dwBytes (UINT_PTR)
r1, _, err := procHeapAlloc.Call(
	uintptr(hHeap),
	uintptr(dwFlags),
	uintptr(dwBytes),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // void*
function HeapAlloc(
  hHeap: THandle;   // HANDLE
  dwFlags: DWORD;   // HEAP_FLAGS
  dwBytes: NativeUInt   // UINT_PTR
): Pointer; stdcall;
  external 'KERNEL32.dll' name 'HeapAlloc';
result := DllCall("KERNEL32\HeapAlloc"
    , "Ptr", hHeap   ; HANDLE
    , "UInt", dwFlags   ; HEAP_FLAGS
    , "UPtr", dwBytes   ; UINT_PTR
    , "Ptr")   ; return: void*
●HeapAlloc(hHeap, dwFlags, dwBytes) = DLL("KERNEL32.dll", "void* HeapAlloc(void*, dword, int)")
# 呼び出し: HeapAlloc(hHeap, dwFlags, dwBytes)
# hHeap : HANDLE -> "void*"
# dwFlags : HEAP_FLAGS -> "dword"
# dwBytes : UINT_PTR -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。