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

GlobalReAlloc

関数
グローバルメモリオブジェクトのサイズや属性を変更する。
DLLKERNEL32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

HGLOBAL GlobalReAlloc(
    HGLOBAL hMem,
    UINT_PTR dwBytes,
    DWORD uFlags
);

パラメーター

名前方向
hMemHGLOBALin
dwBytesUINT_PTRin
uFlagsDWORDin

戻り値の型: HGLOBAL

各言語での呼び出し定義

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

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

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

lib = Fiddle.dlopen('KERNEL32.dll')
GlobalReAlloc = Fiddle::Function.new(
  lib['GlobalReAlloc'],
  [
    Fiddle::TYPE_VOIDP,  # hMem : HGLOBAL
    Fiddle::TYPE_UINTPTR_T,  # dwBytes : UINT_PTR
    -Fiddle::TYPE_INT,  # uFlags : DWORD
  ],
  Fiddle::TYPE_VOIDP)
#[link(name = "kernel32")]
extern "system" {
    fn GlobalReAlloc(
        hMem: *mut core::ffi::c_void,  // HGLOBAL
        dwBytes: usize,  // UINT_PTR
        uFlags: u32  // DWORD
    ) -> *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 GlobalReAlloc(IntPtr hMem, UIntPtr dwBytes, uint uFlags);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_GlobalReAlloc' -Namespace Win32 -PassThru
# $api::GlobalReAlloc(hMem, dwBytes, uFlags)
#uselib "KERNEL32.dll"
#func global GlobalReAlloc "GlobalReAlloc" sptr, sptr, sptr
; GlobalReAlloc hMem, dwBytes, uFlags   ; 戻り値は stat
; hMem : HGLOBAL -> "sptr"
; dwBytes : UINT_PTR -> "sptr"
; uFlags : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "KERNEL32.dll"
#cfunc global GlobalReAlloc "GlobalReAlloc" sptr, sptr, int
; res = GlobalReAlloc(hMem, dwBytes, uFlags)
; hMem : HGLOBAL -> "sptr"
; dwBytes : UINT_PTR -> "sptr"
; uFlags : DWORD -> "int"
; HGLOBAL GlobalReAlloc(HGLOBAL hMem, UINT_PTR dwBytes, DWORD uFlags)
#uselib "KERNEL32.dll"
#cfunc global GlobalReAlloc "GlobalReAlloc" intptr, intptr, int
; res = GlobalReAlloc(hMem, dwBytes, uFlags)
; hMem : HGLOBAL -> "intptr"
; dwBytes : UINT_PTR -> "intptr"
; uFlags : DWORD -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procGlobalReAlloc = kernel32.NewProc("GlobalReAlloc")
)

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