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

MapViewOfFile3

関数
拡張パラメータを指定してファイルマッピングのビューをマップする。
DLLapi-ms-win-core-memory-l1-1-6.dll呼出規約winapiSetLastErrorあり対応OSWindows 10 以降

シグネチャ

// api-ms-win-core-memory-l1-1-6.dll
#include <windows.h>

MEMORY_MAPPED_VIEW_ADDRESS MapViewOfFile3(
    HANDLE FileMapping,
    HANDLE Process,   // optional
    void* BaseAddress,   // optional
    ULONGLONG Offset,
    UINT_PTR ViewSize,
    VIRTUAL_ALLOCATION_TYPE AllocationType,
    DWORD PageProtection,
    MEM_EXTENDED_PARAMETER* ExtendedParameters,   // optional
    DWORD ParameterCount
);

パラメーター

名前方向説明
FileMappingHANDLEin指定されたプロセスのアドレス空間にマップするセクションへの HANDLE です。
ProcessHANDLEinoptionalセクションのマップ先となるプロセスへの HANDLE です。
BaseAddressvoid*inoptional

ビューの希望するベースアドレスです(アドレスは直近の 64k 境界へ切り下げられます)。

このパラメーターが NULL の場合、システムがベースアドレスを選択します。

BaseAddressNULL でない場合、指定する MEM_ADDRESS_REQUIREMENTS はすべてゼロでなければなりません。

OffsetULONGLONGin

セクションの先頭からのオフセットです。

オフセットは 64k 境界に整列していなければなりません。AllocationType で MEM_LARGE_PAGES を使用する場合は GetLargePageMinimum に整列している必要があります。さらに、AllocationType で MEM_REPLACE_PLACEHOLDER を使用する場合、オフセットは VirtualAlloc2 によって付与される基盤ページサイズにページ整列している必要があります。

ViewSizeUINT_PTRin

マップするバイト数です。値が 0 の場合、セクション全体をマップすることを指定します。

サイズは常にページサイズの倍数でなければなりません。

AllocationTypeVIRTUAL_ALLOCATION_TYPEin

メモリ割り当ての種類です。このパラメーターには 0、または次のいずれかの値を指定できます。

Value Meaning
MEM_RESERVE
0x00002000
予約済みビューをマップします。
MEM_REPLACE_PLACEHOLDER
0x00004000
プレースホルダーをマップされたビューで置き換えます。サポートされるのはデータ/ページファイルベースのセクションビューのみです(イメージ、物理メモリなどは不可)。プレースホルダーを置き換える場合、BaseAddressViewSize はプレースホルダーのものと完全に一致していなければならず、指定する MEM_ADDRESS_REQUIREMENTS 構造体はすべてゼロでなければなりません。

プレースホルダーをマップされたビューで置き換えた後、そのマップされたビューを解放してプレースホルダーへ戻すには、UnmapViewOfFileEx および UnmapViewOfFile2UnmapFlags パラメーターを参照してください。

プレースホルダーは予約済みメモリ領域の一種です。

このフラグを指定した場合、Offset および BaseAddress に対する 64k 整列の要件は適用されません。

MEM_LARGE_PAGES
0x20000000
ラージページビューをマップします。このフラグは、ビューを ラージページサポートを使用してマップすることを指定します。ビューのサイズは GetLargePageMinimum 関数が報告するラージページサイズの倍数でなければならず、ファイルマッピングオブジェクトは SEC_LARGE_PAGES オプションを使用して作成されている必要があります。BaseAddress パラメーターに NULL 以外の値を指定する場合、その値は GetLargePageMinimum の倍数でなければなりません。

このフラグを指定した場合、Offset に対する 64k 整列の要件は適用されません。

PageProtectionDWORDin

希望するページ保護です。

SEC_IMAGE 属性で作成されたファイルマッピングオブジェクトの場合、PageProtection パラメーターは効果を持たず、PAGE_READONLY などの任意の有効な値に設定してください。

