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

OpenFileMappingFromApp

関数
UWPアプリ向けに名前付きファイルマッピングを開く。
DLLapi-ms-win-core-memory-l1-1-3.dll呼出規約winapiSetLastErrorあり対応OSWindows 10 以降

シグネチャ

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

HANDLE OpenFileMappingFromApp(
    DWORD DesiredAccess,
    BOOL InheritHandle,
    LPCWSTR Name
);

パラメーター

名前方向説明
DesiredAccessDWORDinファイルマッピングオブジェクトへのアクセス権です。このアクセス権は、対象となるファイルマッピングオブジェクトのセキュリティ記述子に対して検査されます。値の一覧については、File Mapping Security and Access Rights を参照してください。FILE_MAP_EXECUTE アクセスでファイルマッピングオブジェクトを開けるのは、アプリが codeGeneration 機能を持つ場合のみです。
InheritHandleBOOLinこのパラメーターが TRUE の場合、CreateProcess 関数によって作成されたプロセスはハンドルを継承できます。それ以外の場合、ハンドルは継承されません。
NameLPCWSTRin開くファイルマッピングオブジェクトの名前です。この名前を持つファイルマッピングオブジェクトへの開いたハンドルが既に存在し、かつそのマッピングオブジェクトのセキュリティ記述子が DesiredAccess パラメーターと競合しない場合、オープン操作は成功します。名前には、グローバル名前空間またはセッション名前空間でオブジェクトを明示的に開くために、"Global" または "Local" のプレフィックスを付けることができます。名前の残りの部分には、円記号(\)を除く任意の文字を含めることができます。詳細については、Kernel Object Namespaces を参照してください。ファストユーザー切り替えは Terminal Services セッションを使用して実装されています。最初にログオンするユーザーはセッション 0 を使用し、次にログオンするユーザーはセッション 1 を使用する、という具合です。アプリケーションが複数ユーザーをサポートできるように、カーネルオブジェクト名は Terminal Services 向けに定められたガイドラインに従う必要があります。

戻り値の型: HANDLE

公式ドキュメント

名前付きファイルマッピングオブジェクトを開きます。(OpenFileMappingFromApp)

戻り値

関数が成功した場合、戻り値は指定したファイルマッピングオブジェクトへの開いたハンドルです。

関数が失敗した場合、戻り値は NULL です。拡張エラー情報を取得するには、GetLastError を呼び出してください。

解説(Remarks)

ジャストインタイム(JIT)機能を使用するために、JIT 機能を持つ Windows ストアアプリから OpenFileMappingFromApp を呼び出すことができます。JIT 機能を使用するには、アプリのマニフェストファイルに codeGeneration 機能を含める必要があります。OpenFileMappingFromApp により、Windows ストアアプリは .NET Framework の MemoryMappedFile クラスを使用できます。

OpenFileMappingFromApp が返すハンドルは、ファイルマッピングオブジェクトへのハンドルを必要とする任意の関数で使用できます。

マップされたビューを通じてファイルを変更する場合、最終更新タイムスタンプは自動的には更新されないことがあります。必要に応じて、呼び出し元は SetFileTime を使用してタイムスタンプを設定してください。

ハンドルが不要になったら、呼び出し元は CloseHandle を呼び出して、OpenFileMappingFromApp が返したハンドルを解放してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は 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-3.dll
#include <windows.h>

