ホーム › Storage.FileSystem › WriteFile
WriteFile
関数ファイルやデバイスにデータを書き込む。
シグネチャ
// KERNEL32.dll
#include <windows.h>
BOOL WriteFile(
HANDLE hFile,
const BYTE* lpBuffer, // optional
DWORD nNumberOfBytesToWrite,
DWORD* lpNumberOfBytesWritten, // optional
OVERLAPPED* lpOverlapped // optional
);パラメーター
| 名前 | 型 | 方向 |
|---|---|---|
| hFile | HANDLE | in |
| lpBuffer | BYTE* | inoptional |
| nNumberOfBytesToWrite | DWORD | in |
| lpNumberOfBytesWritten | DWORD* | outoptional |
| lpOverlapped | OVERLAPPED* | inoutoptional |
戻り値の型: BOOL
各言語での呼び出し定義
// KERNEL32.dll
#include <windows.h>
BOOL WriteFile(
HANDLE hFile,
const BYTE* lpBuffer, // optional
DWORD nNumberOfBytesToWrite,
DWORD* lpNumberOfBytesWritten, // optional
OVERLAPPED* lpOverlapped // optional
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool WriteFile(
IntPtr hFile, // HANDLE
IntPtr lpBuffer, // BYTE* optional
uint nNumberOfBytesToWrite, // DWORD
IntPtr lpNumberOfBytesWritten, // DWORD* optional, out
IntPtr lpOverlapped // OVERLAPPED* optional, in/out
);<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function WriteFile(
hFile As IntPtr, ' HANDLE
lpBuffer As IntPtr, ' BYTE* optional
nNumberOfBytesToWrite As UInteger, ' DWORD
lpNumberOfBytesWritten As IntPtr, ' DWORD* optional, out
lpOverlapped As IntPtr ' OVERLAPPED* optional, in/out
) As Boolean
End Function' hFile : HANDLE
' lpBuffer : BYTE* optional
' nNumberOfBytesToWrite : DWORD
' lpNumberOfBytesWritten : DWORD* optional, out
' lpOverlapped : OVERLAPPED* optional, in/out
Declare PtrSafe Function WriteFile Lib "kernel32" ( _
ByVal hFile As LongPtr, _
ByVal lpBuffer As LongPtr, _
ByVal nNumberOfBytesToWrite As Long, _
ByVal lpNumberOfBytesWritten As LongPtr, _
ByVal lpOverlapped As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
WriteFile = ctypes.windll.kernel32.WriteFile
WriteFile.restype = wintypes.BOOL
WriteFile.argtypes = [
wintypes.HANDLE, # hFile : HANDLE
ctypes.POINTER(ctypes.c_ubyte), # lpBuffer : BYTE* optional
wintypes.DWORD, # nNumberOfBytesToWrite : DWORD
ctypes.POINTER(wintypes.DWORD), # lpNumberOfBytesWritten : DWORD* optional, out
ctypes.c_void_p, # lpOverlapped : OVERLAPPED* optional, in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('KERNEL32.dll')
WriteFile = Fiddle::Function.new(
lib['WriteFile'],
[
Fiddle::TYPE_VOIDP, # hFile : HANDLE
Fiddle::TYPE_VOIDP, # lpBuffer : BYTE* optional
-Fiddle::TYPE_INT, # nNumberOfBytesToWrite : DWORD
Fiddle::TYPE_VOIDP, # lpNumberOfBytesWritten : DWORD* optional, out
Fiddle::TYPE_VOIDP, # lpOverlapped : OVERLAPPED* optional, in/out
],
Fiddle::TYPE_INT)#[link(name = "kernel32")]
extern "system" {
fn WriteFile(
hFile: *mut core::ffi::c_void, // HANDLE
lpBuffer: *const u8, // BYTE* optional
nNumberOfBytesToWrite: u32, // DWORD
lpNumberOfBytesWritten: *mut u32, // DWORD* optional, out
lpOverlapped: *mut OVERLAPPED // OVERLAPPED* optional, in/out
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", SetLastError = true)]
public static extern bool WriteFile(IntPtr hFile, IntPtr lpBuffer, uint nNumberOfBytesToWrite, IntPtr lpNumberOfBytesWritten, IntPtr lpOverlapped);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_WriteFile' -Namespace Win32 -PassThru
# $api::WriteFile(hFile, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten, lpOverlapped)#uselib "KERNEL32.dll"
#func global WriteFile "WriteFile" sptr, sptr, sptr, sptr, sptr
; WriteFile hFile, varptr(lpBuffer), nNumberOfBytesToWrite, varptr(lpNumberOfBytesWritten), varptr(lpOverlapped) ; 戻り値は stat
; hFile : HANDLE -> "sptr"
; lpBuffer : BYTE* optional -> "sptr"
; nNumberOfBytesToWrite : DWORD -> "sptr"
; lpNumberOfBytesWritten : DWORD* optional, out -> "sptr"
; lpOverlapped : OVERLAPPED* optional, in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "KERNEL32.dll" #cfunc global WriteFile "WriteFile" sptr, var, int, var, var ; res = WriteFile(hFile, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten, lpOverlapped) ; hFile : HANDLE -> "sptr" ; lpBuffer : BYTE* optional -> "var" ; nNumberOfBytesToWrite : DWORD -> "int" ; lpNumberOfBytesWritten : DWORD* optional, out -> "var" ; lpOverlapped : OVERLAPPED* optional, in/out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "KERNEL32.dll" #cfunc global WriteFile "WriteFile" sptr, sptr, int, sptr, sptr ; res = WriteFile(hFile, varptr(lpBuffer), nNumberOfBytesToWrite, varptr(lpNumberOfBytesWritten), varptr(lpOverlapped)) ; hFile : HANDLE -> "sptr" ; lpBuffer : BYTE* optional -> "sptr" ; nNumberOfBytesToWrite : DWORD -> "int" ; lpNumberOfBytesWritten : DWORD* optional, out -> "sptr" ; lpOverlapped : OVERLAPPED* optional, in/out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; BOOL WriteFile(HANDLE hFile, BYTE* lpBuffer, DWORD nNumberOfBytesToWrite, DWORD* lpNumberOfBytesWritten, OVERLAPPED* lpOverlapped) #uselib "KERNEL32.dll" #cfunc global WriteFile "WriteFile" intptr, var, int, var, var ; res = WriteFile(hFile, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten, lpOverlapped) ; hFile : HANDLE -> "intptr" ; lpBuffer : BYTE* optional -> "var" ; nNumberOfBytesToWrite : DWORD -> "int" ; lpNumberOfBytesWritten : DWORD* optional, out -> "var" ; lpOverlapped : OVERLAPPED* optional, in/out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; BOOL WriteFile(HANDLE hFile, BYTE* lpBuffer, DWORD nNumberOfBytesToWrite, DWORD* lpNumberOfBytesWritten, OVERLAPPED* lpOverlapped) #uselib "KERNEL32.dll" #cfunc global WriteFile "WriteFile" intptr, intptr, int, intptr, intptr ; res = WriteFile(hFile, varptr(lpBuffer), nNumberOfBytesToWrite, varptr(lpNumberOfBytesWritten), varptr(lpOverlapped)) ; hFile : HANDLE -> "intptr" ; lpBuffer : BYTE* optional -> "intptr" ; nNumberOfBytesToWrite : DWORD -> "int" ; lpNumberOfBytesWritten : DWORD* optional, out -> "intptr" ; lpOverlapped : OVERLAPPED* optional, in/out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
procWriteFile = kernel32.NewProc("WriteFile")
)
// hFile (HANDLE), lpBuffer (BYTE* optional), nNumberOfBytesToWrite (DWORD), lpNumberOfBytesWritten (DWORD* optional, out), lpOverlapped (OVERLAPPED* optional, in/out)
r1, _, err := procWriteFile.Call(
uintptr(hFile),
uintptr(lpBuffer),
uintptr(nNumberOfBytesToWrite),
uintptr(lpNumberOfBytesWritten),
uintptr(lpOverlapped),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction WriteFile(
hFile: THandle; // HANDLE
lpBuffer: Pointer; // BYTE* optional
nNumberOfBytesToWrite: DWORD; // DWORD
lpNumberOfBytesWritten: Pointer; // DWORD* optional, out
lpOverlapped: Pointer // OVERLAPPED* optional, in/out
): BOOL; stdcall;
external 'KERNEL32.dll' name 'WriteFile';result := DllCall("KERNEL32\WriteFile"
, "Ptr", hFile ; HANDLE
, "Ptr", lpBuffer ; BYTE* optional
, "UInt", nNumberOfBytesToWrite ; DWORD
, "Ptr", lpNumberOfBytesWritten ; DWORD* optional, out
, "Ptr", lpOverlapped ; OVERLAPPED* optional, in/out
, "Int") ; return: BOOL●WriteFile(hFile, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten, lpOverlapped) = DLL("KERNEL32.dll", "bool WriteFile(void*, void*, dword, void*, void*)")
# 呼び出し: WriteFile(hFile, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten, lpOverlapped)
# hFile : HANDLE -> "void*"
# lpBuffer : BYTE* optional -> "void*"
# nNumberOfBytesToWrite : DWORD -> "dword"
# lpNumberOfBytesWritten : DWORD* optional, out -> "void*"
# lpOverlapped : OVERLAPPED* optional, in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。