ExtendedParametersMEM_EXTENDED_PARAMETER*inoutoptionalMEM_EXTENDED_PARAMETER 型の 1 つ以上の拡張パラメーターへのオプションのポインターです。これらの各拡張パラメーター値は、それ自身の Type フィールドとして MemExtendedParameterAddressRequirements または MemExtendedParameterNumaNode のいずれかを持つことができます。MemExtendedParameterNumaNode 拡張パラメーターが指定されない場合の動作は、VirtualAlloc/MapViewOfFile 関数と同じです(すなわち、物理ページに対する優先 NUMA ノードは、そのメモリに最初にアクセスするスレッドの理想プロセッサーに基づいて決定されます)。
ParameterCountDWORDinExtendedParameters が指す拡張パラメーターの数です。

戻り値の型: MEMORY_MAPPED_VIEW_ADDRESS

公式ドキュメント

ファイルまたはページファイルベースのセクションのビューを、指定されたプロセスのアドレス空間にマップします。(MapViewOfFile3)

戻り値

成功した場合、マップされたビューのベースアドレスを返します。失敗した場合は NULL を返し、拡張エラー情報は GetLastError を使用して取得できます。

解説(Remarks)

この API は、仮想アドレス空間の管理に特有の要件を持つ高性能ゲームやサーバーアプリケーションをサポートします。たとえば、以前に予約された領域の上にメモリをマップすることができ、これは自動的にラップするリングバッファーの実装に役立ちます。また、特定の整列でメモリを割り当てることもでき、たとえばアプリケーションがラージ/ヒュージページマップ領域をオンデマンドでコミットできるようにします。

この関数を新規の割り当てに使用すると、次のことが可能です。

NUMA ノードを指定するには、ExtendedParameters パラメーターを参照してください。

Examples

コード例については、VirtualAlloc2 のシナリオ 1 を参照してください。

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

各言語での呼び出し定義

// api-ms-win-core-memory-l1-1-6.dll
#include <windows.h>

