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