VirtualAllocFromApp
関数シグネチャ
// api-ms-win-core-memory-l1-1-3.dll
#include <windows.h>
void* VirtualAllocFromApp(
void* BaseAddress, // optional
UINT_PTR Size,
VIRTUAL_ALLOCATION_TYPE AllocationType,
DWORD Protection
);パラメーター
| 名前 | 型 | 方向 | 説明 | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| BaseAddress | void* | inoptional | 割り当てる領域の開始アドレスです。メモリを予約する場合、指定されたアドレスは 割り当て単位 (allocation granularity) の最も近い倍数に切り下げられます。メモリが既に予約済みで コミットされる場合、アドレスは次のページ境界に切り下げられます。ホストコンピューター上のページサイズと 割り当て単位を確認するには、 GetSystemInfo 関数を使用します。このパラメーターが NULL の場合、システムが領域を割り当てる場所を決定します。 | ||||||||||||||||||||
| Size | UINT_PTR | in | 領域のサイズ (バイト単位) です。BaseAddress パラメーターが NULL の場合、この値は次のページ境界に切り上げられます。それ以外の場合、割り当てられる ページには BaseAddress から BaseAddress+Size までの範囲に 1 バイト以上を含むすべてのページが含まれます。つまり、 ページ境界をまたぐ 2 バイトの範囲は、両方のページが割り当て領域に含まれる原因となります。 | ||||||||||||||||||||
| AllocationType | VIRTUAL_ALLOCATION_TYPE | in | メモリ割り当ての種類です。このパラメーターには次の値のいずれかを含める必要があります。
このパラメーターには、以下に示す値を指定することもできます。
| ||||||||||||||||||||
| Protection | DWORD | in | 割り当てるページ領域のメモリ保護です。ページがコミットされる場合、 メモリ保護定数のいずれかを指定できます。次の定数はエラーを生成します。 |
戻り値の型: void*
公式ドキュメント
呼び出し元プロセスの仮想アドレス空間内のページ領域を予約、コミット、または状態を変更します。(VirtualAllocFromApp)
戻り値
関数が成功すると、戻り値は割り当てられたページ領域のベースアドレスです。
関数が失敗すると、戻り値は NULL です。拡張エラー情報を取得するには、 GetLastError を呼び出します。
解説(Remarks)
JIT (Just-In-Time) 機能を持つ Windows ストアアプリから VirtualAllocFromApp を呼び出して JIT 機能を使用できます。JIT 機能を使用するには、アプリのマニフェストファイルに codeGeneration 機能 (capability) を含める必要があります。
各ページには関連付けられたページ状態があります。 VirtualAllocFromApp 関数は次の 操作を実行できます。
- 予約済みページの領域をコミットする
- 空きページの領域を予約する
- 空きページの領域を同時に予約してコミットする
VirtualAllocFromApp を使用してページのブロックを予約し、 その後 VirtualAllocFromApp を追加で呼び出して、予約済みブロックから 個々のページをコミットできます。これにより、プロセスは必要になるまで物理ストレージを消費することなく、 仮想アドレス空間の範囲を予約できます。
BaseAddress パラメーターが NULL でない場合、関数は BaseAddress および Size パラメーターを使用して、割り当てるページの 領域を計算します。ページ範囲全体の現在の状態は、AllocationType パラメーターで指定された 割り当ての種類と互換性がある必要があります。そうでない場合、関数は失敗し、 どのページも割り当てられません。この互換性要件は、前述のとおり、既にコミット済みの ページをコミットすることを妨げるものではありません。
VirtualAllocFromApp は実行可能なページの作成を許可しません。
VirtualAllocFromApp 関数を使用して、指定されたプロセスの仮想アドレス空間内に Address Windowing Extensions (AWE) メモリ領域を予約できます。このメモリ領域は、アプリケーションの必要に応じて物理ページを仮想メモリに マップしたり、仮想メモリから外したりするために使用できます。 AllocationType パラメーターには MEM_PHYSICAL および MEM_RESERVE 値を設定する必要があります。MEM_COMMIT 値は 設定してはなりません。ページ保護は PAGE_READWRITE に設定する必要があります。
VirtualFree 関数は、コミット済みの ページをデコミットしてそのページのストレージを解放したり、コミット済みのページを同時にデコミットして解放したりできます。また、 予約済みのページを解放して空きページにすることもできます。
実行可能な領域を作成する場合、コードを配置した後に FlushInstructionCache を適切に呼び出して キャッシュの一貫性を確保する責任は呼び出し元のプログラムにあります。そうしないと、新たに実行可能になった領域から コードを実行しようとした際に予測不能な 結果を生じる可能性があります。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// api-ms-win-core-memory-l1-1-3.dll
#include <windows.h>
void* VirtualAllocFromApp(
void* BaseAddress, // optional
UINT_PTR Size,
VIRTUAL_ALLOCATION_TYPE AllocationType,
DWORD Protection
);[DllImport("api-ms-win-core-memory-l1-1-3.dll", SetLastError = true, ExactSpelling = true)]
static extern IntPtr VirtualAllocFromApp(
IntPtr BaseAddress, // void* optional
UIntPtr Size, // UINT_PTR
uint AllocationType, // VIRTUAL_ALLOCATION_TYPE
uint Protection // DWORD
);<DllImport("api-ms-win-core-memory-l1-1-3.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function VirtualAllocFromApp(
BaseAddress As IntPtr, ' void* optional
Size As UIntPtr, ' UINT_PTR
AllocationType As UInteger, ' VIRTUAL_ALLOCATION_TYPE
Protection As UInteger ' DWORD
) As IntPtr
End Function' BaseAddress : void* optional
' Size : UINT_PTR
' AllocationType : VIRTUAL_ALLOCATION_TYPE
' Protection : DWORD
Declare PtrSafe Function VirtualAllocFromApp Lib "api-ms-win-core-memory-l1-1-3" ( _
ByVal BaseAddress As LongPtr, _
ByVal Size As LongPtr, _
ByVal AllocationType As Long, _
ByVal Protection As Long) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
VirtualAllocFromApp = ctypes.windll.LoadLibrary("api-ms-win-core-memory-l1-1-3.dll").VirtualAllocFromApp
VirtualAllocFromApp.restype = ctypes.c_void_p
VirtualAllocFromApp.argtypes = [
ctypes.POINTER(None), # BaseAddress : void* optional
ctypes.c_size_t, # Size : UINT_PTR
wintypes.DWORD, # AllocationType : VIRTUAL_ALLOCATION_TYPE
wintypes.DWORD, # Protection : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('api-ms-win-core-memory-l1-1-3.dll')
VirtualAllocFromApp = Fiddle::Function.new(
lib['VirtualAllocFromApp'],
[
Fiddle::TYPE_VOIDP, # BaseAddress : void* optional
Fiddle::TYPE_UINTPTR_T, # Size : UINT_PTR
-Fiddle::TYPE_INT, # AllocationType : VIRTUAL_ALLOCATION_TYPE
-Fiddle::TYPE_INT, # Protection : DWORD
],
Fiddle::TYPE_VOIDP)#[link(name = "api-ms-win-core-memory-l1-1-3")]
extern "system" {
fn VirtualAllocFromApp(
BaseAddress: *mut (), // void* optional
Size: usize, // UINT_PTR
AllocationType: u32, // VIRTUAL_ALLOCATION_TYPE
Protection: u32 // DWORD
) -> *mut ();
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("api-ms-win-core-memory-l1-1-3.dll", SetLastError = true)]
public static extern IntPtr VirtualAllocFromApp(IntPtr BaseAddress, UIntPtr Size, uint AllocationType, uint Protection);
"@
$api = Add-Type -MemberDefinition $sig -Name 'api-ms-win-core-memory-l1-1-3_VirtualAllocFromApp' -Namespace Win32 -PassThru
# $api::VirtualAllocFromApp(BaseAddress, Size, AllocationType, Protection)#uselib "api-ms-win-core-memory-l1-1-3.dll"
#func global VirtualAllocFromApp "VirtualAllocFromApp" sptr, sptr, sptr, sptr
; VirtualAllocFromApp BaseAddress, Size, AllocationType, Protection ; 戻り値は stat
; BaseAddress : void* optional -> "sptr"
; Size : UINT_PTR -> "sptr"
; AllocationType : VIRTUAL_ALLOCATION_TYPE -> "sptr"
; Protection : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "api-ms-win-core-memory-l1-1-3.dll"
#cfunc global VirtualAllocFromApp "VirtualAllocFromApp" sptr, sptr, int, int
; res = VirtualAllocFromApp(BaseAddress, Size, AllocationType, Protection)
; BaseAddress : void* optional -> "sptr"
; Size : UINT_PTR -> "sptr"
; AllocationType : VIRTUAL_ALLOCATION_TYPE -> "int"
; Protection : DWORD -> "int"; void* VirtualAllocFromApp(void* BaseAddress, UINT_PTR Size, VIRTUAL_ALLOCATION_TYPE AllocationType, DWORD Protection)
#uselib "api-ms-win-core-memory-l1-1-3.dll"
#cfunc global VirtualAllocFromApp "VirtualAllocFromApp" intptr, intptr, int, int
; res = VirtualAllocFromApp(BaseAddress, Size, AllocationType, Protection)
; BaseAddress : void* optional -> "intptr"
; Size : UINT_PTR -> "intptr"
; AllocationType : VIRTUAL_ALLOCATION_TYPE -> "int"
; Protection : DWORD -> "int"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
api_ms_win_core_memory_l1_1_3 = windows.NewLazySystemDLL("api-ms-win-core-memory-l1-1-3.dll")
procVirtualAllocFromApp = api_ms_win_core_memory_l1_1_3.NewProc("VirtualAllocFromApp")
)
// BaseAddress (void* optional), Size (UINT_PTR), AllocationType (VIRTUAL_ALLOCATION_TYPE), Protection (DWORD)
r1, _, err := procVirtualAllocFromApp.Call(
uintptr(BaseAddress),
uintptr(Size),
uintptr(AllocationType),
uintptr(Protection),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // void*function VirtualAllocFromApp(
BaseAddress: Pointer; // void* optional
Size: NativeUInt; // UINT_PTR
AllocationType: DWORD; // VIRTUAL_ALLOCATION_TYPE
Protection: DWORD // DWORD
): Pointer; stdcall;
external 'api-ms-win-core-memory-l1-1-3.dll' name 'VirtualAllocFromApp';result := DllCall("api-ms-win-core-memory-l1-1-3\VirtualAllocFromApp"
, "Ptr", BaseAddress ; void* optional
, "UPtr", Size ; UINT_PTR
, "UInt", AllocationType ; VIRTUAL_ALLOCATION_TYPE
, "UInt", Protection ; DWORD
, "Ptr") ; return: void*●VirtualAllocFromApp(BaseAddress, Size, AllocationType, Protection) = DLL("api-ms-win-core-memory-l1-1-3.dll", "void* VirtualAllocFromApp(void*, int, dword, dword)")
# 呼び出し: VirtualAllocFromApp(BaseAddress, Size, AllocationType, Protection)
# BaseAddress : void* optional -> "void*"
# Size : UINT_PTR -> "int"
# AllocationType : VIRTUAL_ALLOCATION_TYPE -> "dword"
# Protection : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "api-ms-win-core-memory-l1-1-3" fn VirtualAllocFromApp(
BaseAddress: ?*anyopaque, // void* optional
Size: usize, // UINT_PTR
AllocationType: u32, // VIRTUAL_ALLOCATION_TYPE
Protection: u32 // DWORD
) callconv(std.os.windows.WINAPI) ?*anyopaque;proc VirtualAllocFromApp(
BaseAddress: pointer, # void* optional
Size: uint, # UINT_PTR
AllocationType: uint32, # VIRTUAL_ALLOCATION_TYPE
Protection: uint32 # DWORD
): pointer {.importc: "VirtualAllocFromApp", stdcall, dynlib: "api-ms-win-core-memory-l1-1-3.dll".}pragma(lib, "api-ms-win-core-memory-l1-1-3");
extern(Windows)
void* VirtualAllocFromApp(
void* BaseAddress, // void* optional
size_t Size, // UINT_PTR
uint AllocationType, // VIRTUAL_ALLOCATION_TYPE
uint Protection // DWORD
);ccall((:VirtualAllocFromApp, "api-ms-win-core-memory-l1-1-3.dll"), stdcall, Ptr{Cvoid},
(Ptr{Cvoid}, Csize_t, UInt32, UInt32),
BaseAddress, Size, AllocationType, Protection)
# BaseAddress : void* optional -> Ptr{Cvoid}
# Size : UINT_PTR -> Csize_t
# AllocationType : VIRTUAL_ALLOCATION_TYPE -> UInt32
# Protection : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
void* VirtualAllocFromApp(
void* BaseAddress,
uintptr_t Size,
uint32_t AllocationType,
uint32_t Protection);
]]
local api-ms-win-core-memory-l1-1-3 = ffi.load("api-ms-win-core-memory-l1-1-3")
-- api-ms-win-core-memory-l1-1-3.VirtualAllocFromApp(BaseAddress, Size, AllocationType, Protection)
-- BaseAddress : void* optional
-- Size : UINT_PTR
-- AllocationType : VIRTUAL_ALLOCATION_TYPE
-- Protection : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('api-ms-win-core-memory-l1-1-3.dll');
const VirtualAllocFromApp = lib.func('__stdcall', 'VirtualAllocFromApp', 'void *', ['void *', 'uintptr_t', 'uint32_t', 'uint32_t']);
// VirtualAllocFromApp(BaseAddress, Size, AllocationType, Protection)
// BaseAddress : void* optional -> 'void *'
// Size : UINT_PTR -> 'uintptr_t'
// AllocationType : VIRTUAL_ALLOCATION_TYPE -> 'uint32_t'
// Protection : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("api-ms-win-core-memory-l1-1-3.dll", {
VirtualAllocFromApp: { parameters: ["pointer", "usize", "u32", "u32"], result: "pointer" },
});
// lib.symbols.VirtualAllocFromApp(BaseAddress, Size, AllocationType, Protection)
// BaseAddress : void* optional -> "pointer"
// Size : UINT_PTR -> "usize"
// AllocationType : VIRTUAL_ALLOCATION_TYPE -> "u32"
// Protection : DWORD -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
void* VirtualAllocFromApp(
void* BaseAddress,
size_t Size,
uint32_t AllocationType,
uint32_t Protection);
C, "api-ms-win-core-memory-l1-1-3.dll");
// $ffi->VirtualAllocFromApp(BaseAddress, Size, AllocationType, Protection);
// BaseAddress : void* optional
// Size : UINT_PTR
// AllocationType : VIRTUAL_ALLOCATION_TYPE
// Protection : DWORD
// 構造体/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 Api-ms-win-core-memory-l1-1-3 extends StdCallLibrary {
Api-ms-win-core-memory-l1-1-3 INSTANCE = Native.load("api-ms-win-core-memory-l1-1-3", Api-ms-win-core-memory-l1-1-3.class);
Pointer VirtualAllocFromApp(
Pointer BaseAddress, // void* optional
long Size, // UINT_PTR
int AllocationType, // VIRTUAL_ALLOCATION_TYPE
int Protection // DWORD
);
}@[Link("api-ms-win-core-memory-l1-1-3")]
lib Libapi-ms-win-core-memory-l1-1-3
fun VirtualAllocFromApp = VirtualAllocFromApp(
BaseAddress : Void*, # void* optional
Size : LibC::SizeT, # UINT_PTR
AllocationType : UInt32, # VIRTUAL_ALLOCATION_TYPE
Protection : UInt32 # DWORD
) : Void*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef VirtualAllocFromAppNative = Pointer<Void> Function(Pointer<Void>, UintPtr, Uint32, Uint32);
typedef VirtualAllocFromAppDart = Pointer<Void> Function(Pointer<Void>, int, int, int);
final VirtualAllocFromApp = DynamicLibrary.open('api-ms-win-core-memory-l1-1-3.dll')
.lookupFunction<VirtualAllocFromAppNative, VirtualAllocFromAppDart>('VirtualAllocFromApp');
// BaseAddress : void* optional -> Pointer<Void>
// Size : UINT_PTR -> UintPtr
// AllocationType : VIRTUAL_ALLOCATION_TYPE -> Uint32
// Protection : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function VirtualAllocFromApp(
BaseAddress: Pointer; // void* optional
Size: NativeUInt; // UINT_PTR
AllocationType: DWORD; // VIRTUAL_ALLOCATION_TYPE
Protection: DWORD // DWORD
): Pointer; stdcall;
external 'api-ms-win-core-memory-l1-1-3.dll' name 'VirtualAllocFromApp';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "VirtualAllocFromApp"
c_VirtualAllocFromApp :: Ptr () -> CUIntPtr -> Word32 -> Word32 -> IO (Ptr ())
-- BaseAddress : void* optional -> Ptr ()
-- Size : UINT_PTR -> CUIntPtr
-- AllocationType : VIRTUAL_ALLOCATION_TYPE -> Word32
-- Protection : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let virtualallocfromapp =
foreign "VirtualAllocFromApp"
((ptr void) @-> size_t @-> uint32_t @-> uint32_t @-> returning (ptr void))
(* BaseAddress : void* optional -> (ptr void) *)
(* Size : UINT_PTR -> size_t *)
(* AllocationType : VIRTUAL_ALLOCATION_TYPE -> uint32_t *)
(* Protection : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library api-ms-win-core-memory-l1-1-3 (t "api-ms-win-core-memory-l1-1-3.dll"))
(cffi:use-foreign-library api-ms-win-core-memory-l1-1-3)
(cffi:defcfun ("VirtualAllocFromApp" virtual-alloc-from-app :convention :stdcall) :pointer
(base-address :pointer) ; void* optional
(size :uint64) ; UINT_PTR
(allocation-type :uint32) ; VIRTUAL_ALLOCATION_TYPE
(protection :uint32)) ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $VirtualAllocFromApp = Win32::API::More->new('api-ms-win-core-memory-l1-1-3',
'LPVOID VirtualAllocFromApp(LPVOID BaseAddress, WPARAM Size, DWORD AllocationType, DWORD Protection)');
# my $ret = $VirtualAllocFromApp->Call($BaseAddress, $Size, $AllocationType, $Protection);
# BaseAddress : void* optional -> LPVOID
# Size : UINT_PTR -> WPARAM
# AllocationType : VIRTUAL_ALLOCATION_TYPE -> DWORD
# Protection : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f VirtualAlloc — プロセスの仮想アドレス空間にメモリを確保する。
- f VirtualAllocEx — 指定プロセスの仮想アドレス空間にメモリ領域を予約またはコミットする。
- f VirtualFree — 確保した仮想メモリ領域を解放または解除する。
- f VirtualLock — 指定範囲のメモリを物理メモリにロックしページングを防ぐ。
- f VirtualProtectFromApp — UWPアプリ向けにコミット済みメモリの保護属性を変更する。
- f VirtualQuery — 仮想アドレス空間のメモリ領域情報を取得する。