MEMORY_MAPPED_VIEW_ADDRESS MapViewOfFile3(
    HANDLE FileMapping,
    HANDLE Process,   // optional
    void* BaseAddress,   // optional
    ULONGLONG Offset,
    UINT_PTR ViewSize,
    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 MEMORY_MAPPED_VIEW_ADDRESS MapViewOfFile3(
    IntPtr FileMapping,   // HANDLE
    IntPtr Process,   // HANDLE optional
    IntPtr BaseAddress,   // void* optional
    ulong Offset,   // ULONGLONG
    UIntPtr ViewSize,   // 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 MapViewOfFile3(
    FileMapping As IntPtr,   ' HANDLE
    Process As IntPtr,   ' HANDLE optional
    BaseAddress As IntPtr,   ' void* optional
    Offset As ULong,   ' ULONGLONG
    ViewSize 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 MEMORY_MAPPED_VIEW_ADDRESS
End Function
' FileMapping : HANDLE
' Process : HANDLE optional
' BaseAddress : void* optional
' Offset : ULONGLONG
' ViewSize : UINT_PTR
' AllocationType : VIRTUAL_ALLOCATION_TYPE
' PageProtection : DWORD
' ExtendedParameters : MEM_EXTENDED_PARAMETER* optional, in/out
' ParameterCount : DWORD
Declare PtrSafe Function MapViewOfFile3 Lib "api-ms-win-core-memory-l1-1-6" ( _
    ByVal FileMapping As LongPtr, _
    ByVal Process As LongPtr, _
    ByVal BaseAddress As LongPtr, _
    ByVal Offset As LongLong, _
    ByVal ViewSize 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

MapViewOfFile3 = ctypes.windll.LoadLibrary("api-ms-win-core-memory-l1-1-6.dll").MapViewOfFile3
MapViewOfFile3.restype = ctypes.c_void_p
MapViewOfFile3.argtypes = [
    wintypes.HANDLE,  # FileMapping : HANDLE
    wintypes.HANDLE,  # Process : HANDLE optional
    ctypes.POINTER(None),  # BaseAddress : void* optional
    ctypes.c_ulonglong,  # Offset : ULONGLONG
    ctypes.c_size_t,  # ViewSize : 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')
MapViewOfFile3 = Fiddle::Function.new(
  lib['MapViewOfFile3'],
  [
    Fiddle::TYPE_VOIDP,  # FileMapping : HANDLE
    Fiddle::TYPE_VOIDP,  # Process : HANDLE optional
    Fiddle::TYPE_VOIDP,  # BaseAddress : void* optional
    -Fiddle::TYPE_LONG_LONG,  # Offset : ULONGLONG
    Fiddle::TYPE_UINTPTR_T,  # ViewSize : 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 MapViewOfFile3(
        FileMapping: *mut core::ffi::c_void,  // HANDLE
        Process: *mut core::ffi::c_void,  // HANDLE optional
        BaseAddress: *mut (),  // void* optional
        Offset: u64,  // ULONGLONG
        ViewSize: 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
    ) -> MEMORY_MAPPED_VIEW_ADDRESS;
}
// 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 MEMORY_MAPPED_VIEW_ADDRESS MapViewOfFile3(IntPtr FileMapping, IntPtr Process, IntPtr BaseAddress, ulong Offset, UIntPtr ViewSize, uint AllocationType, uint PageProtection, IntPtr ExtendedParameters, uint ParameterCount);
"@
$api = Add-Type -MemberDefinition $sig -Name 'api-ms-win-core-memory-l1-1-6_MapViewOfFile3' -Namespace Win32 -PassThru
# $api::MapViewOfFile3(FileMapping, Process, BaseAddress, Offset, ViewSize, AllocationType, PageProtection, ExtendedParameters, ParameterCount)
#uselib "api-ms-win-core-memory-l1-1-6.dll"
#func global MapViewOfFile3 "MapViewOfFile3" sptr, sptr, sptr, sptr, sptr, sptr, sptr, sptr, sptr
; MapViewOfFile3 FileMapping, Process, BaseAddress, Offset, ViewSize, AllocationType, PageProtection, varptr(ExtendedParameters), ParameterCount   ; 戻り値は stat
; FileMapping : HANDLE -> "sptr"
; Process : HANDLE optional -> "sptr"
; BaseAddress : void* optional -> "sptr"
; Offset : ULONGLONG -> "sptr"
; ViewSize : UINT_PTR -> "sptr"
; AllocationType : VIRTUAL_ALLOCATION_TYPE -> "sptr"
; PageProtection : DWORD -> "sptr"
; ExtendedParameters : MEM_EXTENDED_PARAMETER* optional, in/out -> "sptr"
; ParameterCount : DWORD -> "sptr"
; ※HSP3.7は int64 引数(64bit値渡し)に非対応です。
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "api-ms-win-core-memory-l1-1-6.dll"
#cfunc global MapViewOfFile3 "MapViewOfFile3" sptr, sptr, sptr, int64, sptr, int, int, var, int
; res = MapViewOfFile3(FileMapping, Process, BaseAddress, Offset, ViewSize, AllocationType, PageProtection, ExtendedParameters, ParameterCount)
; FileMapping : HANDLE -> "sptr"
; Process : HANDLE optional -> "sptr"
; BaseAddress : void* optional -> "sptr"
; Offset : ULONGLONG -> "int64"
; ViewSize : UINT_PTR -> "sptr"
; AllocationType : VIRTUAL_ALLOCATION_TYPE -> "int"
; PageProtection : DWORD -> "int"
; ExtendedParameters : MEM_EXTENDED_PARAMETER* optional, in/out -> "var"
; ParameterCount : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
; ※int64 引数の DLL 値渡しは x64 ランタイム(hsp3_64)のみ対応(x86 は未対応)。
出力引数:
; MEMORY_MAPPED_VIEW_ADDRESS MapViewOfFile3(HANDLE FileMapping, HANDLE Process, void* BaseAddress, ULONGLONG Offset, UINT_PTR ViewSize, VIRTUAL_ALLOCATION_TYPE AllocationType, DWORD PageProtection, MEM_EXTENDED_PARAMETER* ExtendedParameters, DWORD ParameterCount)
#uselib "api-ms-win-core-memory-l1-1-6.dll"
#cfunc global MapViewOfFile3 "MapViewOfFile3" intptr, intptr, intptr, int64, intptr, int, int, var, int
; res = MapViewOfFile3(FileMapping, Process, BaseAddress, Offset, ViewSize, AllocationType, PageProtection, ExtendedParameters, ParameterCount)
; FileMapping : HANDLE -> "intptr"
; Process : HANDLE optional -> "intptr"
; BaseAddress : void* optional -> "intptr"
; Offset : ULONGLONG -> "int64"
; ViewSize : UINT_PTR -> "intptr"
; AllocationType : VIRTUAL_ALLOCATION_TYPE -> "int"
; PageProtection : DWORD -> "int"
; ExtendedParameters : MEM_EXTENDED_PARAMETER* optional, in/out -> "var"
; ParameterCount : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。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")
	procMapViewOfFile3 = api_ms_win_core_memory_l1_1_6.NewProc("MapViewOfFile3")
)

// FileMapping (HANDLE), Process (HANDLE optional), BaseAddress (void* optional), Offset (ULONGLONG), ViewSize (UINT_PTR), AllocationType (VIRTUAL_ALLOCATION_TYPE), PageProtection (DWORD), ExtendedParameters (MEM_EXTENDED_PARAMETER* optional, in/out), ParameterCount (DWORD)
r1, _, err := procMapViewOfFile3.Call(
	uintptr(FileMapping),
	uintptr(Process),
	uintptr(BaseAddress),
	uintptr(Offset),
	uintptr(ViewSize),
	uintptr(AllocationType),
	uintptr(PageProtection),
	uintptr(ExtendedParameters),
	uintptr(ParameterCount),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // MEMORY_MAPPED_VIEW_ADDRESS
function MapViewOfFile3(
  FileMapping: THandle;   // HANDLE
  Process: THandle;   // HANDLE optional
  BaseAddress: Pointer;   // void* optional
  Offset: UInt64;   // ULONGLONG
  ViewSize: NativeUInt;   // UINT_PTR
  AllocationType: DWORD;   // VIRTUAL_ALLOCATION_TYPE
  PageProtection: DWORD;   // DWORD
  ExtendedParameters: Pointer;   // MEM_EXTENDED_PARAMETER* optional, in/out
  ParameterCount: DWORD   // DWORD
): MEMORY_MAPPED_VIEW_ADDRESS; stdcall;
  external 'api-ms-win-core-memory-l1-1-6.dll' name 'MapViewOfFile3';
result := DllCall("api-ms-win-core-memory-l1-1-6\MapViewOfFile3"
    , "Ptr", FileMapping   ; HANDLE
    , "Ptr", Process   ; HANDLE optional
    , "Ptr", BaseAddress   ; void* optional
    , "Int64", Offset   ; ULONGLONG
    , "UPtr", ViewSize   ; UINT_PTR
    , "UInt", AllocationType   ; VIRTUAL_ALLOCATION_TYPE
    , "UInt", PageProtection   ; DWORD
    , "Ptr", ExtendedParameters   ; MEM_EXTENDED_PARAMETER* optional, in/out
    , "UInt", ParameterCount   ; DWORD
    , "Ptr")   ; return: MEMORY_MAPPED_VIEW_ADDRESS
●MapViewOfFile3(FileMapping, Process, BaseAddress, Offset, ViewSize, AllocationType, PageProtection, ExtendedParameters, ParameterCount) = DLL("api-ms-win-core-memory-l1-1-6.dll", "void* MapViewOfFile3(void*, void*, void*, qword, int, dword, dword, void*, dword)")
# 呼び出し: MapViewOfFile3(FileMapping, Process, BaseAddress, Offset, ViewSize, AllocationType, PageProtection, ExtendedParameters, ParameterCount)
# FileMapping : HANDLE -> "void*"
# Process : HANDLE optional -> "void*"
# BaseAddress : void* optional -> "void*"
# Offset : ULONGLONG -> "qword"
# ViewSize : 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)。
const std = @import("std");

extern "api-ms-win-core-memory-l1-1-6" fn MapViewOfFile3(
    FileMapping: ?*anyopaque, // HANDLE
    Process: ?*anyopaque, // HANDLE optional
    BaseAddress: ?*anyopaque, // void* optional
    Offset: u64, // ULONGLONG
    ViewSize: usize, // UINT_PTR
    AllocationType: u32, // VIRTUAL_ALLOCATION_TYPE
    PageProtection: u32, // DWORD
    ExtendedParameters: [*c]MEM_EXTENDED_PARAMETER, // MEM_EXTENDED_PARAMETER* optional, in/out
    ParameterCount: u32 // DWORD
) callconv(std.os.windows.WINAPI) MEMORY_MAPPED_VIEW_ADDRESS;
proc MapViewOfFile3(
    FileMapping: pointer,  # HANDLE
    Process: pointer,  # HANDLE optional
    BaseAddress: pointer,  # void* optional
    Offset: uint64,  # ULONGLONG
    ViewSize: uint,  # UINT_PTR
    AllocationType: uint32,  # VIRTUAL_ALLOCATION_TYPE
    PageProtection: uint32,  # DWORD
    ExtendedParameters: ptr MEM_EXTENDED_PARAMETER,  # MEM_EXTENDED_PARAMETER* optional, in/out
    ParameterCount: uint32  # DWORD
): MEMORY_MAPPED_VIEW_ADDRESS {.importc: "MapViewOfFile3", stdcall, dynlib: "api-ms-win-core-memory-l1-1-6.dll".}
pragma(lib, "api-ms-win-core-memory-l1-1-6");
extern(Windows)
MEMORY_MAPPED_VIEW_ADDRESS MapViewOfFile3(
    void* FileMapping,   // HANDLE
    void* Process,   // HANDLE optional
    void* BaseAddress,   // void* optional
    ulong Offset,   // ULONGLONG
    size_t ViewSize,   // UINT_PTR
    uint AllocationType,   // VIRTUAL_ALLOCATION_TYPE
    uint PageProtection,   // DWORD
    MEM_EXTENDED_PARAMETER* ExtendedParameters,   // MEM_EXTENDED_PARAMETER* optional, in/out
    uint ParameterCount   // DWORD
);
ccall((:MapViewOfFile3, "api-ms-win-core-memory-l1-1-6.dll"), stdcall, MEMORY_MAPPED_VIEW_ADDRESS,
      (Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, UInt64, Csize_t, UInt32, UInt32, Ptr{MEM_EXTENDED_PARAMETER}, UInt32),
      FileMapping, Process, BaseAddress, Offset, ViewSize, AllocationType, PageProtection, ExtendedParameters, ParameterCount)
# FileMapping : HANDLE -> Ptr{Cvoid}
# Process : HANDLE optional -> Ptr{Cvoid}
# BaseAddress : void* optional -> Ptr{Cvoid}
# Offset : ULONGLONG -> UInt64
# ViewSize : UINT_PTR -> Csize_t
# AllocationType : VIRTUAL_ALLOCATION_TYPE -> UInt32
# PageProtection : DWORD -> UInt32
# ExtendedParameters : MEM_EXTENDED_PARAMETER* optional, in/out -> Ptr{MEM_EXTENDED_PARAMETER}
# ParameterCount : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
MEMORY_MAPPED_VIEW_ADDRESS MapViewOfFile3(
    void* FileMapping,
    void* Process,
    void* BaseAddress,
    uint64_t Offset,
    uintptr_t ViewSize,
    uint32_t AllocationType,
    uint32_t PageProtection,
    void* ExtendedParameters,
    uint32_t ParameterCount);
]]
local api-ms-win-core-memory-l1-1-6 = ffi.load("api-ms-win-core-memory-l1-1-6")
-- api-ms-win-core-memory-l1-1-6.MapViewOfFile3(FileMapping, Process, BaseAddress, Offset, ViewSize, AllocationType, PageProtection, ExtendedParameters, ParameterCount)
-- FileMapping : HANDLE
-- Process : HANDLE optional
-- BaseAddress : void* optional
-- Offset : ULONGLONG
-- ViewSize : UINT_PTR
-- AllocationType : VIRTUAL_ALLOCATION_TYPE
-- PageProtection : DWORD
-- ExtendedParameters : MEM_EXTENDED_PARAMETER* optional, in/out
-- ParameterCount : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('api-ms-win-core-memory-l1-1-6.dll');
const MapViewOfFile3 = lib.func('__stdcall', 'MapViewOfFile3', 'MEMORY_MAPPED_VIEW_ADDRESS', ['void *', 'void *', 'void *', 'uint64_t', 'uintptr_t', 'uint32_t', 'uint32_t', 'void *', 'uint32_t']);
// MapViewOfFile3(FileMapping, Process, BaseAddress, Offset, ViewSize, AllocationType, PageProtection, ExtendedParameters, ParameterCount)
// FileMapping : HANDLE -> 'void *'
// Process : HANDLE optional -> 'void *'
// BaseAddress : void* optional -> 'void *'
// Offset : ULONGLONG -> 'uint64_t'
// ViewSize : UINT_PTR -> 'uintptr_t'
// AllocationType : VIRTUAL_ALLOCATION_TYPE -> 'uint32_t'
// PageProtection : DWORD -> 'uint32_t'
// ExtendedParameters : MEM_EXTENDED_PARAMETER* optional, in/out -> 'void *'
// ParameterCount : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("api-ms-win-core-memory-l1-1-6.dll", {
  MapViewOfFile3: { parameters: ["pointer", "pointer", "pointer", "u64", "usize", "u32", "u32", "pointer", "u32"], result: "pointer" },
});
// lib.symbols.MapViewOfFile3(FileMapping, Process, BaseAddress, Offset, ViewSize, AllocationType, PageProtection, ExtendedParameters, ParameterCount)
// FileMapping : HANDLE -> "pointer"
// Process : HANDLE optional -> "pointer"
// BaseAddress : void* optional -> "pointer"
// Offset : ULONGLONG -> "u64"
// ViewSize : UINT_PTR -> "usize"
// AllocationType : VIRTUAL_ALLOCATION_TYPE -> "u32"
// PageProtection : DWORD -> "u32"
// ExtendedParameters : MEM_EXTENDED_PARAMETER* optional, in/out -> "pointer"
// ParameterCount : DWORD -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
MEMORY_MAPPED_VIEW_ADDRESS MapViewOfFile3(
    void* FileMapping,
    void* Process,
    void* BaseAddress,
    uint64_t Offset,
    size_t ViewSize,
    uint32_t AllocationType,
    uint32_t PageProtection,
    void* ExtendedParameters,
    uint32_t ParameterCount);
