VirtualFreeEx
関数シグネチャ
// KERNEL32.dll
#include <windows.h>
BOOL VirtualFreeEx(
HANDLE hProcess,
void* lpAddress,
UINT_PTR dwSize,
VIRTUAL_FREE_TYPE dwFreeType
);パラメーター
| 名前 | 型 | 方向 | 説明 | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| hProcess | HANDLE | in | プロセスへのハンドルです。この関数は、そのプロセスの仮想アドレス空間内でメモリを解放します。 このハンドルには PROCESS_VM_OPERATION アクセス権が必要です。詳細については、Process Security and Access Rights を参照してください。 | ||||||||||||
| lpAddress | void* | inout | 解放するメモリ領域の開始アドレスへのポインターです。 dwFreeType パラメーターが MEM_RELEASE の場合、lpAddress は、領域を予約したときに VirtualAllocEx 関数が返したベースアドレスでなければなりません。 | ||||||||||||
| dwSize | UINT_PTR | in | 解放するメモリ領域のサイズ(バイト単位)です。 dwFreeType パラメーターが MEM_RELEASE の場合、dwSize は 0(ゼロ)でなければなりません。この関数は、最初の割り当て呼び出しで VirtualAllocEx によって予約された領域全体を解放します。 dwFreeType が MEM_DECOMMIT の場合、この関数は、lpAddress パラメーターから | ||||||||||||
| dwFreeType | VIRTUAL_FREE_TYPE | in | 解放操作の種類です。このパラメーターには、次のいずれかの値を指定する必要があります。
MEM_RELEASE を使用する場合、このパラメーターには次のいずれかの値を追加で指定できます。
|
戻り値の型: BOOL
公式ドキュメント
指定したプロセスの仮想アドレス空間内にあるメモリ領域を、解放(release)、デコミット(decommit)、または解放とデコミットの両方を行います。
戻り値
関数が成功した場合、戻り値は 0 以外の値になります。
関数が失敗した場合、戻り値は 0(ゼロ)になります。拡張エラー情報を取得するには、 GetLastError を呼び出してください。
解説(Remarks)
プロセスの仮想アドレス空間内の各メモリページには、 Page State があります。 VirtualFreeEx 関数は、一部がコミット済みで一部が未コミットというように、異なる状態のページの範囲をデコミットできます。つまり、各ページの現在のコミット状態を事前に判定せずに、ページの範囲をデコミットできます。ページをデコミットすると、メモリ内またはディスク上のページングファイル内にあるその物理ストレージが解放されます。
ページがデコミットされても解放されていない場合、その状態は予約済みに変わります。その後、 VirtualAllocEx を呼び出してコミットするか、 VirtualFreeEx を呼び出して解放できます。予約済みページに対して読み取りや書き込みを試みると、アクセス違反例外が発生します。
VirtualFreeEx 関数は、一部が予約済みで一部がコミット済みというように、異なる状態のページの範囲を解放できます。つまり、各ページの現在のコミット状態を事前に判定せずに、ページの範囲を解放できます。VirtualAllocEx によって最初に予約されたページの範囲全体を、同時に解放する必要があります。
ページが解放されると、その状態は空き(free)に変わり、以降の割り当て操作で利用できるようになります。メモリが解放またはデコミットされた後は、そのメモリを二度と参照できません。そのメモリに格納されていた可能性のある情報は永久に失われます。空きページに対して読み取りや書き込みを試みると、アクセス違反例外が発生します。情報を保持する必要がある場合は、その情報を含むメモリをデコミットしたり解放したりしないでください。
VirtualFreeEx 関数は AWE メモリ領域に対して使用でき、アドレス空間を解放する際に、その領域内のすべての物理ページマッピングを無効化します。ただし、物理ページ自体は削除されず、アプリケーションはそれらを使用できます。物理ページを解放するには、アプリケーションが明示的に FreeUserPhysicalPages を呼び出す必要があります。プロセスが終了すると、すべてのリソースは自動的にクリーンアップされます。
Windows 10 バージョン 1709 以降および Windows 11: エンクレーブの使用が終わった後に削除するには、DeleteEnclave を呼び出します。VBS エンクレーブは、VirtualFree 関数や VirtualFreeEx 関数を呼び出しても削除できません。SGX エンクレーブは、VirtualFree または VirtualFreeEx を呼び出すことで引き続き削除できます。
Windows 10 バージョン 1507、Windows 10 バージョン 1511、Windows 10 バージョン 1607、および Windows 10 バージョン 1703: エンクレーブの使用が終わった後に削除するには、VirtualFree 関数または VirtualFreeEx 関数を呼び出し、次の値を指定します。
- lpAddress パラメーターにエンクレーブのベースアドレス。
- dwSize パラメーターに 0。
- dwFreeType パラメーターに MEM_RELEASE。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// 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 <MarshalAs(UnmanagedType.Bool)> 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 // BOOLfunction 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)。const std = @import("std");
extern "kernel32" fn VirtualFreeEx(
hProcess: ?*anyopaque, // HANDLE
lpAddress: ?*anyopaque, // void* in/out
dwSize: usize, // UINT_PTR
dwFreeType: u32 // VIRTUAL_FREE_TYPE
) callconv(std.os.windows.WINAPI) i32;proc VirtualFreeEx(
hProcess: pointer, # HANDLE
lpAddress: pointer, # void* in/out
dwSize: uint, # UINT_PTR
dwFreeType: uint32 # VIRTUAL_FREE_TYPE
): int32 {.importc: "VirtualFreeEx", stdcall, dynlib: "KERNEL32.dll".}pragma(lib, "kernel32");
extern(Windows)
int VirtualFreeEx(
void* hProcess, // HANDLE
void* lpAddress, // void* in/out
size_t dwSize, // UINT_PTR
uint dwFreeType // VIRTUAL_FREE_TYPE
);ccall((:VirtualFreeEx, "KERNEL32.dll"), stdcall, Int32,
(Ptr{Cvoid}, Ptr{Cvoid}, Csize_t, UInt32),
hProcess, lpAddress, dwSize, dwFreeType)
# hProcess : HANDLE -> Ptr{Cvoid}
# lpAddress : void* in/out -> Ptr{Cvoid}
# dwSize : UINT_PTR -> Csize_t
# dwFreeType : VIRTUAL_FREE_TYPE -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t VirtualFreeEx(
void* hProcess,
void* lpAddress,
uintptr_t dwSize,
uint32_t dwFreeType);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.VirtualFreeEx(hProcess, lpAddress, dwSize, dwFreeType)
-- hProcess : HANDLE
-- lpAddress : void* in/out
-- dwSize : UINT_PTR
-- dwFreeType : VIRTUAL_FREE_TYPE
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const VirtualFreeEx = lib.func('__stdcall', 'VirtualFreeEx', 'int32_t', ['void *', 'void *', 'uintptr_t', 'uint32_t']);
// VirtualFreeEx(hProcess, lpAddress, dwSize, dwFreeType)
// hProcess : HANDLE -> 'void *'
// lpAddress : void* in/out -> 'void *'
// dwSize : UINT_PTR -> 'uintptr_t'
// dwFreeType : VIRTUAL_FREE_TYPE -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("KERNEL32.dll", {
VirtualFreeEx: { parameters: ["pointer", "pointer", "usize", "u32"], result: "i32" },
});
// lib.symbols.VirtualFreeEx(hProcess, lpAddress, dwSize, dwFreeType)
// hProcess : HANDLE -> "pointer"
// lpAddress : void* in/out -> "pointer"
// dwSize : UINT_PTR -> "usize"
// dwFreeType : VIRTUAL_FREE_TYPE -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t VirtualFreeEx(
void* hProcess,
void* lpAddress,
size_t dwSize,
uint32_t dwFreeType);
C, "KERNEL32.dll");
// $ffi->VirtualFreeEx(hProcess, lpAddress, dwSize, dwFreeType);
// hProcess : HANDLE
// lpAddress : void* in/out
// dwSize : UINT_PTR
// dwFreeType : VIRTUAL_FREE_TYPE
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;
public interface Kernel32 extends StdCallLibrary {
Kernel32 INSTANCE = Native.load("kernel32", Kernel32.class);
boolean VirtualFreeEx(
Pointer hProcess, // HANDLE
Pointer lpAddress, // void* in/out
long dwSize, // UINT_PTR
int dwFreeType // VIRTUAL_FREE_TYPE
);
}@[Link("kernel32")]
lib LibKERNEL32
fun VirtualFreeEx = VirtualFreeEx(
hProcess : Void*, # HANDLE
lpAddress : Void*, # void* in/out
dwSize : LibC::SizeT, # UINT_PTR
dwFreeType : UInt32 # VIRTUAL_FREE_TYPE
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef VirtualFreeExNative = Int32 Function(Pointer<Void>, Pointer<Void>, UintPtr, Uint32);
typedef VirtualFreeExDart = int Function(Pointer<Void>, Pointer<Void>, int, int);
final VirtualFreeEx = DynamicLibrary.open('KERNEL32.dll')
.lookupFunction<VirtualFreeExNative, VirtualFreeExDart>('VirtualFreeEx');
// hProcess : HANDLE -> Pointer<Void>
// lpAddress : void* in/out -> Pointer<Void>
// dwSize : UINT_PTR -> UintPtr
// dwFreeType : VIRTUAL_FREE_TYPE -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
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';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "VirtualFreeEx"
c_VirtualFreeEx :: Ptr () -> Ptr () -> CUIntPtr -> Word32 -> IO CInt
-- hProcess : HANDLE -> Ptr ()
-- lpAddress : void* in/out -> Ptr ()
-- dwSize : UINT_PTR -> CUIntPtr
-- dwFreeType : VIRTUAL_FREE_TYPE -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let virtualfreeex =
foreign "VirtualFreeEx"
((ptr void) @-> (ptr void) @-> size_t @-> uint32_t @-> returning int32_t)
(* hProcess : HANDLE -> (ptr void) *)
(* lpAddress : void* in/out -> (ptr void) *)
(* dwSize : UINT_PTR -> size_t *)
(* dwFreeType : VIRTUAL_FREE_TYPE -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)
(cffi:defcfun ("VirtualFreeEx" virtual-free-ex :convention :stdcall) :int32
(h-process :pointer) ; HANDLE
(lp-address :pointer) ; void* in/out
(dw-size :uint64) ; UINT_PTR
(dw-free-type :uint32)) ; VIRTUAL_FREE_TYPE
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $VirtualFreeEx = Win32::API::More->new('KERNEL32',
'BOOL VirtualFreeEx(HANDLE hProcess, LPVOID lpAddress, WPARAM dwSize, DWORD dwFreeType)');
# my $ret = $VirtualFreeEx->Call($hProcess, $lpAddress, $dwSize, $dwFreeType);
# hProcess : HANDLE -> HANDLE
# lpAddress : void* in/out -> LPVOID
# dwSize : UINT_PTR -> WPARAM
# dwFreeType : VIRTUAL_FREE_TYPE -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f VirtualFree — 確保した仮想メモリ領域を解放または解除する。