ホーム › System.Memory › VirtualAlloc
VirtualAlloc
関数プロセスの仮想アドレス空間にメモリを確保する。
シグネチャ
// KERNEL32.dll
#include <windows.h>
void* VirtualAlloc(
void* lpAddress, // optional
UINT_PTR dwSize,
VIRTUAL_ALLOCATION_TYPE flAllocationType,
PAGE_PROTECTION_FLAGS flProtect
);パラメーター
| 名前 | 型 | 方向 |
|---|---|---|
| lpAddress | void* | inoptional |
| dwSize | UINT_PTR | in |
| flAllocationType | VIRTUAL_ALLOCATION_TYPE | in |
| flProtect | PAGE_PROTECTION_FLAGS | in |
戻り値の型: void*
各言語での呼び出し定義
// KERNEL32.dll
#include <windows.h>
void* VirtualAlloc(
void* lpAddress, // optional
UINT_PTR dwSize,
VIRTUAL_ALLOCATION_TYPE flAllocationType,
PAGE_PROTECTION_FLAGS flProtect
);[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern IntPtr VirtualAlloc(
IntPtr lpAddress, // void* optional
UIntPtr dwSize, // UINT_PTR
uint flAllocationType, // VIRTUAL_ALLOCATION_TYPE
uint flProtect // PAGE_PROTECTION_FLAGS
);<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function VirtualAlloc(
lpAddress As IntPtr, ' void* optional
dwSize As UIntPtr, ' UINT_PTR
flAllocationType As UInteger, ' VIRTUAL_ALLOCATION_TYPE
flProtect As UInteger ' PAGE_PROTECTION_FLAGS
) As IntPtr
End Function' lpAddress : void* optional
' dwSize : UINT_PTR
' flAllocationType : VIRTUAL_ALLOCATION_TYPE
' flProtect : PAGE_PROTECTION_FLAGS
Declare PtrSafe Function VirtualAlloc Lib "kernel32" ( _
ByVal lpAddress As LongPtr, _
ByVal dwSize As LongPtr, _
ByVal flAllocationType As Long, _
ByVal flProtect As Long) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
VirtualAlloc = ctypes.windll.kernel32.VirtualAlloc
VirtualAlloc.restype = ctypes.c_void_p
VirtualAlloc.argtypes = [
ctypes.POINTER(None), # lpAddress : void* optional
ctypes.c_size_t, # dwSize : UINT_PTR
wintypes.DWORD, # flAllocationType : VIRTUAL_ALLOCATION_TYPE
wintypes.DWORD, # flProtect : PAGE_PROTECTION_FLAGS
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('KERNEL32.dll')
VirtualAlloc = Fiddle::Function.new(
lib['VirtualAlloc'],
[
Fiddle::TYPE_VOIDP, # lpAddress : void* optional
Fiddle::TYPE_UINTPTR_T, # dwSize : UINT_PTR
-Fiddle::TYPE_INT, # flAllocationType : VIRTUAL_ALLOCATION_TYPE
-Fiddle::TYPE_INT, # flProtect : PAGE_PROTECTION_FLAGS
],
Fiddle::TYPE_VOIDP)#[link(name = "kernel32")]
extern "system" {
fn VirtualAlloc(
lpAddress: *mut (), // void* optional
dwSize: usize, // UINT_PTR
flAllocationType: u32, // VIRTUAL_ALLOCATION_TYPE
flProtect: u32 // PAGE_PROTECTION_FLAGS
) -> *mut ();
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("KERNEL32.dll", SetLastError = true)]
public static extern IntPtr VirtualAlloc(IntPtr lpAddress, UIntPtr dwSize, uint flAllocationType, uint flProtect);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_VirtualAlloc' -Namespace Win32 -PassThru
# $api::VirtualAlloc(lpAddress, dwSize, flAllocationType, flProtect)#uselib "KERNEL32.dll"
#func global VirtualAlloc "VirtualAlloc" sptr, sptr, sptr, sptr
; VirtualAlloc lpAddress, dwSize, flAllocationType, flProtect ; 戻り値は stat
; lpAddress : void* optional -> "sptr"
; dwSize : UINT_PTR -> "sptr"
; flAllocationType : VIRTUAL_ALLOCATION_TYPE -> "sptr"
; flProtect : PAGE_PROTECTION_FLAGS -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "KERNEL32.dll"
#cfunc global VirtualAlloc "VirtualAlloc" sptr, sptr, int, int
; res = VirtualAlloc(lpAddress, dwSize, flAllocationType, flProtect)
; lpAddress : void* optional -> "sptr"
; dwSize : UINT_PTR -> "sptr"
; flAllocationType : VIRTUAL_ALLOCATION_TYPE -> "int"
; flProtect : PAGE_PROTECTION_FLAGS -> "int"; void* VirtualAlloc(void* lpAddress, UINT_PTR dwSize, VIRTUAL_ALLOCATION_TYPE flAllocationType, PAGE_PROTECTION_FLAGS flProtect)
#uselib "KERNEL32.dll"
#cfunc global VirtualAlloc "VirtualAlloc" intptr, intptr, int, int
; res = VirtualAlloc(lpAddress, dwSize, flAllocationType, flProtect)
; lpAddress : void* optional -> "intptr"
; dwSize : UINT_PTR -> "intptr"
; flAllocationType : VIRTUAL_ALLOCATION_TYPE -> "int"
; flProtect : PAGE_PROTECTION_FLAGS -> "int"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
procVirtualAlloc = kernel32.NewProc("VirtualAlloc")
)
// lpAddress (void* optional), dwSize (UINT_PTR), flAllocationType (VIRTUAL_ALLOCATION_TYPE), flProtect (PAGE_PROTECTION_FLAGS)
r1, _, err := procVirtualAlloc.Call(
uintptr(lpAddress),
uintptr(dwSize),
uintptr(flAllocationType),
uintptr(flProtect),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // void*function VirtualAlloc(
lpAddress: Pointer; // void* optional
dwSize: NativeUInt; // UINT_PTR
flAllocationType: DWORD; // VIRTUAL_ALLOCATION_TYPE
flProtect: DWORD // PAGE_PROTECTION_FLAGS
): Pointer; stdcall;
external 'KERNEL32.dll' name 'VirtualAlloc';result := DllCall("KERNEL32\VirtualAlloc"
, "Ptr", lpAddress ; void* optional
, "UPtr", dwSize ; UINT_PTR
, "UInt", flAllocationType ; VIRTUAL_ALLOCATION_TYPE
, "UInt", flProtect ; PAGE_PROTECTION_FLAGS
, "Ptr") ; return: void*●VirtualAlloc(lpAddress, dwSize, flAllocationType, flProtect) = DLL("KERNEL32.dll", "void* VirtualAlloc(void*, int, dword, dword)")
# 呼び出し: VirtualAlloc(lpAddress, dwSize, flAllocationType, flProtect)
# lpAddress : void* optional -> "void*"
# dwSize : UINT_PTR -> "int"
# flAllocationType : VIRTUAL_ALLOCATION_TYPE -> "dword"
# flProtect : PAGE_PROTECTION_FLAGS -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。