ホーム › Storage.FileSystem › CreateFile3
CreateFile3
関数拡張パラメータを指定してファイルを作成または開く。
シグネチャ
// KERNEL32.dll
#include <windows.h>
HANDLE CreateFile3(
LPCWSTR lpFileName,
DWORD dwDesiredAccess,
DWORD dwShareMode,
DWORD dwCreationDisposition,
CREATEFILE3_EXTENDED_PARAMETERS* pCreateExParams // optional
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| lpFileName | LPCWSTR | in | 作成または開くファイルのパス。Unicode文字列。 |
| dwDesiredAccess | DWORD | in | 要求するアクセス権(GENERIC_READ/GENERIC_WRITE 等)。 |
| dwShareMode | DWORD | in | 他プロセスとの共有方法を示す共有フラグ(読み取り・書き込み・削除の共有)。 |
| dwCreationDisposition | DWORD | in | ファイルが存在する/しない場合の動作を示す作成ディスポジション値。 |
| pCreateExParams | CREATEFILE3_EXTENDED_PARAMETERS* | inoptional | 属性・フラグ・セキュリティ等の拡張パラメータを保持する CREATEFILE3_EXTENDED_PARAMETERS 構造体へのポインタ。NULL可。 |
戻り値の型: HANDLE
各言語での呼び出し定義
// KERNEL32.dll
#include <windows.h>
HANDLE CreateFile3(
LPCWSTR lpFileName,
DWORD dwDesiredAccess,
DWORD dwShareMode,
DWORD dwCreationDisposition,
CREATEFILE3_EXTENDED_PARAMETERS* pCreateExParams // optional
);[DllImport("KERNEL32.dll", ExactSpelling = true)]
static extern IntPtr CreateFile3(
[MarshalAs(UnmanagedType.LPWStr)] string lpFileName, // LPCWSTR
uint dwDesiredAccess, // DWORD
uint dwShareMode, // DWORD
uint dwCreationDisposition, // DWORD
IntPtr pCreateExParams // CREATEFILE3_EXTENDED_PARAMETERS* optional
);<DllImport("KERNEL32.dll", ExactSpelling:=True)>
Public Shared Function CreateFile3(
<MarshalAs(UnmanagedType.LPWStr)> lpFileName As String, ' LPCWSTR
dwDesiredAccess As UInteger, ' DWORD
dwShareMode As UInteger, ' DWORD
dwCreationDisposition As UInteger, ' DWORD
pCreateExParams As IntPtr ' CREATEFILE3_EXTENDED_PARAMETERS* optional
) As IntPtr
End Function' lpFileName : LPCWSTR
' dwDesiredAccess : DWORD
' dwShareMode : DWORD
' dwCreationDisposition : DWORD
' pCreateExParams : CREATEFILE3_EXTENDED_PARAMETERS* optional
Declare PtrSafe Function CreateFile3 Lib "kernel32" ( _
ByVal lpFileName As LongPtr, _
ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, _
ByVal dwCreationDisposition As Long, _
ByVal pCreateExParams As LongPtr) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
CreateFile3 = ctypes.windll.kernel32.CreateFile3
CreateFile3.restype = ctypes.c_void_p
CreateFile3.argtypes = [
wintypes.LPCWSTR, # lpFileName : LPCWSTR
wintypes.DWORD, # dwDesiredAccess : DWORD
wintypes.DWORD, # dwShareMode : DWORD
wintypes.DWORD, # dwCreationDisposition : DWORD
ctypes.c_void_p, # pCreateExParams : CREATEFILE3_EXTENDED_PARAMETERS* optional
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('KERNEL32.dll')
CreateFile3 = Fiddle::Function.new(
lib['CreateFile3'],
[
Fiddle::TYPE_VOIDP, # lpFileName : LPCWSTR
-Fiddle::TYPE_INT, # dwDesiredAccess : DWORD
-Fiddle::TYPE_INT, # dwShareMode : DWORD
-Fiddle::TYPE_INT, # dwCreationDisposition : DWORD
Fiddle::TYPE_VOIDP, # pCreateExParams : CREATEFILE3_EXTENDED_PARAMETERS* optional
],
Fiddle::TYPE_VOIDP)#[link(name = "kernel32")]
extern "system" {
fn CreateFile3(
lpFileName: *const u16, // LPCWSTR
dwDesiredAccess: u32, // DWORD
dwShareMode: u32, // DWORD
dwCreationDisposition: u32, // DWORD
pCreateExParams: *mut CREATEFILE3_EXTENDED_PARAMETERS // CREATEFILE3_EXTENDED_PARAMETERS* optional
) -> *mut core::ffi::c_void;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("KERNEL32.dll")]
public static extern IntPtr CreateFile3([MarshalAs(UnmanagedType.LPWStr)] string lpFileName, uint dwDesiredAccess, uint dwShareMode, uint dwCreationDisposition, IntPtr pCreateExParams);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_CreateFile3' -Namespace Win32 -PassThru
# $api::CreateFile3(lpFileName, dwDesiredAccess, dwShareMode, dwCreationDisposition, pCreateExParams)#uselib "KERNEL32.dll"
#func global CreateFile3 "CreateFile3" sptr, sptr, sptr, sptr, sptr
; CreateFile3 lpFileName, dwDesiredAccess, dwShareMode, dwCreationDisposition, varptr(pCreateExParams) ; 戻り値は stat
; lpFileName : LPCWSTR -> "sptr"
; dwDesiredAccess : DWORD -> "sptr"
; dwShareMode : DWORD -> "sptr"
; dwCreationDisposition : DWORD -> "sptr"
; pCreateExParams : CREATEFILE3_EXTENDED_PARAMETERS* optional -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "KERNEL32.dll" #cfunc global CreateFile3 "CreateFile3" wstr, int, int, int, var ; res = CreateFile3(lpFileName, dwDesiredAccess, dwShareMode, dwCreationDisposition, pCreateExParams) ; lpFileName : LPCWSTR -> "wstr" ; dwDesiredAccess : DWORD -> "int" ; dwShareMode : DWORD -> "int" ; dwCreationDisposition : DWORD -> "int" ; pCreateExParams : CREATEFILE3_EXTENDED_PARAMETERS* optional -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "KERNEL32.dll" #cfunc global CreateFile3 "CreateFile3" wstr, int, int, int, sptr ; res = CreateFile3(lpFileName, dwDesiredAccess, dwShareMode, dwCreationDisposition, varptr(pCreateExParams)) ; lpFileName : LPCWSTR -> "wstr" ; dwDesiredAccess : DWORD -> "int" ; dwShareMode : DWORD -> "int" ; dwCreationDisposition : DWORD -> "int" ; pCreateExParams : CREATEFILE3_EXTENDED_PARAMETERS* optional -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; HANDLE CreateFile3(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, DWORD dwCreationDisposition, CREATEFILE3_EXTENDED_PARAMETERS* pCreateExParams) #uselib "KERNEL32.dll" #cfunc global CreateFile3 "CreateFile3" wstr, int, int, int, var ; res = CreateFile3(lpFileName, dwDesiredAccess, dwShareMode, dwCreationDisposition, pCreateExParams) ; lpFileName : LPCWSTR -> "wstr" ; dwDesiredAccess : DWORD -> "int" ; dwShareMode : DWORD -> "int" ; dwCreationDisposition : DWORD -> "int" ; pCreateExParams : CREATEFILE3_EXTENDED_PARAMETERS* optional -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; HANDLE CreateFile3(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, DWORD dwCreationDisposition, CREATEFILE3_EXTENDED_PARAMETERS* pCreateExParams) #uselib "KERNEL32.dll" #cfunc global CreateFile3 "CreateFile3" wstr, int, int, int, intptr ; res = CreateFile3(lpFileName, dwDesiredAccess, dwShareMode, dwCreationDisposition, varptr(pCreateExParams)) ; lpFileName : LPCWSTR -> "wstr" ; dwDesiredAccess : DWORD -> "int" ; dwShareMode : DWORD -> "int" ; dwCreationDisposition : DWORD -> "int" ; pCreateExParams : CREATEFILE3_EXTENDED_PARAMETERS* optional -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
procCreateFile3 = kernel32.NewProc("CreateFile3")
)
// lpFileName (LPCWSTR), dwDesiredAccess (DWORD), dwShareMode (DWORD), dwCreationDisposition (DWORD), pCreateExParams (CREATEFILE3_EXTENDED_PARAMETERS* optional)
r1, _, err := procCreateFile3.Call(
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpFileName))),
uintptr(dwDesiredAccess),
uintptr(dwShareMode),
uintptr(dwCreationDisposition),
uintptr(pCreateExParams),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // HANDLEfunction CreateFile3(
lpFileName: PWideChar; // LPCWSTR
dwDesiredAccess: DWORD; // DWORD
dwShareMode: DWORD; // DWORD
dwCreationDisposition: DWORD; // DWORD
pCreateExParams: Pointer // CREATEFILE3_EXTENDED_PARAMETERS* optional
): THandle; stdcall;
external 'KERNEL32.dll' name 'CreateFile3';result := DllCall("KERNEL32\CreateFile3"
, "WStr", lpFileName ; LPCWSTR
, "UInt", dwDesiredAccess ; DWORD
, "UInt", dwShareMode ; DWORD
, "UInt", dwCreationDisposition ; DWORD
, "Ptr", pCreateExParams ; CREATEFILE3_EXTENDED_PARAMETERS* optional
, "Ptr") ; return: HANDLE●CreateFile3(lpFileName, dwDesiredAccess, dwShareMode, dwCreationDisposition, pCreateExParams) = DLL("KERNEL32.dll", "void* CreateFile3(char*, dword, dword, dword, void*)")
# 呼び出し: CreateFile3(lpFileName, dwDesiredAccess, dwShareMode, dwCreationDisposition, pCreateExParams)
# lpFileName : LPCWSTR -> "char*"
# dwDesiredAccess : DWORD -> "dword"
# dwShareMode : DWORD -> "dword"
# dwCreationDisposition : DWORD -> "dword"
# pCreateExParams : CREATEFILE3_EXTENDED_PARAMETERS* optional -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "kernel32" fn CreateFile3(
lpFileName: [*c]const u16, // LPCWSTR
dwDesiredAccess: u32, // DWORD
dwShareMode: u32, // DWORD
dwCreationDisposition: u32, // DWORD
pCreateExParams: [*c]CREATEFILE3_EXTENDED_PARAMETERS // CREATEFILE3_EXTENDED_PARAMETERS* optional
) callconv(std.os.windows.WINAPI) ?*anyopaque;proc CreateFile3(
lpFileName: WideCString, # LPCWSTR
dwDesiredAccess: uint32, # DWORD
dwShareMode: uint32, # DWORD
dwCreationDisposition: uint32, # DWORD
pCreateExParams: ptr CREATEFILE3_EXTENDED_PARAMETERS # CREATEFILE3_EXTENDED_PARAMETERS* optional
): pointer {.importc: "CreateFile3", stdcall, dynlib: "KERNEL32.dll".}pragma(lib, "kernel32");
extern(Windows)
void* CreateFile3(
const(wchar)* lpFileName, // LPCWSTR
uint dwDesiredAccess, // DWORD
uint dwShareMode, // DWORD
uint dwCreationDisposition, // DWORD
CREATEFILE3_EXTENDED_PARAMETERS* pCreateExParams // CREATEFILE3_EXTENDED_PARAMETERS* optional
);ccall((:CreateFile3, "KERNEL32.dll"), stdcall, Ptr{Cvoid},
(Cwstring, UInt32, UInt32, UInt32, Ptr{CREATEFILE3_EXTENDED_PARAMETERS}),
lpFileName, dwDesiredAccess, dwShareMode, dwCreationDisposition, pCreateExParams)
# lpFileName : LPCWSTR -> Cwstring
# dwDesiredAccess : DWORD -> UInt32
# dwShareMode : DWORD -> UInt32
# dwCreationDisposition : DWORD -> UInt32
# pCreateExParams : CREATEFILE3_EXTENDED_PARAMETERS* optional -> Ptr{CREATEFILE3_EXTENDED_PARAMETERS}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
void* CreateFile3(
const uint16_t* lpFileName,
uint32_t dwDesiredAccess,
uint32_t dwShareMode,
uint32_t dwCreationDisposition,
void* pCreateExParams);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.CreateFile3(lpFileName, dwDesiredAccess, dwShareMode, dwCreationDisposition, pCreateExParams)
-- lpFileName : LPCWSTR
-- dwDesiredAccess : DWORD
-- dwShareMode : DWORD
-- dwCreationDisposition : DWORD
-- pCreateExParams : CREATEFILE3_EXTENDED_PARAMETERS* optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const CreateFile3 = lib.func('__stdcall', 'CreateFile3', 'void *', ['str16', 'uint32_t', 'uint32_t', 'uint32_t', 'void *']);
// CreateFile3(lpFileName, dwDesiredAccess, dwShareMode, dwCreationDisposition, pCreateExParams)
// lpFileName : LPCWSTR -> 'str16'
// dwDesiredAccess : DWORD -> 'uint32_t'
// dwShareMode : DWORD -> 'uint32_t'
// dwCreationDisposition : DWORD -> 'uint32_t'
// pCreateExParams : CREATEFILE3_EXTENDED_PARAMETERS* optional -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("KERNEL32.dll", {
CreateFile3: { parameters: ["buffer", "u32", "u32", "u32", "pointer"], result: "pointer" },
});
// lib.symbols.CreateFile3(lpFileName, dwDesiredAccess, dwShareMode, dwCreationDisposition, pCreateExParams)
// lpFileName : LPCWSTR -> "buffer"
// dwDesiredAccess : DWORD -> "u32"
// dwShareMode : DWORD -> "u32"
// dwCreationDisposition : DWORD -> "u32"
// pCreateExParams : CREATEFILE3_EXTENDED_PARAMETERS* optional -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
void* CreateFile3(
const uint16_t* lpFileName,
uint32_t dwDesiredAccess,
uint32_t dwShareMode,
uint32_t dwCreationDisposition,
void* pCreateExParams);
C, "KERNEL32.dll");
// $ffi->CreateFile3(lpFileName, dwDesiredAccess, dwShareMode, dwCreationDisposition, pCreateExParams);
// lpFileName : LPCWSTR
// dwDesiredAccess : DWORD
// dwShareMode : DWORD
// dwCreationDisposition : DWORD
// pCreateExParams : CREATEFILE3_EXTENDED_PARAMETERS* 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);
Pointer CreateFile3(
WString lpFileName, // LPCWSTR
int dwDesiredAccess, // DWORD
int dwShareMode, // DWORD
int dwCreationDisposition, // DWORD
Pointer pCreateExParams // CREATEFILE3_EXTENDED_PARAMETERS* optional
);
}@[Link("kernel32")]
lib LibKERNEL32
fun CreateFile3 = CreateFile3(
lpFileName : UInt16*, # LPCWSTR
dwDesiredAccess : UInt32, # DWORD
dwShareMode : UInt32, # DWORD
dwCreationDisposition : UInt32, # DWORD
pCreateExParams : CREATEFILE3_EXTENDED_PARAMETERS* # CREATEFILE3_EXTENDED_PARAMETERS* optional
) : Void*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef CreateFile3Native = Pointer<Void> Function(Pointer<Utf16>, Uint32, Uint32, Uint32, Pointer<Void>);
typedef CreateFile3Dart = Pointer<Void> Function(Pointer<Utf16>, int, int, int, Pointer<Void>);
final CreateFile3 = DynamicLibrary.open('KERNEL32.dll')
.lookupFunction<CreateFile3Native, CreateFile3Dart>('CreateFile3');
// lpFileName : LPCWSTR -> Pointer<Utf16>
// dwDesiredAccess : DWORD -> Uint32
// dwShareMode : DWORD -> Uint32
// dwCreationDisposition : DWORD -> Uint32
// pCreateExParams : CREATEFILE3_EXTENDED_PARAMETERS* optional -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function CreateFile3(
lpFileName: PWideChar; // LPCWSTR
dwDesiredAccess: DWORD; // DWORD
dwShareMode: DWORD; // DWORD
dwCreationDisposition: DWORD; // DWORD
pCreateExParams: Pointer // CREATEFILE3_EXTENDED_PARAMETERS* optional
): THandle; stdcall;
external 'KERNEL32.dll' name 'CreateFile3';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "CreateFile3"
c_CreateFile3 :: CWString -> Word32 -> Word32 -> Word32 -> Ptr () -> IO (Ptr ())
-- lpFileName : LPCWSTR -> CWString
-- dwDesiredAccess : DWORD -> Word32
-- dwShareMode : DWORD -> Word32
-- dwCreationDisposition : DWORD -> Word32
-- pCreateExParams : CREATEFILE3_EXTENDED_PARAMETERS* optional -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let createfile3 =
foreign "CreateFile3"
((ptr uint16_t) @-> uint32_t @-> uint32_t @-> uint32_t @-> (ptr void) @-> returning (ptr void))
(* lpFileName : LPCWSTR -> (ptr uint16_t) *)
(* dwDesiredAccess : DWORD -> uint32_t *)
(* dwShareMode : DWORD -> uint32_t *)
(* dwCreationDisposition : DWORD -> uint32_t *)
(* pCreateExParams : CREATEFILE3_EXTENDED_PARAMETERS* optional -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)
(cffi:defcfun ("CreateFile3" create-file3 :convention :stdcall) :pointer
(lp-file-name (:string :encoding :utf-16le)) ; LPCWSTR
(dw-desired-access :uint32) ; DWORD
(dw-share-mode :uint32) ; DWORD
(dw-creation-disposition :uint32) ; DWORD
(p-create-ex-params :pointer)) ; CREATEFILE3_EXTENDED_PARAMETERS* optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $CreateFile3 = Win32::API::More->new('KERNEL32',
'HANDLE CreateFile3(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, DWORD dwCreationDisposition, LPVOID pCreateExParams)');
# my $ret = $CreateFile3->Call($lpFileName, $dwDesiredAccess, $dwShareMode, $dwCreationDisposition, $pCreateExParams);
# lpFileName : LPCWSTR -> LPCWSTR
# dwDesiredAccess : DWORD -> DWORD
# dwShareMode : DWORD -> DWORD
# dwCreationDisposition : DWORD -> DWORD
# pCreateExParams : CREATEFILE3_EXTENDED_PARAMETERS* optional -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
類似 API
- f CreateFile2 — 拡張パラメータを指定してファイルを作成または開く。
- f CreateFileW — ファイルやデバイスを作成または開いてハンドルを返す。