ホーム › Storage.FileSystem › OpenFile
OpenFile
関数ファイルを作成・開く・存在確認する古いファイル操作関数。
シグネチャ
// KERNEL32.dll
#include <windows.h>
INT OpenFile(
LPCSTR lpFileName,
OFSTRUCT* lpReOpenBuff,
DWORD uStyle
);パラメーター
| 名前 | 型 | 方向 |
|---|---|---|
| lpFileName | LPCSTR | in |
| lpReOpenBuff | OFSTRUCT* | inout |
| uStyle | DWORD | in |
戻り値の型: INT
各言語での呼び出し定義
// KERNEL32.dll
#include <windows.h>
INT OpenFile(
LPCSTR lpFileName,
OFSTRUCT* lpReOpenBuff,
DWORD uStyle
);[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern int OpenFile(
[MarshalAs(UnmanagedType.LPStr)] string lpFileName, // LPCSTR
IntPtr lpReOpenBuff, // OFSTRUCT* in/out
uint uStyle // DWORD
);<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function OpenFile(
<MarshalAs(UnmanagedType.LPStr)> lpFileName As String, ' LPCSTR
lpReOpenBuff As IntPtr, ' OFSTRUCT* in/out
uStyle As UInteger ' DWORD
) As Integer
End Function' lpFileName : LPCSTR
' lpReOpenBuff : OFSTRUCT* in/out
' uStyle : DWORD
Declare PtrSafe Function OpenFile Lib "kernel32" ( _
ByVal lpFileName As String, _
ByVal lpReOpenBuff As LongPtr, _
ByVal uStyle As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
OpenFile = ctypes.windll.kernel32.OpenFile
OpenFile.restype = ctypes.c_int
OpenFile.argtypes = [
wintypes.LPCSTR, # lpFileName : LPCSTR
ctypes.c_void_p, # lpReOpenBuff : OFSTRUCT* in/out
wintypes.DWORD, # uStyle : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('KERNEL32.dll')
OpenFile = Fiddle::Function.new(
lib['OpenFile'],
[
Fiddle::TYPE_VOIDP, # lpFileName : LPCSTR
Fiddle::TYPE_VOIDP, # lpReOpenBuff : OFSTRUCT* in/out
-Fiddle::TYPE_INT, # uStyle : DWORD
],
Fiddle::TYPE_INT)#[link(name = "kernel32")]
extern "system" {
fn OpenFile(
lpFileName: *const u8, // LPCSTR
lpReOpenBuff: *mut OFSTRUCT, // OFSTRUCT* in/out
uStyle: u32 // DWORD
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("KERNEL32.dll", SetLastError = true)]
public static extern int OpenFile([MarshalAs(UnmanagedType.LPStr)] string lpFileName, IntPtr lpReOpenBuff, uint uStyle);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_OpenFile' -Namespace Win32 -PassThru
# $api::OpenFile(lpFileName, lpReOpenBuff, uStyle)#uselib "KERNEL32.dll"
#func global OpenFile "OpenFile" sptr, sptr, sptr
; OpenFile lpFileName, varptr(lpReOpenBuff), uStyle ; 戻り値は stat
; lpFileName : LPCSTR -> "sptr"
; lpReOpenBuff : OFSTRUCT* in/out -> "sptr"
; uStyle : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "KERNEL32.dll" #cfunc global OpenFile "OpenFile" str, var, int ; res = OpenFile(lpFileName, lpReOpenBuff, uStyle) ; lpFileName : LPCSTR -> "str" ; lpReOpenBuff : OFSTRUCT* in/out -> "var" ; uStyle : DWORD -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "KERNEL32.dll" #cfunc global OpenFile "OpenFile" str, sptr, int ; res = OpenFile(lpFileName, varptr(lpReOpenBuff), uStyle) ; lpFileName : LPCSTR -> "str" ; lpReOpenBuff : OFSTRUCT* in/out -> "sptr" ; uStyle : DWORD -> "int" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; INT OpenFile(LPCSTR lpFileName, OFSTRUCT* lpReOpenBuff, DWORD uStyle) #uselib "KERNEL32.dll" #cfunc global OpenFile "OpenFile" str, var, int ; res = OpenFile(lpFileName, lpReOpenBuff, uStyle) ; lpFileName : LPCSTR -> "str" ; lpReOpenBuff : OFSTRUCT* in/out -> "var" ; uStyle : DWORD -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; INT OpenFile(LPCSTR lpFileName, OFSTRUCT* lpReOpenBuff, DWORD uStyle) #uselib "KERNEL32.dll" #cfunc global OpenFile "OpenFile" str, intptr, int ; res = OpenFile(lpFileName, varptr(lpReOpenBuff), uStyle) ; lpFileName : LPCSTR -> "str" ; lpReOpenBuff : OFSTRUCT* in/out -> "intptr" ; uStyle : DWORD -> "int" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
procOpenFile = kernel32.NewProc("OpenFile")
)
// lpFileName (LPCSTR), lpReOpenBuff (OFSTRUCT* in/out), uStyle (DWORD)
r1, _, err := procOpenFile.Call(
uintptr(unsafe.Pointer(windows.BytePtrFromString(lpFileName))),
uintptr(lpReOpenBuff),
uintptr(uStyle),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // INTfunction OpenFile(
lpFileName: PAnsiChar; // LPCSTR
lpReOpenBuff: Pointer; // OFSTRUCT* in/out
uStyle: DWORD // DWORD
): Integer; stdcall;
external 'KERNEL32.dll' name 'OpenFile';result := DllCall("KERNEL32\OpenFile"
, "AStr", lpFileName ; LPCSTR
, "Ptr", lpReOpenBuff ; OFSTRUCT* in/out
, "UInt", uStyle ; DWORD
, "Int") ; return: INT●OpenFile(lpFileName, lpReOpenBuff, uStyle) = DLL("KERNEL32.dll", "int OpenFile(char*, void*, dword)")
# 呼び出し: OpenFile(lpFileName, lpReOpenBuff, uStyle)
# lpFileName : LPCSTR -> "char*"
# lpReOpenBuff : OFSTRUCT* in/out -> "void*"
# uStyle : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。