Win32 API 日本語リファレンス
ホームStorage.FileSystem › ReadFileEx

ReadFileEx

関数
完了ルーチンを使い非同期でファイルを読み込む。
DLLKERNEL32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL ReadFileEx(
    HANDLE hFile,
    BYTE* lpBuffer,   // optional
    DWORD nNumberOfBytesToRead,
    OVERLAPPED* lpOverlapped,
    LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
);

パラメーター

名前方向
hFileHANDLEin
lpBufferBYTE*outoptional
nNumberOfBytesToReadDWORDin
lpOverlappedOVERLAPPED*inout
lpCompletionRoutineLPOVERLAPPED_COMPLETION_ROUTINEin

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL ReadFileEx(
    HANDLE hFile,
    BYTE* lpBuffer,   // optional
    DWORD nNumberOfBytesToRead,
    OVERLAPPED* lpOverlapped,
    LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool ReadFileEx(
    IntPtr hFile,   // HANDLE
    IntPtr lpBuffer,   // BYTE* optional, out
    uint nNumberOfBytesToRead,   // DWORD
    IntPtr lpOverlapped,   // OVERLAPPED* in/out
    IntPtr lpCompletionRoutine   // LPOVERLAPPED_COMPLETION_ROUTINE
);
<DllImport("KERNEL32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function ReadFileEx(
    hFile As IntPtr,   ' HANDLE
    lpBuffer As IntPtr,   ' BYTE* optional, out
    nNumberOfBytesToRead As UInteger,   ' DWORD
    lpOverlapped As IntPtr,   ' OVERLAPPED* in/out
    lpCompletionRoutine As IntPtr   ' LPOVERLAPPED_COMPLETION_ROUTINE
) As Boolean
End Function
' hFile : HANDLE
' lpBuffer : BYTE* optional, out
' nNumberOfBytesToRead : DWORD
' lpOverlapped : OVERLAPPED* in/out
' lpCompletionRoutine : LPOVERLAPPED_COMPLETION_ROUTINE
Declare PtrSafe Function ReadFileEx Lib "kernel32" ( _
    ByVal hFile As LongPtr, _
    ByVal lpBuffer As LongPtr, _
    ByVal nNumberOfBytesToRead As Long, _
    ByVal lpOverlapped As LongPtr, _
    ByVal lpCompletionRoutine As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

ReadFileEx = ctypes.windll.kernel32.ReadFileEx
ReadFileEx.restype = wintypes.BOOL
ReadFileEx.argtypes = [
    wintypes.HANDLE,  # hFile : HANDLE
    ctypes.POINTER(ctypes.c_ubyte),  # lpBuffer : BYTE* optional, out
    wintypes.DWORD,  # nNumberOfBytesToRead : DWORD
    ctypes.c_void_p,  # lpOverlapped : OVERLAPPED* in/out
    ctypes.c_void_p,  # lpCompletionRoutine : LPOVERLAPPED_COMPLETION_ROUTINE
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
ReadFileEx = Fiddle::Function.new(
  lib['ReadFileEx'],
  [
    Fiddle::TYPE_VOIDP,  # hFile : HANDLE
    Fiddle::TYPE_VOIDP,  # lpBuffer : BYTE* optional, out
    -Fiddle::TYPE_INT,  # nNumberOfBytesToRead : DWORD
    Fiddle::TYPE_VOIDP,  # lpOverlapped : OVERLAPPED* in/out
    Fiddle::TYPE_VOIDP,  # lpCompletionRoutine : LPOVERLAPPED_COMPLETION_ROUTINE
  ],
  Fiddle::TYPE_INT)
#[link(name = "kernel32")]
extern "system" {
    fn ReadFileEx(
        hFile: *mut core::ffi::c_void,  // HANDLE
        lpBuffer: *mut u8,  // BYTE* optional, out
        nNumberOfBytesToRead: u32,  // DWORD
        lpOverlapped: *mut OVERLAPPED,  // OVERLAPPED* in/out
        lpCompletionRoutine: *const core::ffi::c_void  // LPOVERLAPPED_COMPLETION_ROUTINE
    ) -> 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 ReadFileEx(IntPtr hFile, IntPtr lpBuffer, uint nNumberOfBytesToRead, IntPtr lpOverlapped, IntPtr lpCompletionRoutine);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_ReadFileEx' -Namespace Win32 -PassThru
# $api::ReadFileEx(hFile, lpBuffer, nNumberOfBytesToRead, lpOverlapped, lpCompletionRoutine)
#uselib "KERNEL32.dll"
#func global ReadFileEx "ReadFileEx" sptr, sptr, sptr, sptr, sptr
; ReadFileEx hFile, varptr(lpBuffer), nNumberOfBytesToRead, varptr(lpOverlapped), lpCompletionRoutine   ; 戻り値は stat
; hFile : HANDLE -> "sptr"
; lpBuffer : BYTE* optional, out -> "sptr"
; nNumberOfBytesToRead : DWORD -> "sptr"
; lpOverlapped : OVERLAPPED* in/out -> "sptr"
; lpCompletionRoutine : LPOVERLAPPED_COMPLETION_ROUTINE -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "KERNEL32.dll"
#cfunc global ReadFileEx "ReadFileEx" sptr, var, int, var, sptr
; res = ReadFileEx(hFile, lpBuffer, nNumberOfBytesToRead, lpOverlapped, lpCompletionRoutine)
; hFile : HANDLE -> "sptr"
; lpBuffer : BYTE* optional, out -> "var"
; nNumberOfBytesToRead : DWORD -> "int"
; lpOverlapped : OVERLAPPED* in/out -> "var"
; lpCompletionRoutine : LPOVERLAPPED_COMPLETION_ROUTINE -> "sptr"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL ReadFileEx(HANDLE hFile, BYTE* lpBuffer, DWORD nNumberOfBytesToRead, OVERLAPPED* lpOverlapped, LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
#uselib "KERNEL32.dll"
#cfunc global ReadFileEx "ReadFileEx" intptr, var, int, var, intptr
; res = ReadFileEx(hFile, lpBuffer, nNumberOfBytesToRead, lpOverlapped, lpCompletionRoutine)
; hFile : HANDLE -> "intptr"
; lpBuffer : BYTE* optional, out -> "var"
; nNumberOfBytesToRead : DWORD -> "int"
; lpOverlapped : OVERLAPPED* in/out -> "var"
; lpCompletionRoutine : LPOVERLAPPED_COMPLETION_ROUTINE -> "intptr"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procReadFileEx = kernel32.NewProc("ReadFileEx")
)

// hFile (HANDLE), lpBuffer (BYTE* optional, out), nNumberOfBytesToRead (DWORD), lpOverlapped (OVERLAPPED* in/out), lpCompletionRoutine (LPOVERLAPPED_COMPLETION_ROUTINE)
r1, _, err := procReadFileEx.Call(
	uintptr(hFile),
	uintptr(lpBuffer),
	uintptr(nNumberOfBytesToRead),
	uintptr(lpOverlapped),
	uintptr(lpCompletionRoutine),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function ReadFileEx(
  hFile: THandle;   // HANDLE
  lpBuffer: Pointer;   // BYTE* optional, out
  nNumberOfBytesToRead: DWORD;   // DWORD
  lpOverlapped: Pointer;   // OVERLAPPED* in/out
  lpCompletionRoutine: Pointer   // LPOVERLAPPED_COMPLETION_ROUTINE
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'ReadFileEx';
result := DllCall("KERNEL32\ReadFileEx"
    , "Ptr", hFile   ; HANDLE
    , "Ptr", lpBuffer   ; BYTE* optional, out
    , "UInt", nNumberOfBytesToRead   ; DWORD
    , "Ptr", lpOverlapped   ; OVERLAPPED* in/out
    , "Ptr", lpCompletionRoutine   ; LPOVERLAPPED_COMPLETION_ROUTINE
    , "Int")   ; return: BOOL
●ReadFileEx(hFile, lpBuffer, nNumberOfBytesToRead, lpOverlapped, lpCompletionRoutine) = DLL("KERNEL32.dll", "bool ReadFileEx(void*, void*, dword, void*, void*)")
# 呼び出し: ReadFileEx(hFile, lpBuffer, nNumberOfBytesToRead, lpOverlapped, lpCompletionRoutine)
# hFile : HANDLE -> "void*"
# lpBuffer : BYTE* optional, out -> "void*"
# nNumberOfBytesToRead : DWORD -> "dword"
# lpOverlapped : OVERLAPPED* in/out -> "void*"
# lpCompletionRoutine : LPOVERLAPPED_COMPLETION_ROUTINE -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。