ホーム › System.Memory › VirtualAlloc2
VirtualAlloc2
関数拡張パラメータを指定して仮想メモリを予約またはコミットする。
シグネチャ
// api-ms-win-core-memory-l1-1-6.dll
#include <windows.h>
void* VirtualAlloc2(
HANDLE Process, // optional
void* BaseAddress, // optional
UINT_PTR Size,
VIRTUAL_ALLOCATION_TYPE AllocationType,
DWORD PageProtection,
MEM_EXTENDED_PARAMETER* ExtendedParameters, // optional
DWORD ParameterCount
);パラメーター
| 名前 | 型 | 方向 |
|---|---|---|
| Process | HANDLE | inoptional |
| BaseAddress | void* | inoptional |
| Size | UINT_PTR | in |
| AllocationType | VIRTUAL_ALLOCATION_TYPE | in |
| PageProtection | DWORD | in |
| ExtendedParameters | MEM_EXTENDED_PARAMETER* | inoutoptional |
| ParameterCount | DWORD | in |
戻り値の型: void*
各言語での呼び出し定義
// api-ms-win-core-memory-l1-1-6.dll
#include <windows.h>
void* VirtualAlloc2(
HANDLE Process, // optional
void* BaseAddress, // optional
UINT_PTR Size,
VIRTUAL_ALLOCATION_TYPE AllocationType,
DWORD PageProtection,
MEM_EXTENDED_PARAMETER* ExtendedParameters, // optional
DWORD ParameterCount
);[DllImport("api-ms-win-core-memory-l1-1-6.dll", SetLastError = true, ExactSpelling = true)]
static extern IntPtr VirtualAlloc2(
IntPtr Process, // HANDLE optional
IntPtr BaseAddress, // void* optional
UIntPtr Size, // UINT_PTR
uint AllocationType, // VIRTUAL_ALLOCATION_TYPE
uint PageProtection, // DWORD
IntPtr ExtendedParameters, // MEM_EXTENDED_PARAMETER* optional, in/out
uint ParameterCount // DWORD
);<DllImport("api-ms-win-core-memory-l1-1-6.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function VirtualAlloc2(
Process As IntPtr, ' HANDLE optional
BaseAddress As IntPtr, ' void* optional
Size As UIntPtr, ' UINT_PTR
AllocationType As UInteger, ' VIRTUAL_ALLOCATION_TYPE
PageProtection As UInteger, ' DWORD
ExtendedParameters As IntPtr, ' MEM_EXTENDED_PARAMETER* optional, in/out
ParameterCount As UInteger ' DWORD
) As IntPtr
End Function' Process : HANDLE optional
' BaseAddress : void* optional
' Size : UINT_PTR
' AllocationType : VIRTUAL_ALLOCATION_TYPE
' PageProtection : DWORD
' ExtendedParameters : MEM_EXTENDED_PARAMETER* optional, in/out
' ParameterCount : DWORD
Declare PtrSafe Function VirtualAlloc2 Lib "api-ms-win-core-memory-l1-1-6" ( _
ByVal Process As LongPtr, _
ByVal BaseAddress As LongPtr, _
ByVal Size As LongPtr, _
ByVal AllocationType As Long, _
ByVal PageProtection As Long, _
ByVal ExtendedParameters As LongPtr, _
ByVal ParameterCount As Long) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
VirtualAlloc2 = ctypes.windll.LoadLibrary("api-ms-win-core-memory-l1-1-6.dll").VirtualAlloc2
VirtualAlloc2.restype = ctypes.c_void_p
VirtualAlloc2.argtypes = [
wintypes.HANDLE, # Process : HANDLE optional
ctypes.POINTER(None), # BaseAddress : void* optional
ctypes.c_size_t, # Size : UINT_PTR
wintypes.DWORD, # AllocationType : VIRTUAL_ALLOCATION_TYPE
wintypes.DWORD, # PageProtection : DWORD
ctypes.c_void_p, # ExtendedParameters : MEM_EXTENDED_PARAMETER* optional, in/out
wintypes.DWORD, # ParameterCount : 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-6.dll')
VirtualAlloc2 = Fiddle::Function.new(
lib['VirtualAlloc2'],
[
Fiddle::TYPE_VOIDP, # Process : HANDLE optional
Fiddle::TYPE_VOIDP, # BaseAddress : void* optional
Fiddle::TYPE_UINTPTR_T, # Size : UINT_PTR
-Fiddle::TYPE_INT, # AllocationType : VIRTUAL_ALLOCATION_TYPE
-Fiddle::TYPE_INT, # PageProtection : DWORD
Fiddle::TYPE_VOIDP, # ExtendedParameters : MEM_EXTENDED_PARAMETER* optional, in/out
-Fiddle::TYPE_INT, # ParameterCount : DWORD
],
Fiddle::TYPE_VOIDP)#[link(name = "api-ms-win-core-memory-l1-1-6")]
extern "system" {
fn VirtualAlloc2(
Process: *mut core::ffi::c_void, // HANDLE optional
BaseAddress: *mut (), // void* optional
Size: usize, // UINT_PTR
AllocationType: u32, // VIRTUAL_ALLOCATION_TYPE
PageProtection: u32, // DWORD
ExtendedParameters: *mut MEM_EXTENDED_PARAMETER, // MEM_EXTENDED_PARAMETER* optional, in/out
ParameterCount: u32 // DWORD
) -> *mut ();
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("api-ms-win-core-memory-l1-1-6.dll", SetLastError = true)]
public static extern IntPtr VirtualAlloc2(IntPtr Process, IntPtr BaseAddress, UIntPtr Size, uint AllocationType, uint PageProtection, IntPtr ExtendedParameters, uint ParameterCount);
"@
$api = Add-Type -MemberDefinition $sig -Name 'api-ms-win-core-memory-l1-1-6_VirtualAlloc2' -Namespace Win32 -PassThru
# $api::VirtualAlloc2(Process, BaseAddress, Size, AllocationType, PageProtection, ExtendedParameters, ParameterCount)#uselib "api-ms-win-core-memory-l1-1-6.dll"
#func global VirtualAlloc2 "VirtualAlloc2" sptr, sptr, sptr, sptr, sptr, sptr, sptr
; VirtualAlloc2 Process, BaseAddress, Size, AllocationType, PageProtection, varptr(ExtendedParameters), ParameterCount ; 戻り値は stat
; Process : HANDLE optional -> "sptr"
; BaseAddress : void* optional -> "sptr"
; Size : UINT_PTR -> "sptr"
; AllocationType : VIRTUAL_ALLOCATION_TYPE -> "sptr"
; PageProtection : DWORD -> "sptr"
; ExtendedParameters : MEM_EXTENDED_PARAMETER* optional, in/out -> "sptr"
; ParameterCount : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "api-ms-win-core-memory-l1-1-6.dll" #cfunc global VirtualAlloc2 "VirtualAlloc2" sptr, sptr, sptr, int, int, var, int ; res = VirtualAlloc2(Process, BaseAddress, Size, AllocationType, PageProtection, ExtendedParameters, ParameterCount) ; Process : HANDLE optional -> "sptr" ; BaseAddress : void* optional -> "sptr" ; Size : UINT_PTR -> "sptr" ; AllocationType : VIRTUAL_ALLOCATION_TYPE -> "int" ; PageProtection : DWORD -> "int" ; ExtendedParameters : MEM_EXTENDED_PARAMETER* optional, in/out -> "var" ; ParameterCount : DWORD -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "api-ms-win-core-memory-l1-1-6.dll" #cfunc global VirtualAlloc2 "VirtualAlloc2" sptr, sptr, sptr, int, int, sptr, int ; res = VirtualAlloc2(Process, BaseAddress, Size, AllocationType, PageProtection, varptr(ExtendedParameters), ParameterCount) ; Process : HANDLE optional -> "sptr" ; BaseAddress : void* optional -> "sptr" ; Size : UINT_PTR -> "sptr" ; AllocationType : VIRTUAL_ALLOCATION_TYPE -> "int" ; PageProtection : DWORD -> "int" ; ExtendedParameters : MEM_EXTENDED_PARAMETER* optional, in/out -> "sptr" ; ParameterCount : DWORD -> "int" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; void* VirtualAlloc2(HANDLE Process, void* BaseAddress, UINT_PTR Size, VIRTUAL_ALLOCATION_TYPE AllocationType, DWORD PageProtection, MEM_EXTENDED_PARAMETER* ExtendedParameters, DWORD ParameterCount) #uselib "api-ms-win-core-memory-l1-1-6.dll" #cfunc global VirtualAlloc2 "VirtualAlloc2" intptr, intptr, intptr, int, int, var, int ; res = VirtualAlloc2(Process, BaseAddress, Size, AllocationType, PageProtection, ExtendedParameters, ParameterCount) ; Process : HANDLE optional -> "intptr" ; BaseAddress : void* optional -> "intptr" ; Size : UINT_PTR -> "intptr" ; AllocationType : VIRTUAL_ALLOCATION_TYPE -> "int" ; PageProtection : DWORD -> "int" ; ExtendedParameters : MEM_EXTENDED_PARAMETER* optional, in/out -> "var" ; ParameterCount : DWORD -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; void* VirtualAlloc2(HANDLE Process, void* BaseAddress, UINT_PTR Size, VIRTUAL_ALLOCATION_TYPE AllocationType, DWORD PageProtection, MEM_EXTENDED_PARAMETER* ExtendedParameters, DWORD ParameterCount) #uselib "api-ms-win-core-memory-l1-1-6.dll" #cfunc global VirtualAlloc2 "VirtualAlloc2" intptr, intptr, intptr, int, int, intptr, int ; res = VirtualAlloc2(Process, BaseAddress, Size, AllocationType, PageProtection, varptr(ExtendedParameters), ParameterCount) ; Process : HANDLE optional -> "intptr" ; BaseAddress : void* optional -> "intptr" ; Size : UINT_PTR -> "intptr" ; AllocationType : VIRTUAL_ALLOCATION_TYPE -> "int" ; PageProtection : DWORD -> "int" ; ExtendedParameters : MEM_EXTENDED_PARAMETER* optional, in/out -> "intptr" ; ParameterCount : DWORD -> "int" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
api_ms_win_core_memory_l1_1_6 = windows.NewLazySystemDLL("api-ms-win-core-memory-l1-1-6.dll")
procVirtualAlloc2 = api_ms_win_core_memory_l1_1_6.NewProc("VirtualAlloc2")
)
// Process (HANDLE optional), BaseAddress (void* optional), Size (UINT_PTR), AllocationType (VIRTUAL_ALLOCATION_TYPE), PageProtection (DWORD), ExtendedParameters (MEM_EXTENDED_PARAMETER* optional, in/out), ParameterCount (DWORD)
r1, _, err := procVirtualAlloc2.Call(
uintptr(Process),
uintptr(BaseAddress),
uintptr(Size),
uintptr(AllocationType),
uintptr(PageProtection),
uintptr(ExtendedParameters),
uintptr(ParameterCount),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // void*function VirtualAlloc2(
Process: THandle; // HANDLE optional
BaseAddress: Pointer; // void* optional
Size: NativeUInt; // UINT_PTR
AllocationType: DWORD; // VIRTUAL_ALLOCATION_TYPE
PageProtection: DWORD; // DWORD
ExtendedParameters: Pointer; // MEM_EXTENDED_PARAMETER* optional, in/out
ParameterCount: DWORD // DWORD
): Pointer; stdcall;
external 'api-ms-win-core-memory-l1-1-6.dll' name 'VirtualAlloc2';result := DllCall("api-ms-win-core-memory-l1-1-6\VirtualAlloc2"
, "Ptr", Process ; HANDLE optional
, "Ptr", BaseAddress ; void* optional
, "UPtr", Size ; UINT_PTR
, "UInt", AllocationType ; VIRTUAL_ALLOCATION_TYPE
, "UInt", PageProtection ; DWORD
, "Ptr", ExtendedParameters ; MEM_EXTENDED_PARAMETER* optional, in/out
, "UInt", ParameterCount ; DWORD
, "Ptr") ; return: void*●VirtualAlloc2(Process, BaseAddress, Size, AllocationType, PageProtection, ExtendedParameters, ParameterCount) = DLL("api-ms-win-core-memory-l1-1-6.dll", "void* VirtualAlloc2(void*, void*, int, dword, dword, void*, dword)")
# 呼び出し: VirtualAlloc2(Process, BaseAddress, Size, AllocationType, PageProtection, ExtendedParameters, ParameterCount)
# Process : HANDLE optional -> "void*"
# BaseAddress : void* optional -> "void*"
# Size : UINT_PTR -> "int"
# AllocationType : VIRTUAL_ALLOCATION_TYPE -> "dword"
# PageProtection : DWORD -> "dword"
# ExtendedParameters : MEM_EXTENDED_PARAMETER* optional, in/out -> "void*"
# ParameterCount : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。