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

VirtualQuery

関数
仮想アドレス空間のメモリ領域情報を取得する。
DLLKERNEL32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

UINT_PTR VirtualQuery(
    const void* lpAddress,   // optional
    MEMORY_BASIC_INFORMATION* lpBuffer,
    UINT_PTR dwLength
);

パラメーター

名前方向
lpAddressvoid*inoptional
lpBufferMEMORY_BASIC_INFORMATION*out
dwLengthUINT_PTRin

戻り値の型: UINT_PTR

各言語での呼び出し定義

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

UINT_PTR VirtualQuery(
    const void* lpAddress,   // optional
    MEMORY_BASIC_INFORMATION* lpBuffer,
    UINT_PTR dwLength
);
[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern UIntPtr VirtualQuery(
    IntPtr lpAddress,   // void* optional
    IntPtr lpBuffer,   // MEMORY_BASIC_INFORMATION* out
    UIntPtr dwLength   // UINT_PTR
);
<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function VirtualQuery(
    lpAddress As IntPtr,   ' void* optional
    lpBuffer As IntPtr,   ' MEMORY_BASIC_INFORMATION* out
    dwLength As UIntPtr   ' UINT_PTR
) As UIntPtr
End Function
' lpAddress : void* optional
' lpBuffer : MEMORY_BASIC_INFORMATION* out
' dwLength : UINT_PTR
Declare PtrSafe Function VirtualQuery Lib "kernel32" ( _
    ByVal lpAddress As LongPtr, _
    ByVal lpBuffer As LongPtr, _
    ByVal dwLength As LongPtr) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

VirtualQuery = ctypes.windll.kernel32.VirtualQuery
VirtualQuery.restype = ctypes.c_size_t
VirtualQuery.argtypes = [
    ctypes.POINTER(None),  # lpAddress : void* optional
    ctypes.c_void_p,  # lpBuffer : MEMORY_BASIC_INFORMATION* out
    ctypes.c_size_t,  # dwLength : UINT_PTR
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
VirtualQuery = Fiddle::Function.new(
  lib['VirtualQuery'],
  [
    Fiddle::TYPE_VOIDP,  # lpAddress : void* optional
    Fiddle::TYPE_VOIDP,  # lpBuffer : MEMORY_BASIC_INFORMATION* out
    Fiddle::TYPE_UINTPTR_T,  # dwLength : UINT_PTR
  ],
  Fiddle::TYPE_UINTPTR_T)
#[link(name = "kernel32")]
extern "system" {
    fn VirtualQuery(
        lpAddress: *const (),  // void* optional
        lpBuffer: *mut MEMORY_BASIC_INFORMATION,  // MEMORY_BASIC_INFORMATION* out
        dwLength: usize  // UINT_PTR
    ) -> usize;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("KERNEL32.dll", SetLastError = true)]
public static extern UIntPtr VirtualQuery(IntPtr lpAddress, IntPtr lpBuffer, UIntPtr dwLength);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_VirtualQuery' -Namespace Win32 -PassThru
# $api::VirtualQuery(lpAddress, lpBuffer, dwLength)
#uselib "KERNEL32.dll"
#func global VirtualQuery "VirtualQuery" sptr, sptr, sptr
; VirtualQuery lpAddress, varptr(lpBuffer), dwLength   ; 戻り値は stat
; lpAddress : void* optional -> "sptr"
; lpBuffer : MEMORY_BASIC_INFORMATION* out -> "sptr"
; dwLength : UINT_PTR -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "KERNEL32.dll"
#cfunc global VirtualQuery "VirtualQuery" sptr, var, sptr
; res = VirtualQuery(lpAddress, lpBuffer, dwLength)
; lpAddress : void* optional -> "sptr"
; lpBuffer : MEMORY_BASIC_INFORMATION* out -> "var"
; dwLength : UINT_PTR -> "sptr"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; UINT_PTR VirtualQuery(void* lpAddress, MEMORY_BASIC_INFORMATION* lpBuffer, UINT_PTR dwLength)
#uselib "KERNEL32.dll"
#cfunc global VirtualQuery "VirtualQuery" intptr, var, intptr
; res = VirtualQuery(lpAddress, lpBuffer, dwLength)
; lpAddress : void* optional -> "intptr"
; lpBuffer : MEMORY_BASIC_INFORMATION* out -> "var"
; dwLength : UINT_PTR -> "intptr"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procVirtualQuery = kernel32.NewProc("VirtualQuery")
)

// lpAddress (void* optional), lpBuffer (MEMORY_BASIC_INFORMATION* out), dwLength (UINT_PTR)
r1, _, err := procVirtualQuery.Call(
	uintptr(lpAddress),
	uintptr(lpBuffer),
	uintptr(dwLength),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // UINT_PTR
function VirtualQuery(
  lpAddress: Pointer;   // void* optional
  lpBuffer: Pointer;   // MEMORY_BASIC_INFORMATION* out
  dwLength: NativeUInt   // UINT_PTR
): NativeUInt; stdcall;
  external 'KERNEL32.dll' name 'VirtualQuery';
result := DllCall("KERNEL32\VirtualQuery"
    , "Ptr", lpAddress   ; void* optional
    , "Ptr", lpBuffer   ; MEMORY_BASIC_INFORMATION* out
    , "UPtr", dwLength   ; UINT_PTR
    , "UPtr")   ; return: UINT_PTR
●VirtualQuery(lpAddress, lpBuffer, dwLength) = DLL("KERNEL32.dll", "int VirtualQuery(void*, void*, int)")
# 呼び出し: VirtualQuery(lpAddress, lpBuffer, dwLength)
# lpAddress : void* optional -> "void*"
# lpBuffer : MEMORY_BASIC_INFORMATION* out -> "void*"
# dwLength : UINT_PTR -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。