HANDLE OpenFileMappingFromApp(
    DWORD DesiredAccess,
    BOOL InheritHandle,
    LPCWSTR Name
);
[DllImport("api-ms-win-core-memory-l1-1-3.dll", SetLastError = true, ExactSpelling = true)]
static extern IntPtr OpenFileMappingFromApp(
    uint DesiredAccess,   // DWORD
    bool InheritHandle,   // BOOL
    [MarshalAs(UnmanagedType.LPWStr)] string Name   // LPCWSTR
);
<DllImport("api-ms-win-core-memory-l1-1-3.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function OpenFileMappingFromApp(
    DesiredAccess As UInteger,   ' DWORD
    InheritHandle As Boolean,   ' BOOL
    <MarshalAs(UnmanagedType.LPWStr)> Name As String   ' LPCWSTR
) As IntPtr
End Function
' DesiredAccess : DWORD
' InheritHandle : BOOL
' Name : LPCWSTR
Declare PtrSafe Function OpenFileMappingFromApp Lib "api-ms-win-core-memory-l1-1-3" ( _
    ByVal DesiredAccess As Long, _
    ByVal InheritHandle As Long, _
    ByVal Name As LongPtr) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

OpenFileMappingFromApp = ctypes.windll.LoadLibrary("api-ms-win-core-memory-l1-1-3.dll").OpenFileMappingFromApp
OpenFileMappingFromApp.restype = ctypes.c_void_p
OpenFileMappingFromApp.argtypes = [
    wintypes.DWORD,  # DesiredAccess : DWORD
    wintypes.BOOL,  # InheritHandle : BOOL
    wintypes.LPCWSTR,  # Name : LPCWSTR
]
# 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-3.dll')
OpenFileMappingFromApp = Fiddle::Function.new(
  lib['OpenFileMappingFromApp'],
  [
    -Fiddle::TYPE_INT,  # DesiredAccess : DWORD
    Fiddle::TYPE_INT,  # InheritHandle : BOOL
    Fiddle::TYPE_VOIDP,  # Name : LPCWSTR
  ],
  Fiddle::TYPE_VOIDP)
#[link(name = "api-ms-win-core-memory-l1-1-3")]
extern "system" {
    fn OpenFileMappingFromApp(
        DesiredAccess: u32,  // DWORD
        InheritHandle: i32,  // BOOL
        Name: *const u16  // LPCWSTR
    ) -> *mut core::ffi::c_void;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("api-ms-win-core-memory-l1-1-3.dll", SetLastError = true)]
public static extern IntPtr OpenFileMappingFromApp(uint DesiredAccess, bool InheritHandle, [MarshalAs(UnmanagedType.LPWStr)] string Name);
"@
$api = Add-Type -MemberDefinition $sig -Name 'api-ms-win-core-memory-l1-1-3_OpenFileMappingFromApp' -Namespace Win32 -PassThru
# $api::OpenFileMappingFromApp(DesiredAccess, InheritHandle, Name)
#uselib "api-ms-win-core-memory-l1-1-3.dll"
#func global OpenFileMappingFromApp "OpenFileMappingFromApp" sptr, sptr, sptr
; OpenFileMappingFromApp DesiredAccess, InheritHandle, Name   ; 戻り値は stat
; DesiredAccess : DWORD -> "sptr"
; InheritHandle : BOOL -> "sptr"
; Name : LPCWSTR -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "api-ms-win-core-memory-l1-1-3.dll"
#cfunc global OpenFileMappingFromApp "OpenFileMappingFromApp" int, int, wstr
; res = OpenFileMappingFromApp(DesiredAccess, InheritHandle, Name)
; DesiredAccess : DWORD -> "int"
; InheritHandle : BOOL -> "int"
; Name : LPCWSTR -> "wstr"
; HANDLE OpenFileMappingFromApp(DWORD DesiredAccess, BOOL InheritHandle, LPCWSTR Name)
#uselib "api-ms-win-core-memory-l1-1-3.dll"
#cfunc global OpenFileMappingFromApp "OpenFileMappingFromApp" int, int, wstr
; res = OpenFileMappingFromApp(DesiredAccess, InheritHandle, Name)
; DesiredAccess : DWORD -> "int"
; InheritHandle : BOOL -> "int"
; Name : LPCWSTR -> "wstr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	api_ms_win_core_memory_l1_1_3 = windows.NewLazySystemDLL("api-ms-win-core-memory-l1-1-3.dll")
	procOpenFileMappingFromApp = api_ms_win_core_memory_l1_1_3.NewProc("OpenFileMappingFromApp")
)

