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

MapUserPhysicalPages

関数
AWE物理ページを仮想アドレス領域にマップする。
DLLKERNEL32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL MapUserPhysicalPages(
    void* VirtualAddress,
    UINT_PTR NumberOfPages,
    UINT_PTR* PageArray   // optional
);

パラメーター

名前方向説明
VirtualAddressvoid*in

再マップするメモリ領域の開始アドレスへのポインターです。

lpAddress の値は、Address Windowing Extensions (AWE) 領域の割り当て時に VirtualAlloc 関数が返すアドレス範囲内になければなりません。

NumberOfPagesUINT_PTRin

変換を確立する物理メモリおよび仮想アドレス空間のサイズ(ページ単位)です。

仮想アドレス範囲は lpAddress から始まる連続した範囲です。物理フレームは UserPfnArray によって指定されます。

ページの合計数は、開始アドレスから AllocateUserPhysicalPages で指定された範囲の終端を超えることはできません。

PageArrayUINT_PTR*inoptional

物理ページフレーム番号の配列へのポインターです。

これらのフレームは、この関数からの戻り時に引数 lpAddress によってマップされます。割り当てるメモリのサイズは、少なくとも NumberOfPages にデータ型 ULONG_PTR のサイズを乗じた大きさである必要があります。

このバッファーを変更しようとしないでください。オペレーティングシステムのデータが含まれており、破損すると 致命的な結果を招く可能性があります。バッファー内の情報はアプリケーションにとって有用ではありません。

このパラメーターが NULL の場合、指定したアドレス範囲のマッピングが解除されます。また、指定した物理ページは 解放されないため、それらを解放するには FreeUserPhysicalPages を 呼び出す必要があります。

戻り値の型: BOOL

公式ドキュメント

Address Windowing Extensions (AWE) 領域内の指定したアドレスに、事前に割り当てられた物理メモリページをマップします。(MapUserPhysicalPages)

戻り値

関数が成功した場合、戻り値は TRUE です。

関数が失敗した場合、戻り値は FALSE であり、部分的なものを含めマッピングは一切行われません。 拡張エラー情報を取得するには、 GetLastError を呼び出します。

解説(Remarks)

物理ページはマッピングが解除されますが、解放はされません。物理ページを解放するには FreeUserPhysicalPages を呼び出す必要があります。

任意の数の物理メモリページを指定できますが、メモリは VirtualAlloc が割り当てる仮想 アドレス空間の外に及んではなりません。既存のアドレスマップは新しい変換によって自動的に上書きされ、古い変換は マッピング解除されます。

AllocateUserPhysicalPages で指定された範囲の外にある物理メモリページをマップすることはできません。 複数の領域を同時にマップできますが、それらが重なり合うことはできません。

物理ページは任意の物理アドレスに配置できますが、物理ページが連続していると仮定しないでください。

現在のアドレス範囲のマッピングを解除するには、物理メモリページ配列パラメーターに NULL を指定します。現在 マップされているページはマッピング解除されますが、解放はされません。物理ページを解放するには FreeUserPhysicalPages を呼び出す必要があります。

マルチプロセッサ環境では、この関数はハードウェア変換バッファーのコヒーレンシを維持します。この関数からの戻り時には、 すべてのプロセッサ上のすべてのスレッドが正しいマッピングを参照できることが保証されます。

この関数を使用するアプリケーションをコンパイルするには、_WIN32_WINNT マクロを 0x0500 以降として定義します。詳細については、 Windows ヘッダーの使用を参照してください。

例については、AWE の例を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

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