C, "api-ms-win-core-memory-l1-1-6.dll");
// $ffi->MapViewOfFile3(FileMapping, Process, BaseAddress, Offset, ViewSize, AllocationType, PageProtection, ExtendedParameters, ParameterCount);
// FileMapping : HANDLE
// Process : HANDLE optional
// BaseAddress : void* optional
// Offset : ULONGLONG
// ViewSize : UINT_PTR
// AllocationType : VIRTUAL_ALLOCATION_TYPE
// PageProtection : DWORD
// ExtendedParameters : MEM_EXTENDED_PARAMETER* optional, in/out
// ParameterCount : DWORD
// 構造体/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 Api-ms-win-core-memory-l1-1-6 extends StdCallLibrary {
    Api-ms-win-core-memory-l1-1-6 INSTANCE = Native.load("api-ms-win-core-memory-l1-1-6", Api-ms-win-core-memory-l1-1-6.class);
    MEMORY_MAPPED_VIEW_ADDRESS MapViewOfFile3(
        Pointer FileMapping,   // HANDLE
        Pointer Process,   // HANDLE optional
        Pointer BaseAddress,   // void* optional
        long Offset,   // ULONGLONG
        long ViewSize,   // UINT_PTR
        int AllocationType,   // VIRTUAL_ALLOCATION_TYPE
        int PageProtection,   // DWORD
        Pointer ExtendedParameters,   // MEM_EXTENDED_PARAMETER* optional, in/out
        int ParameterCount   // DWORD
    );
}
@[Link("api-ms-win-core-memory-l1-1-6")]
lib Libapi-ms-win-core-memory-l1-1-6
  fun MapViewOfFile3 = MapViewOfFile3(
    FileMapping : Void*,   # HANDLE
    Process : Void*,   # HANDLE optional
    BaseAddress : Void*,   # void* optional
    Offset : UInt64,   # ULONGLONG
    ViewSize : LibC::SizeT,   # UINT_PTR
    AllocationType : UInt32,   # VIRTUAL_ALLOCATION_TYPE
    PageProtection : UInt32,   # DWORD
    ExtendedParameters : MEM_EXTENDED_PARAMETER*,   # MEM_EXTENDED_PARAMETER* optional, in/out
    ParameterCount : UInt32   # DWORD
  ) : MEMORY_MAPPED_VIEW_ADDRESS
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef MapViewOfFile3Native = MEMORY_MAPPED_VIEW_ADDRESS Function(Pointer<Void>, Pointer<Void>, Pointer<Void>, Uint64, UintPtr, Uint32, Uint32, Pointer<Void>, Uint32);
typedef MapViewOfFile3Dart = int Function(Pointer<Void>, Pointer<Void>, Pointer<Void>, int, int, int, int, Pointer<Void>, int);
final MapViewOfFile3 = DynamicLibrary.open('api-ms-win-core-memory-l1-1-6.dll')
    .lookupFunction<MapViewOfFile3Native, MapViewOfFile3Dart>('MapViewOfFile3');
