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

VirtualFreeEx

関数
指定プロセスの仮想メモリ領域を解放または解放予約する。
DLLKERNEL32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL VirtualFreeEx(
    HANDLE hProcess,
    void* lpAddress,
    UINT_PTR dwSize,
    VIRTUAL_FREE_TYPE dwFreeType
);

パラメーター

名前方向
hProcessHANDLEin
lpAddressvoid*inout
dwSizeUINT_PTRin
dwFreeTypeVIRTUAL_FREE_TYPEin

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL VirtualFreeEx(
    HANDLE hProcess,
    void* lpAddress,
    UINT_PTR dwSize,
    VIRTUAL_FREE_TYPE dwFreeType
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool VirtualFreeEx(
    IntPtr hProcess,   // HANDLE
    IntPtr lpAddress,   // void* in/out
    UIntPtr dwSize,   // UINT_PTR
    uint dwFreeType   // VIRTUAL_FREE_TYPE
);
<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function VirtualFreeEx(
    hProcess As IntPtr,   ' HANDLE
    lpAddress As IntPtr,   ' void* in/out
    dwSize As UIntPtr,   ' UINT_PTR
    dwFreeType As UInteger   ' VIRTUAL_FREE_TYPE
) As Boolean
End Function
' hProcess : HANDLE
' lpAddress : void* in/out
' dwSize : UINT_PTR
' dwFreeType : VIRTUAL_FREE_TYPE
Declare PtrSafe Function VirtualFreeEx Lib "kernel32" ( _
    ByVal hProcess As LongPtr, _
    ByVal lpAddress As LongPtr, _
    ByVal dwSize As LongPtr, _
    ByVal dwFreeType As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

VirtualFreeEx = ctypes.windll.kernel32.VirtualFreeEx
VirtualFreeEx.restype = wintypes.BOOL
VirtualFreeEx.argtypes = [
    wintypes.HANDLE,  # hProcess : HANDLE
    ctypes.POINTER(None),  # lpAddress : void* in/out
    ctypes.c_size_t,  # dwSize : UINT_PTR
    wintypes.DWORD,  # dwFreeType : VIRTUAL_FREE_TYPE
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procVirtualFreeEx = kernel32.NewProc("VirtualFreeEx")
)

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