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

CreateFileMappingFromApp

関数
UWPアプリ向けにファイルマッピングオブジェクトを作成する。
DLLKERNEL32.dll呼出規約winapiSetLastErrorあり対応OSwindows8.0

シグネチャ

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

HANDLE CreateFileMappingFromApp(
    HANDLE hFile,
    SECURITY_ATTRIBUTES* SecurityAttributes,   // optional
    PAGE_PROTECTION_FLAGS PageProtection,
    ULONGLONG MaximumSize,
    LPCWSTR Name   // optional
);

パラメーター

名前方向
hFileHANDLEin
SecurityAttributesSECURITY_ATTRIBUTES*inoptional
PageProtectionPAGE_PROTECTION_FLAGSin
MaximumSizeULONGLONGin
NameLPCWSTRinoptional

戻り値の型: HANDLE

各言語での呼び出し定義

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

HANDLE CreateFileMappingFromApp(
    HANDLE hFile,
    SECURITY_ATTRIBUTES* SecurityAttributes,   // optional
    PAGE_PROTECTION_FLAGS PageProtection,
    ULONGLONG MaximumSize,
    LPCWSTR Name   // optional
);
[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern IntPtr CreateFileMappingFromApp(
    IntPtr hFile,   // HANDLE
    IntPtr SecurityAttributes,   // SECURITY_ATTRIBUTES* optional
    uint PageProtection,   // PAGE_PROTECTION_FLAGS
    ulong MaximumSize,   // ULONGLONG
    [MarshalAs(UnmanagedType.LPWStr)] string Name   // LPCWSTR optional
);
<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CreateFileMappingFromApp(
    hFile As IntPtr,   ' HANDLE
    SecurityAttributes As IntPtr,   ' SECURITY_ATTRIBUTES* optional
    PageProtection As UInteger,   ' PAGE_PROTECTION_FLAGS
    MaximumSize As ULong,   ' ULONGLONG
    <MarshalAs(UnmanagedType.LPWStr)> Name As String   ' LPCWSTR optional
) As IntPtr
End Function
' hFile : HANDLE
' SecurityAttributes : SECURITY_ATTRIBUTES* optional
' PageProtection : PAGE_PROTECTION_FLAGS
' MaximumSize : ULONGLONG
' Name : LPCWSTR optional
Declare PtrSafe Function CreateFileMappingFromApp Lib "kernel32" ( _
    ByVal hFile As LongPtr, _
    ByVal SecurityAttributes As LongPtr, _
    ByVal PageProtection As Long, _
    ByVal MaximumSize As LongLong, _
    ByVal Name As LongPtr) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CreateFileMappingFromApp = ctypes.windll.kernel32.CreateFileMappingFromApp
CreateFileMappingFromApp.restype = ctypes.c_void_p
CreateFileMappingFromApp.argtypes = [
    wintypes.HANDLE,  # hFile : HANDLE
    ctypes.c_void_p,  # SecurityAttributes : SECURITY_ATTRIBUTES* optional
    wintypes.DWORD,  # PageProtection : PAGE_PROTECTION_FLAGS
    ctypes.c_ulonglong,  # MaximumSize : ULONGLONG
    wintypes.LPCWSTR,  # Name : LPCWSTR optional
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
CreateFileMappingFromApp = Fiddle::Function.new(
  lib['CreateFileMappingFromApp'],
  [
    Fiddle::TYPE_VOIDP,  # hFile : HANDLE
    Fiddle::TYPE_VOIDP,  # SecurityAttributes : SECURITY_ATTRIBUTES* optional
    -Fiddle::TYPE_INT,  # PageProtection : PAGE_PROTECTION_FLAGS
    -Fiddle::TYPE_LONG_LONG,  # MaximumSize : ULONGLONG
    Fiddle::TYPE_VOIDP,  # Name : LPCWSTR optional
  ],
  Fiddle::TYPE_VOIDP)
#[link(name = "kernel32")]
extern "system" {
    fn CreateFileMappingFromApp(
        hFile: *mut core::ffi::c_void,  // HANDLE
        SecurityAttributes: *mut SECURITY_ATTRIBUTES,  // SECURITY_ATTRIBUTES* optional
        PageProtection: u32,  // PAGE_PROTECTION_FLAGS
        MaximumSize: u64,  // ULONGLONG
        Name: *const u16  // LPCWSTR optional
    ) -> *mut core::ffi::c_void;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("KERNEL32.dll", SetLastError = true)]
public static extern IntPtr CreateFileMappingFromApp(IntPtr hFile, IntPtr SecurityAttributes, uint PageProtection, ulong MaximumSize, [MarshalAs(UnmanagedType.LPWStr)] string Name);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_CreateFileMappingFromApp' -Namespace Win32 -PassThru
# $api::CreateFileMappingFromApp(hFile, SecurityAttributes, PageProtection, MaximumSize, Name)
#uselib "KERNEL32.dll"
#func global CreateFileMappingFromApp "CreateFileMappingFromApp" sptr, sptr, sptr, sptr, sptr
; CreateFileMappingFromApp hFile, varptr(SecurityAttributes), PageProtection, MaximumSize, Name   ; 戻り値は stat
; hFile : HANDLE -> "sptr"
; SecurityAttributes : SECURITY_ATTRIBUTES* optional -> "sptr"
; PageProtection : PAGE_PROTECTION_FLAGS -> "sptr"
; MaximumSize : ULONGLONG -> "sptr"
; Name : LPCWSTR optional -> "sptr"
; ※HSP3.7は int64 引数(64bit値渡し)に非対応です。
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "KERNEL32.dll"
#cfunc global CreateFileMappingFromApp "CreateFileMappingFromApp" sptr, var, int, int64, wstr
; res = CreateFileMappingFromApp(hFile, SecurityAttributes, PageProtection, MaximumSize, Name)
; hFile : HANDLE -> "sptr"
; SecurityAttributes : SECURITY_ATTRIBUTES* optional -> "var"
; PageProtection : PAGE_PROTECTION_FLAGS -> "int"
; MaximumSize : ULONGLONG -> "int64"
; Name : LPCWSTR optional -> "wstr"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
; ※int64 引数の DLL 値渡しは x64 ランタイム(hsp3_64)のみ対応(x86 は未対応)。
出力引数:
; HANDLE CreateFileMappingFromApp(HANDLE hFile, SECURITY_ATTRIBUTES* SecurityAttributes, PAGE_PROTECTION_FLAGS PageProtection, ULONGLONG MaximumSize, LPCWSTR Name)
#uselib "KERNEL32.dll"
#cfunc global CreateFileMappingFromApp "CreateFileMappingFromApp" intptr, var, int, int64, wstr
; res = CreateFileMappingFromApp(hFile, SecurityAttributes, PageProtection, MaximumSize, Name)
; hFile : HANDLE -> "intptr"
; SecurityAttributes : SECURITY_ATTRIBUTES* optional -> "var"
; PageProtection : PAGE_PROTECTION_FLAGS -> "int"
; MaximumSize : ULONGLONG -> "int64"
; Name : LPCWSTR optional -> "wstr"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procCreateFileMappingFromApp = kernel32.NewProc("CreateFileMappingFromApp")
)

// hFile (HANDLE), SecurityAttributes (SECURITY_ATTRIBUTES* optional), PageProtection (PAGE_PROTECTION_FLAGS), MaximumSize (ULONGLONG), Name (LPCWSTR optional)
r1, _, err := procCreateFileMappingFromApp.Call(
	uintptr(hFile),
	uintptr(SecurityAttributes),
	uintptr(PageProtection),
	uintptr(MaximumSize),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(Name))),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HANDLE