// FileMapping : HANDLE -> Pointer<Void>
// Process : HANDLE optional -> Pointer<Void>
// BaseAddress : void* optional -> Pointer<Void>
// Offset : ULONGLONG -> Uint64
// ViewSize : UINT_PTR -> UintPtr
// AllocationType : VIRTUAL_ALLOCATION_TYPE -> Uint32
// PageProtection : DWORD -> Uint32
// ExtendedParameters : MEM_EXTENDED_PARAMETER* optional, in/out -> Pointer<Void>
// ParameterCount : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function MapViewOfFile3(
  FileMapping: THandle;   // HANDLE
  Process: THandle;   // HANDLE optional
  BaseAddress: Pointer;   // void* optional
  Offset: UInt64;   // ULONGLONG
  ViewSize: NativeUInt;   // UINT_PTR
  AllocationType: DWORD;   // VIRTUAL_ALLOCATION_TYPE
  PageProtection: DWORD;   // DWORD
  ExtendedParameters: Pointer;   // MEM_EXTENDED_PARAMETER* optional, in/out
  ParameterCount: DWORD   // DWORD
): MEMORY_MAPPED_VIEW_ADDRESS; stdcall;
  external 'api-ms-win-core-memory-l1-1-6.dll' name 'MapViewOfFile3';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "MapViewOfFile3"
  c_MapViewOfFile3 :: Ptr () -> Ptr () -> Ptr () -> Word64 -> CUIntPtr -> Word32 -> Word32 -> Ptr () -> Word32 -> IO MEMORY_MAPPED_VIEW_ADDRESS