// DesiredAccess (DWORD), InheritHandle (BOOL), Name (LPCWSTR)
r1, _, err := procOpenFileMappingFromApp.Call(
	uintptr(DesiredAccess),
	uintptr(InheritHandle),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(Name))),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HANDLE
function OpenFileMappingFromApp(
  DesiredAccess: DWORD;   // DWORD
  InheritHandle: BOOL;   // BOOL
  Name: PWideChar   // LPCWSTR
): THandle; stdcall;
  external 'api-ms-win-core-memory-l1-1-3.dll' name 'OpenFileMappingFromApp';
result := DllCall("api-ms-win-core-memory-l1-1-3\OpenFileMappingFromApp"
    , "UInt", DesiredAccess   ; DWORD
    , "Int", InheritHandle   ; BOOL
    , "WStr", Name   ; LPCWSTR
    , "Ptr")   ; return: HANDLE
●OpenFileMappingFromApp(DesiredAccess, InheritHandle, Name) = DLL("api-ms-win-core-memory-l1-1-3.dll", "void* OpenFileMappingFromApp(dword, bool, char*)")
# 呼び出し: OpenFileMappingFromApp(DesiredAccess, InheritHandle, Name)
# DesiredAccess : DWORD -> "dword"
# InheritHandle : BOOL -> "bool"
# Name : LPCWSTR -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "api-ms-win-core-memory-l1-1-3" fn OpenFileMappingFromApp(
    DesiredAccess: u32, // DWORD
    InheritHandle: i32, // BOOL
    Name: [*c]const u16 // LPCWSTR
) callconv(std.os.windows.WINAPI) ?*anyopaque;
proc OpenFileMappingFromApp(
    DesiredAccess: uint32,  # DWORD
    InheritHandle: int32,  # BOOL
    Name: WideCString  # LPCWSTR
): pointer {.importc: "OpenFileMappingFromApp", stdcall, dynlib: "api-ms-win-core-memory-l1-1-3.dll".}
pragma(lib, "api-ms-win-core-memory-l1-1-3");
extern(Windows)
void* OpenFileMappingFromApp(
    uint DesiredAccess,   // DWORD
    int InheritHandle,   // BOOL
    const(wchar)* Name   // LPCWSTR
);
ccall((:OpenFileMappingFromApp, "api-ms-win-core-memory-l1-1-3.dll"), stdcall, Ptr{Cvoid},
      (UInt32, Int32, Cwstring),
      DesiredAccess, InheritHandle, Name)
# DesiredAccess : DWORD -> UInt32
# InheritHandle : BOOL -> Int32
# Name : LPCWSTR -> Cwstring
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
void* OpenFileMappingFromApp(
    uint32_t DesiredAccess,
    int32_t InheritHandle,
    const uint16_t* Name);
]]
local api-ms-win-core-memory-l1-1-3 = ffi.load("api-ms-win-core-memory-l1-1-3")
-- api-ms-win-core-memory-l1-1-3.OpenFileMappingFromApp(DesiredAccess, InheritHandle, Name)
-- DesiredAccess : DWORD
-- InheritHandle : BOOL
-- Name : LPCWSTR
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('api-ms-win-core-memory-l1-1-3.dll');
const OpenFileMappingFromApp = lib.func('__stdcall', 'OpenFileMappingFromApp', 'void *', ['uint32_t', 'int32_t', 'str16']);
// OpenFileMappingFromApp(DesiredAccess, InheritHandle, Name)
// DesiredAccess : DWORD -> 'uint32_t'
// InheritHandle : BOOL -> 'int32_t'
// Name : LPCWSTR -> 'str16'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("api-ms-win-core-memory-l1-1-3.dll", {
  OpenFileMappingFromApp: { parameters: ["u32", "i32", "buffer"], result: "pointer" },
});
// lib.symbols.OpenFileMappingFromApp(DesiredAccess, InheritHandle, Name)
// DesiredAccess : DWORD -> "u32"
// InheritHandle : BOOL -> "i32"
// Name : LPCWSTR -> "buffer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
void* OpenFileMappingFromApp(
    uint32_t DesiredAccess,
    int32_t InheritHandle,
    const uint16_t* Name);
