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

LocalReAlloc

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

シグネチャ

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

HLOCAL LocalReAlloc(
    HLOCAL hMem,   // optional
    UINT_PTR uBytes,
    DWORD uFlags
);

パラメーター

名前方向
hMemHLOCALinoptional
uBytesUINT_PTRin
uFlagsDWORDin

戻り値の型: HLOCAL

各言語での呼び出し定義

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

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

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

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procLocalReAlloc = kernel32.NewProc("LocalReAlloc")
)

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