function CreateFileMappingFromApp(
  hFile: THandle;   // HANDLE
  SecurityAttributes: Pointer;   // SECURITY_ATTRIBUTES* optional
  PageProtection: DWORD;   // PAGE_PROTECTION_FLAGS
  MaximumSize: UInt64;   // ULONGLONG
  Name: PWideChar   // LPCWSTR optional
): THandle; stdcall;
  external 'KERNEL32.dll' name 'CreateFileMappingFromApp';
result := DllCall("KERNEL32\CreateFileMappingFromApp"
    , "Ptr", hFile   ; HANDLE
    , "Ptr", SecurityAttributes   ; SECURITY_ATTRIBUTES* optional
    , "UInt", PageProtection   ; PAGE_PROTECTION_FLAGS
    , "Int64", MaximumSize   ; ULONGLONG
    , "WStr", Name   ; LPCWSTR optional
    , "Ptr")   ; return: HANDLE
●CreateFileMappingFromApp(hFile, SecurityAttributes, PageProtection, MaximumSize, Name) = DLL("KERNEL32.dll", "void* CreateFileMappingFromApp(void*, void*, dword, qword, char*)")
# 呼び出し: CreateFileMappingFromApp(hFile, SecurityAttributes, PageProtection, MaximumSize, Name)
# hFile : HANDLE -> "void*"
# SecurityAttributes : SECURITY_ATTRIBUTES* optional -> "void*"
# PageProtection : PAGE_PROTECTION_FLAGS -> "dword"
# MaximumSize : ULONGLONG -> "qword"
# Name : LPCWSTR optional -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。