VirtualQuery
関数シグネチャ
// KERNEL32.dll
#include <windows.h>
UINT_PTR VirtualQuery(
const void* lpAddress, // optional
MEMORY_BASIC_INFORMATION* lpBuffer,
UINT_PTR dwLength
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| lpAddress | void* | inoptional | クエリ対象となるページ領域のベースアドレスへのポインターです。この値は、直前のページ境界へ切り下げられます。ホストコンピューター上のページサイズを調べるには、GetSystemInfo 関数を使用してください。 lpAddress が、プロセスからアクセス可能な最上位のメモリアドレスを超えるアドレスを指定している場合、関数は失敗し、ERROR_INVALID_PARAMETER を返します。 |
| lpBuffer | MEMORY_BASIC_INFORMATION* | out | 指定したページ範囲に関する情報が返される MEMORY_BASIC_INFORMATION 構造体へのポインターです。 |
| dwLength | UINT_PTR | in | lpBuffer パラメーターが指すバッファーのサイズ(バイト単位)です。 |
戻り値の型: UINT_PTR
公式ドキュメント
呼び出し元プロセスの仮想アドレス空間内にあるページ範囲に関する情報を取得します。
戻り値
戻り値は、情報バッファーに返された実際のバイト数です。
関数が失敗した場合、戻り値は 0 です。拡張エラー情報を取得するには、GetLastError を呼び出してください。返される可能性のあるエラー値には ERROR_INVALID_PARAMETER が含まれます。
解説(Remarks)
VirtualQuery は、指定したアドレスから始まる連続したページ領域に関する情報を提供します。これらのページは次の属性を共有しています。
- すべてのページの状態が同一であること(MEM_COMMIT、MEM_RESERVE、MEM_FREE、MEM_PRIVATE、MEM_MAPPED、または MEM_IMAGE)。
- 先頭ページが空き状態でない場合、領域内のすべてのページが、VirtualAlloc、MapViewOfFile、またはこれらの拡張版である次のいずれかの関数(VirtualAllocEx、VirtualAllocExNuma、MapViewOfFileEx、MapViewOfFileExNuma)の単一の呼び出しによって作成された、同一の初期ページ割り当ての一部であること。
- すべてのページに付与されたアクセス権が同一であること(PAGE_READONLY、PAGE_READWRITE、PAGE_NOACCESS、PAGE_WRITECOPY、PAGE_EXECUTE、PAGE_EXECUTE_READ、PAGE_EXECUTE_READWRITE、PAGE_EXECUTE_WRITECOPY、PAGE_GUARD、または PAGE_NOCACHE)。
関数は領域内の先頭ページの属性を判定し、その後、ページ範囲全体をスキャンし終えるか、属性のセットが一致しないページに遭遇するまで、後続のページをスキャンします。関数は、属性が一致するページ領域の属性とサイズ(バイト単位)を返します。たとえば、40 メガバイト (MB) の空きメモリ領域があり、その領域の先頭から 10 MB の位置にあるページに対して VirtualQuery を呼び出した場合、関数は MEM_FREE の状態と 30 MB のサイズを取得します。
共有のコピーオンライトページが変更されると、そのページは変更したプロセスに固有のものになります。ただし、VirtualQuery 関数は、そのようなページを MEM_PRIVATE ではなく、MEM_MAPPED(データビューの場合)または MEM_IMAGE(実行可能イメージビューの場合)として引き続き報告します。特定のページでコピーオンライトが発生したかどうかを検出するには、そのページにアクセスするか、VirtualLock 関数を使ってロックし、ページがメモリ上に常駐していることを確認してから、QueryWorkingSetEx 関数を使用して、そのページの拡張ワーキングセット情報内の Shared ビットを確認します。Shared ビットがクリアされている場合、そのページはプライベートです。
この関数は呼び出し元プロセスのメモリ内のページ領域について報告し、VirtualQueryEx 関数は指定したプロセスのメモリ内のページ領域について報告します。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// 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 方式にも切替可。#uselib "KERNEL32.dll" #cfunc global VirtualQuery "VirtualQuery" sptr, sptr, sptr ; res = VirtualQuery(lpAddress, varptr(lpBuffer), dwLength) ; lpAddress : void* optional -> "sptr" ; lpBuffer : MEMORY_BASIC_INFORMATION* out -> "sptr" ; dwLength : UINT_PTR -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは 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 方式にも切替可。; UINT_PTR VirtualQuery(void* lpAddress, MEMORY_BASIC_INFORMATION* lpBuffer, UINT_PTR dwLength) #uselib "KERNEL32.dll" #cfunc global VirtualQuery "VirtualQuery" intptr, intptr, intptr ; res = VirtualQuery(lpAddress, varptr(lpBuffer), dwLength) ; lpAddress : void* optional -> "intptr" ; lpBuffer : MEMORY_BASIC_INFORMATION* out -> "intptr" ; dwLength : UINT_PTR -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは 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_PTRfunction 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)。const std = @import("std");
extern "kernel32" fn VirtualQuery(
lpAddress: ?*anyopaque, // void* optional
lpBuffer: [*c]MEMORY_BASIC_INFORMATION, // MEMORY_BASIC_INFORMATION* out
dwLength: usize // UINT_PTR
) callconv(std.os.windows.WINAPI) usize;proc VirtualQuery(
lpAddress: pointer, # void* optional
lpBuffer: ptr MEMORY_BASIC_INFORMATION, # MEMORY_BASIC_INFORMATION* out
dwLength: uint # UINT_PTR
): uint {.importc: "VirtualQuery", stdcall, dynlib: "KERNEL32.dll".}pragma(lib, "kernel32");
extern(Windows)
size_t VirtualQuery(
void* lpAddress, // void* optional
MEMORY_BASIC_INFORMATION* lpBuffer, // MEMORY_BASIC_INFORMATION* out
size_t dwLength // UINT_PTR
);ccall((:VirtualQuery, "KERNEL32.dll"), stdcall, Csize_t,
(Ptr{Cvoid}, Ptr{MEMORY_BASIC_INFORMATION}, Csize_t),
lpAddress, lpBuffer, dwLength)
# lpAddress : void* optional -> Ptr{Cvoid}
# lpBuffer : MEMORY_BASIC_INFORMATION* out -> Ptr{MEMORY_BASIC_INFORMATION}
# dwLength : UINT_PTR -> Csize_t
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
uintptr_t VirtualQuery(
void* lpAddress,
void* lpBuffer,
uintptr_t dwLength);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.VirtualQuery(lpAddress, lpBuffer, dwLength)
-- lpAddress : void* optional
-- lpBuffer : MEMORY_BASIC_INFORMATION* out
-- dwLength : UINT_PTR
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const VirtualQuery = lib.func('__stdcall', 'VirtualQuery', 'uintptr_t', ['void *', 'void *', 'uintptr_t']);
// VirtualQuery(lpAddress, lpBuffer, dwLength)
// lpAddress : void* optional -> 'void *'
// lpBuffer : MEMORY_BASIC_INFORMATION* out -> 'void *'
// dwLength : UINT_PTR -> 'uintptr_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("KERNEL32.dll", {
VirtualQuery: { parameters: ["pointer", "pointer", "usize"], result: "usize" },
});
// lib.symbols.VirtualQuery(lpAddress, lpBuffer, dwLength)
// lpAddress : void* optional -> "pointer"
// lpBuffer : MEMORY_BASIC_INFORMATION* out -> "pointer"
// dwLength : UINT_PTR -> "usize"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
size_t VirtualQuery(
void* lpAddress,
void* lpBuffer,
size_t dwLength);
C, "KERNEL32.dll");
// $ffi->VirtualQuery(lpAddress, lpBuffer, dwLength);
// lpAddress : void* optional
// lpBuffer : MEMORY_BASIC_INFORMATION* out
// dwLength : UINT_PTR
// 構造体/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);
long VirtualQuery(
Pointer lpAddress, // void* optional
Pointer lpBuffer, // MEMORY_BASIC_INFORMATION* out
long dwLength // UINT_PTR
);
}@[Link("kernel32")]
lib LibKERNEL32
fun VirtualQuery = VirtualQuery(
lpAddress : Void*, # void* optional
lpBuffer : MEMORY_BASIC_INFORMATION*, # MEMORY_BASIC_INFORMATION* out
dwLength : LibC::SizeT # UINT_PTR
) : LibC::SizeT
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef VirtualQueryNative = UintPtr Function(Pointer<Void>, Pointer<Void>, UintPtr);
typedef VirtualQueryDart = int Function(Pointer<Void>, Pointer<Void>, int);
final VirtualQuery = DynamicLibrary.open('KERNEL32.dll')
.lookupFunction<VirtualQueryNative, VirtualQueryDart>('VirtualQuery');
// lpAddress : void* optional -> Pointer<Void>
// lpBuffer : MEMORY_BASIC_INFORMATION* out -> Pointer<Void>
// dwLength : UINT_PTR -> UintPtr
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function VirtualQuery(
lpAddress: Pointer; // void* optional
lpBuffer: Pointer; // MEMORY_BASIC_INFORMATION* out
dwLength: NativeUInt // UINT_PTR
): NativeUInt; stdcall;
external 'KERNEL32.dll' name 'VirtualQuery';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "VirtualQuery"
c_VirtualQuery :: Ptr () -> Ptr () -> CUIntPtr -> IO CUIntPtr
-- lpAddress : void* optional -> Ptr ()
-- lpBuffer : MEMORY_BASIC_INFORMATION* out -> Ptr ()
-- dwLength : UINT_PTR -> CUIntPtr
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let virtualquery =
foreign "VirtualQuery"
((ptr void) @-> (ptr void) @-> size_t @-> returning size_t)
(* lpAddress : void* optional -> (ptr void) *)
(* lpBuffer : MEMORY_BASIC_INFORMATION* out -> (ptr void) *)
(* dwLength : UINT_PTR -> size_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)
(cffi:defcfun ("VirtualQuery" virtual-query :convention :stdcall) :uint64
(lp-address :pointer) ; void* optional
(lp-buffer :pointer) ; MEMORY_BASIC_INFORMATION* out
(dw-length :uint64)) ; UINT_PTR
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $VirtualQuery = Win32::API::More->new('KERNEL32',
'WPARAM VirtualQuery(LPVOID lpAddress, LPVOID lpBuffer, WPARAM dwLength)');
# my $ret = $VirtualQuery->Call($lpAddress, $lpBuffer, $dwLength);
# lpAddress : void* optional -> LPVOID
# lpBuffer : MEMORY_BASIC_INFORMATION* out -> LPVOID
# dwLength : UINT_PTR -> WPARAM
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f VirtualQueryEx — 指定プロセス内の仮想アドレス領域に関する情報を取得する。