-- FileMapping : HANDLE -> Ptr ()
-- Process : HANDLE optional -> Ptr ()
-- BaseAddress : void* optional -> Ptr ()
-- Offset : ULONGLONG -> Word64
-- ViewSize : UINT_PTR -> CUIntPtr
-- AllocationType : VIRTUAL_ALLOCATION_TYPE -> Word32
-- PageProtection : DWORD -> Word32
-- ExtendedParameters : MEM_EXTENDED_PARAMETER* optional, in/out -> Ptr ()
-- ParameterCount : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
-- ※値渡し構造体は GHC FFI で直接渡せません。Ptr で渡すラッパ(C側)経由にしてください。
open Ctypes
open Foreign

let mapviewoffile3 =
  foreign "MapViewOfFile3"
    ((ptr void) @-> (ptr void) @-> (ptr void) @-> uint64_t @-> size_t @-> uint32_t @-> uint32_t @-> (ptr void) @-> uint32_t @-> returning MEMORY_MAPPED_VIEW_ADDRESS)
(* FileMapping : HANDLE -> (ptr void) *)
(* Process : HANDLE optional -> (ptr void) *)
(* BaseAddress : void* optional -> (ptr void) *)
(* Offset : ULONGLONG -> uint64_t *)
(* ViewSize : UINT_PTR -> size_t *)
(* AllocationType : VIRTUAL_ALLOCATION_TYPE -> uint32_t *)
(* PageProtection : DWORD -> uint32_t *)
(* ExtendedParameters : MEM_EXTENDED_PARAMETER* optional, in/out -> (ptr void) *)
(* ParameterCount : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library api-ms-win-core-memory-l1-1-6 (t "api-ms-win-core-memory-l1-1-6.dll"))
(cffi:use-foreign-library api-ms-win-core-memory-l1-1-6)

