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

GlobalAlloc

関数
グローバルヒープからメモリを割り当てる。
DLLKERNEL32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

HGLOBAL GlobalAlloc(
    GLOBAL_ALLOC_FLAGS uFlags,
    UINT_PTR dwBytes
);

パラメーター

名前方向
uFlagsGLOBAL_ALLOC_FLAGSin
dwBytesUINT_PTRin

戻り値の型: HGLOBAL

各言語での呼び出し定義

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

HGLOBAL GlobalAlloc(
    GLOBAL_ALLOC_FLAGS uFlags,
    UINT_PTR dwBytes
);
[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern IntPtr GlobalAlloc(
    uint uFlags,   // GLOBAL_ALLOC_FLAGS
    UIntPtr dwBytes   // UINT_PTR
);
<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GlobalAlloc(
    uFlags As UInteger,   ' GLOBAL_ALLOC_FLAGS
    dwBytes As UIntPtr   ' UINT_PTR
) As IntPtr
End Function
' uFlags : GLOBAL_ALLOC_FLAGS
' dwBytes : UINT_PTR
Declare PtrSafe Function GlobalAlloc Lib "kernel32" ( _
    ByVal uFlags 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

GlobalAlloc = ctypes.windll.kernel32.GlobalAlloc
GlobalAlloc.restype = ctypes.c_void_p
GlobalAlloc.argtypes = [
    wintypes.DWORD,  # uFlags : GLOBAL_ALLOC_FLAGS
    ctypes.c_size_t,  # dwBytes : UINT_PTR
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procGlobalAlloc = kernel32.NewProc("GlobalAlloc")
)

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