BOOL MapUserPhysicalPages(
    void* VirtualAddress,
    UINT_PTR NumberOfPages,
    UINT_PTR* PageArray   // optional
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool MapUserPhysicalPages(
    IntPtr VirtualAddress,   // void*
    UIntPtr NumberOfPages,   // UINT_PTR
    IntPtr PageArray   // UINT_PTR* optional
);
<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function MapUserPhysicalPages(
    VirtualAddress As IntPtr,   ' void*
    NumberOfPages As UIntPtr,   ' UINT_PTR
    PageArray As IntPtr   ' UINT_PTR* optional
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' VirtualAddress : void*
' NumberOfPages : UINT_PTR
' PageArray : UINT_PTR* optional
Declare PtrSafe Function MapUserPhysicalPages Lib "kernel32" ( _
    ByVal VirtualAddress As LongPtr, _
    ByVal NumberOfPages As LongPtr, _
    ByVal PageArray As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

MapUserPhysicalPages = ctypes.windll.kernel32.MapUserPhysicalPages
MapUserPhysicalPages.restype = wintypes.BOOL
MapUserPhysicalPages.argtypes = [
    ctypes.POINTER(None),  # VirtualAddress : void*
    ctypes.c_size_t,  # NumberOfPages : UINT_PTR
    ctypes.POINTER(ctypes.c_size_t),  # PageArray : UINT_PTR* optional
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procMapUserPhysicalPages = kernel32.NewProc("MapUserPhysicalPages")
)

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

extern "kernel32" fn MapUserPhysicalPages(
    VirtualAddress: ?*anyopaque, // void*
    NumberOfPages: usize, // UINT_PTR
    PageArray: [*c]usize // UINT_PTR* optional
) callconv(std.os.windows.WINAPI) i32;
proc MapUserPhysicalPages(
    VirtualAddress: pointer,  # void*
    NumberOfPages: uint,  # UINT_PTR
    PageArray: ptr uint  # UINT_PTR* optional
): int32 {.importc: "MapUserPhysicalPages", stdcall, dynlib: "KERNEL32.dll".}
pragma(lib, "kernel32");
extern(Windows)
int MapUserPhysicalPages(
    void* VirtualAddress,   // void*
    size_t NumberOfPages,   // UINT_PTR
    size_t* PageArray   // UINT_PTR* optional
);
ccall((:MapUserPhysicalPages, "KERNEL32.dll"), stdcall, Int32,
      (Ptr{Cvoid}, Csize_t, Ptr{Csize_t}),
      VirtualAddress, NumberOfPages, PageArray)
# VirtualAddress : void* -> Ptr{Cvoid}
# NumberOfPages : UINT_PTR -> Csize_t
# PageArray : UINT_PTR* optional -> Ptr{Csize_t}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t MapUserPhysicalPages(
    void* VirtualAddress,
    uintptr_t NumberOfPages,
    uintptr_t* PageArray);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.MapUserPhysicalPages(VirtualAddress, NumberOfPages, PageArray)
-- VirtualAddress : void*
-- NumberOfPages : UINT_PTR
-- PageArray : UINT_PTR* optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const MapUserPhysicalPages = lib.func('__stdcall', 'MapUserPhysicalPages', 'int32_t', ['void *', 'uintptr_t', 'uintptr_t *']);
// MapUserPhysicalPages(VirtualAddress, NumberOfPages, PageArray)
// VirtualAddress : void* -> 'void *'
// NumberOfPages : UINT_PTR -> 'uintptr_t'
// PageArray : UINT_PTR* optional -> 'uintptr_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("KERNEL32.dll", {
  MapUserPhysicalPages: { parameters: ["pointer", "usize", "pointer"], result: "i32" },
});
// lib.symbols.MapUserPhysicalPages(VirtualAddress, NumberOfPages, PageArray)
// VirtualAddress : void* -> "pointer"
// NumberOfPages : UINT_PTR -> "usize"
// PageArray : UINT_PTR* optional -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t MapUserPhysicalPages(
    void* VirtualAddress,
    size_t NumberOfPages,
    size_t* PageArray);
C, "KERNEL32.dll");
// $ffi->MapUserPhysicalPages(VirtualAddress, NumberOfPages, PageArray);
// VirtualAddress : void*
// NumberOfPages : UINT_PTR
// PageArray : UINT_PTR* optional
// 構造体/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);
    boolean MapUserPhysicalPages(
        Pointer VirtualAddress,   // void*
        long NumberOfPages,   // UINT_PTR
        LongByReference PageArray   // UINT_PTR* optional
    );
}
@[Link("kernel32")]
lib LibKERNEL32
  fun MapUserPhysicalPages = MapUserPhysicalPages(
    VirtualAddress : Void*,   # void*
    NumberOfPages : LibC::SizeT,   # UINT_PTR
    PageArray : LibC::SizeT*   # UINT_PTR* optional
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef MapUserPhysicalPagesNative = Int32 Function(Pointer<Void>, UintPtr, Pointer<UintPtr>);
typedef MapUserPhysicalPagesDart = int Function(Pointer<Void>, int, Pointer<UintPtr>);
final MapUserPhysicalPages = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<MapUserPhysicalPagesNative, MapUserPhysicalPagesDart>('MapUserPhysicalPages');
// VirtualAddress : void* -> Pointer<Void>
// NumberOfPages : UINT_PTR -> UintPtr
// PageArray : UINT_PTR* optional -> Pointer<UintPtr>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function MapUserPhysicalPages(
  VirtualAddress: Pointer;   // void*
  NumberOfPages: NativeUInt;   // UINT_PTR
  PageArray: Pointer   // UINT_PTR* optional
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'MapUserPhysicalPages';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "MapUserPhysicalPages"
  c_MapUserPhysicalPages :: Ptr () -> CUIntPtr -> Ptr CUIntPtr -> IO CInt
-- VirtualAddress : void* -> Ptr ()
-- NumberOfPages : UINT_PTR -> CUIntPtr
-- PageArray : UINT_PTR* optional -> Ptr CUIntPtr
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let mapuserphysicalpages =
  foreign "MapUserPhysicalPages"
    ((ptr void) @-> size_t @-> (ptr size_t) @-> returning int32_t)
(* VirtualAddress : void* -> (ptr void) *)
(* NumberOfPages : UINT_PTR -> size_t *)
(* PageArray : UINT_PTR* optional -> (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 ("MapUserPhysicalPages" map-user-physical-pages :convention :stdcall) :int32
  (virtual-address :pointer)   ; void*
  (number-of-pages :uint64)   ; UINT_PTR
  (page-array :pointer))   ; UINT_PTR* optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $MapUserPhysicalPages = Win32::API::More->new('KERNEL32',
    'BOOL MapUserPhysicalPages(LPVOID VirtualAddress, WPARAM NumberOfPages, LPVOID PageArray)');
# my $ret = $MapUserPhysicalPages->Call($VirtualAddress, $NumberOfPages, $PageArray);
# VirtualAddress : void* -> LPVOID
# NumberOfPages : UINT_PTR -> WPARAM
# PageArray : UINT_PTR* optional -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目