C, "api-ms-win-core-memory-l1-1-3.dll");
// $ffi->OpenFileMappingFromApp(DesiredAccess, InheritHandle, Name);
// DesiredAccess : DWORD
// InheritHandle : BOOL
// Name : LPCWSTR
// 構造体/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-3 extends StdCallLibrary {
    Api-ms-win-core-memory-l1-1-3 INSTANCE = Native.load("api-ms-win-core-memory-l1-1-3", Api-ms-win-core-memory-l1-1-3.class);
    Pointer OpenFileMappingFromApp(
        int DesiredAccess,   // DWORD
        boolean InheritHandle,   // BOOL
        WString Name   // LPCWSTR
    );
}
@[Link("api-ms-win-core-memory-l1-1-3")]
lib Libapi-ms-win-core-memory-l1-1-3
  fun OpenFileMappingFromApp = OpenFileMappingFromApp(
    DesiredAccess : UInt32,   # DWORD
    InheritHandle : Int32,   # BOOL
    Name : UInt16*   # LPCWSTR
  ) : Void*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef OpenFileMappingFromAppNative = Pointer<Void> Function(Uint32, Int32, Pointer<Utf16>);
typedef OpenFileMappingFromAppDart = Pointer<Void> Function(int, int, Pointer<Utf16>);
final OpenFileMappingFromApp = DynamicLibrary.open('api-ms-win-core-memory-l1-1-3.dll')
    .lookupFunction<OpenFileMappingFromAppNative, OpenFileMappingFromAppDart>('OpenFileMappingFromApp');
// DesiredAccess : DWORD -> Uint32
// InheritHandle : BOOL -> Int32
// Name : LPCWSTR -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function OpenFileMappingFromApp(
  DesiredAccess: DWORD;   // DWORD
  InheritHandle: BOOL;   // BOOL
  Name: PWideChar   // LPCWSTR
): THandle; stdcall;
  external 'api-ms-win-core-memory-l1-1-3.dll' name 'OpenFileMappingFromApp';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "OpenFileMappingFromApp"
  c_OpenFileMappingFromApp :: Word32 -> CInt -> CWString -> IO (Ptr ())
-- DesiredAccess : DWORD -> Word32
-- InheritHandle : BOOL -> CInt
-- Name : LPCWSTR -> CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let openfilemappingfromapp =
  foreign "OpenFileMappingFromApp"
    (uint32_t @-> int32_t @-> (ptr uint16_t) @-> returning (ptr void))
(* DesiredAccess : DWORD -> uint32_t *)
(* InheritHandle : BOOL -> int32_t *)
(* Name : LPCWSTR -> (ptr uint16_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library api-ms-win-core-memory-l1-1-3 (t "api-ms-win-core-memory-l1-1-3.dll"))
(cffi:use-foreign-library api-ms-win-core-memory-l1-1-3)

(cffi:defcfun ("OpenFileMappingFromApp" open-file-mapping-from-app :convention :stdcall) :pointer
  (desired-access :uint32)   ; DWORD
  (inherit-handle :int32)   ; BOOL
  (name (:string :encoding :utf-16le)))   ; LPCWSTR
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $OpenFileMappingFromApp = Win32::API::More->new('api-ms-win-core-memory-l1-1-3',
    'HANDLE OpenFileMappingFromApp(DWORD DesiredAccess, BOOL InheritHandle, LPCWSTR Name)');
# my $ret = $OpenFileMappingFromApp->Call($DesiredAccess, $InheritHandle, $Name);
# DesiredAccess : DWORD -> DWORD
# InheritHandle : BOOL -> BOOL
# Name : LPCWSTR -> LPCWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目