(cffi:defcfun ("MapViewOfFile3" map-view-of-file3 :convention :stdcall) (:struct memory-mapped-view-address)
  (file-mapping :pointer)   ; HANDLE
  (process :pointer)   ; HANDLE optional
  (base-address :pointer)   ; void* optional
  (offset :uint64)   ; ULONGLONG
  (view-size :uint64)   ; UINT_PTR
  (allocation-type :uint32)   ; VIRTUAL_ALLOCATION_TYPE
  (page-protection :uint32)   ; DWORD
  (extended-parameters :pointer)   ; MEM_EXTENDED_PARAMETER* optional, in/out
  (parameter-count :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $MapViewOfFile3 = Win32::API::More->new('api-ms-win-core-memory-l1-1-6',
    'LPVOID MapViewOfFile3(HANDLE FileMapping, HANDLE Process, LPVOID BaseAddress, UINT64 Offset, WPARAM ViewSize, DWORD AllocationType, DWORD PageProtection, LPVOID ExtendedParameters, DWORD ParameterCount)');
# my $ret = $MapViewOfFile3->Call($FileMapping, $Process, $BaseAddress, $Offset, $ViewSize, $AllocationType, $PageProtection, $ExtendedParameters, $ParameterCount);
# FileMapping : HANDLE -> HANDLE
# Process : HANDLE optional -> HANDLE
# BaseAddress : void* optional -> LPVOID
# Offset : ULONGLONG -> UINT64
# ViewSize : UINT_PTR -> WPARAM
# AllocationType : VIRTUAL_ALLOCATION_TYPE -> DWORD
# PageProtection : DWORD -> DWORD
# ExtendedParameters : MEM_EXTENDED_PARAMETER* optional, in/out -> LPVOID
# ParameterCount : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

類似 API
公式の関連項目
使用する型