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

ReadDirectoryChangesExW

関数
情報クラス指定でディレクトリの変更を監視して通知を受け取る。
DLLKERNEL32.dll呼出規約winapiSetLastErrorあり対応OSWindows 10 以降

シグネチャ

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

BOOL ReadDirectoryChangesExW(
    HANDLE hDirectory,
    void* lpBuffer,
    DWORD nBufferLength,
    BOOL bWatchSubtree,
    FILE_NOTIFY_CHANGE dwNotifyFilter,
    DWORD* lpBytesReturned,   // optional
    OVERLAPPED* lpOverlapped,   // optional
    LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine,   // optional
    READ_DIRECTORY_NOTIFY_INFORMATION_CLASS ReadDirectoryNotifyInformationClass
);

パラメーター

名前方向説明
hDirectoryHANDLEin監視対象のディレクトリへのハンドル。このディレクトリは、FILE_LIST_DIRECTORY アクセス権、または FILE_LIST_DIRECTORY アクセス権を含む GENERIC_READ のようなアクセス権で開かれている必要があります。
lpBuffervoid*out

ReadDirectoryChangesExW が読み取り結果を返す、DWORD 境界にアラインされた整形済みバッファーへのポインター。このバッファーの構造は、ReadDirectoryNotifyInformationClass パラメーターの値が ReadDirectoryNotifyExtendedInformation の場合は FILE_NOTIFY_EXTENDED_INFORMATION 構造体で定義され、ReadDirectoryNotifyInformationClassReadDirectoryNotifyInformation の場合は FILE_NOTIFY_INFORMATION 構造体で定義されます。

このバッファーは、ディレクトリの開き方と lpOverlapped パラメーターに指定された値に応じて、同期的または非同期的に格納されます。詳細については、「解説」セクションを参照してください。

nBufferLengthDWORDinlpBuffer パラメーターが指すバッファーのサイズ (バイト単位)。
bWatchSubtreeBOOLinこのパラメーターが TRUE の場合、関数は指定したディレクトリをルートとするディレクトリツリーを監視します。このパラメーターが FALSE の場合、関数は hDirectory パラメーターで指定したディレクトリのみを監視します。
dwNotifyFilterFILE_NOTIFY_CHANGEin

待機操作が完了したかどうかを判定するために関数がチェックするフィルター条件。このパラメーターには、次の値のうち 1 つ以上を指定できます。

意味
FILE_NOTIFY_CHANGE_FILE_NAME
0x00000001
監視対象のディレクトリまたはサブツリー内でファイル名が変更されると、変更通知の待機操作が戻ります。変更には、ファイルの名前変更、作成、削除が含まれます。
FILE_NOTIFY_CHANGE_DIR_NAME
0x00000002
監視対象のディレクトリまたはサブツリー内でディレクトリ名が変更されると、変更通知の待機操作が戻ります。変更には、ディレクトリの作成または削除が含まれます。
FILE_NOTIFY_CHANGE_ATTRIBUTES
0x00000004
監視対象のディレクトリまたはサブツリー内で属性が変更されると、変更通知の待機操作が戻ります。
FILE_NOTIFY_CHANGE_SIZE
0x00000008
監視対象のディレクトリまたはサブツリー内でファイルサイズが変更されると、変更通知の待機操作が戻ります。オペレーティングシステムは、ファイルがディスクに書き込まれたときにのみファイルサイズの変更を検出します。大規模なキャッシュを使用するオペレーティングシステムでは、キャッシュが十分にフラッシュされたときにのみ検出が行われます。
FILE_NOTIFY_CHANGE_LAST_WRITE
0x00000010
監視対象のディレクトリまたはサブツリー内のファイルの最終書き込み時刻が変更されると、変更通知の待機操作が戻ります。オペレーティングシステムは、ファイルがディスクに書き込まれたときにのみ最終書き込み時刻の変更を検出します。大規模なキャッシュを使用するオペレーティングシステムでは、キャッシュが十分にフラッシュされたときにのみ検出が行われます。
FILE_NOTIFY_CHANGE_LAST_ACCESS
0x00000020
監視対象のディレクトリまたはサブツリー内のファイルの最終アクセス時刻が変更されると、変更通知の待機操作が戻ります。
FILE_NOTIFY_CHANGE_CREATION
0x00000040
監視対象のディレクトリまたはサブツリー内のファイルの作成時刻が変更されると、変更通知の待機操作が戻ります。
FILE_NOTIFY_CHANGE_SECURITY
0x00000100
監視対象のディレクトリまたはサブツリー内でセキュリティ記述子が変更されると、変更通知の待機操作が戻ります。
lpBytesReturnedDWORD*outoptional同期呼び出しの場合、このパラメーターは lpBuffer パラメーターに転送されたバイト数を受け取ります。非同期呼び出しの場合、このパラメーターは未定義です。転送されたバイト数を取得するには、非同期通知の手法を使用する必要があります。
lpOverlappedOVERLAPPED*inoutoptional非同期操作中に使用するデータを供給する OVERLAPPED 構造体へのポインター。それ以外の場合、この値は NULL です。この構造体の Offset メンバーおよび OffsetHigh メンバーは使用されません。
lpCompletionRoutineLPOVERLAPPED_COMPLETION_ROUTINEinoptional操作が完了またはキャンセルされ、呼び出し元スレッドがアラート可能な待機状態にあるときに呼び出される完了ルーチンへのポインター。この完了ルーチンの詳細については、FileIOCompletionRoutine を参照してください。
ReadDirectoryNotifyInformationClassREAD_DIRECTORY_NOTIFY_INFORMATION_CLASSinReadDirectoryChangesExWlpBuffer パラメーターが指すバッファーに書き込む情報の種類。情報を FILE_NOTIFY_INFORMATION 構造体で構成することを示すには ReadDirectoryNotifyInformation を、FILE_NOTIFY_EXTENDED_INFORMATION 構造体で構成することを示すには ReadDirectoryNotifyExtendedInformation を指定します。

戻り値の型: BOOL

公式ドキュメント

指定したディレクトリ内の変更を記述する情報を取得します。情報の種類が指定されている場合は、拡張情報を含めることができます。

戻り値

関数が成功した場合、戻り値は 0 以外になります。同期呼び出しの場合、これは操作が成功したことを意味します。非同期呼び出しの場合、これは操作が正常にキューに登録されたことを示します。

関数が失敗した場合、戻り値は 0 になります。拡張エラー情報を取得するには、GetLastError を呼び出します。

ネットワークリダイレクターまたは対象のファイルシステムがこの操作をサポートしていない場合、関数は ERROR_INVALID_FUNCTION で失敗します。

解説(Remarks)

ディレクトリへのハンドルを取得するには、FILE_FLAG_BACKUP_SEMANTICS フラグを指定して CreateFile 関数を使用します。

ReadDirectoryChangesExW の呼び出しは、同期的または非同期的に完了できます。非同期完了を指定するには、上記のように CreateFile でディレクトリを開きますが、加えて dwFlagsAndAttributes パラメーターに FILE_FLAG_OVERLAPPED 属性を指定します。その後、ReadDirectoryChangesExW を呼び出すときに OVERLAPPED 構造体を指定します。

ReadDirectoryChangesExW を最初に呼び出すと、システムは変更情報を格納するためのバッファーを割り当てます。このバッファーはディレクトリハンドルが閉じられるまで関連付けられ、その存続期間中にサイズは変わりません。この関数の呼び出し間に発生したディレクトリの変更はバッファーに追加され、次回の呼び出しで返されます。バッファーがオーバーフローした場合でも、ReadDirectoryChangesExWtrue を返しますが、バッファーの内容全体が破棄され、lpBytesReturned パラメーターは 0 になります。これは、発生したすべての変更を保持するにはバッファーが小さすぎたことを示します。

同期完了が成功すると、lpBuffer パラメーターは整形済みのバッファーとなり、バッファーに書き込まれたバイト数が lpBytesReturned で得られます。転送されたバイト数が 0 の場合、バッファーはシステムが割り当てるには大きすぎたか、またはディレクトリやサブツリーで発生したすべての変更に関する詳細情報を提供するには小さすぎました。この場合は、ディレクトリまたはサブツリーを列挙して変更を計算する必要があります。

非同期完了の場合、次の 3 つの方法のいずれかで通知を受け取ることができます。

詳細については、同期 I/O と非同期 I/O を参照してください。

バッファー長が 64 KB より大きく、アプリケーションがネットワーク経由でディレクトリを監視している場合、ReadDirectoryChangesExWERROR_INVALID_PARAMETER で失敗します。これは、基盤となるファイル共有プロトコルのパケットサイズ制限によるものです。

バッファーが DWORD 境界にアラインされていない場合、ReadDirectoryChangesExWERROR_NOACCESS で失敗します。

システムがディレクトリへのすべての変更を記録できなかった場合、ReadDirectoryChangesExWERROR_NOTIFY_ENUM_DIR で失敗します。この場合は、ディレクトリまたはサブツリーを列挙して変更を計算する必要があります。

ショートネームを使用してファイルを開いた場合、ショートネームに対する変更通知を受け取ることができます。

ReadDirectoryChangesExW は、現在 NTFS ファイルシステムでのみサポートされています。

トランザクション操作

ディレクトリハンドルにトランザクションがバインドされている場合、通知は該当するトランザクション分離規則に従います。
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

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

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

ReadDirectoryChangesExW = ctypes.windll.kernel32.ReadDirectoryChangesExW
ReadDirectoryChangesExW.restype = wintypes.BOOL
ReadDirectoryChangesExW.argtypes = [
    wintypes.HANDLE,  # hDirectory : HANDLE
    ctypes.POINTER(None),  # lpBuffer : void* out
    wintypes.DWORD,  # nBufferLength : DWORD
    wintypes.BOOL,  # bWatchSubtree : BOOL
    wintypes.DWORD,  # dwNotifyFilter : FILE_NOTIFY_CHANGE
    ctypes.POINTER(wintypes.DWORD),  # lpBytesReturned : DWORD* optional, out
    ctypes.c_void_p,  # lpOverlapped : OVERLAPPED* optional, in/out
    ctypes.c_void_p,  # lpCompletionRoutine : LPOVERLAPPED_COMPLETION_ROUTINE optional
    ctypes.c_int,  # ReadDirectoryNotifyInformationClass : READ_DIRECTORY_NOTIFY_INFORMATION_CLASS
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procReadDirectoryChangesExW = kernel32.NewProc("ReadDirectoryChangesExW")
)

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

extern "kernel32" fn ReadDirectoryChangesExW(
    hDirectory: ?*anyopaque, // HANDLE
    lpBuffer: ?*anyopaque, // void* out
    nBufferLength: u32, // DWORD
    bWatchSubtree: i32, // BOOL
    dwNotifyFilter: u32, // FILE_NOTIFY_CHANGE
    lpBytesReturned: [*c]u32, // DWORD* optional, out
    lpOverlapped: [*c]OVERLAPPED, // OVERLAPPED* optional, in/out
    lpCompletionRoutine: ?*anyopaque, // LPOVERLAPPED_COMPLETION_ROUTINE optional
    ReadDirectoryNotifyInformationClass: i32 // READ_DIRECTORY_NOTIFY_INFORMATION_CLASS
) callconv(std.os.windows.WINAPI) i32;
proc ReadDirectoryChangesExW(
    hDirectory: pointer,  # HANDLE
    lpBuffer: pointer,  # void* out
    nBufferLength: uint32,  # DWORD
    bWatchSubtree: int32,  # BOOL
    dwNotifyFilter: uint32,  # FILE_NOTIFY_CHANGE
    lpBytesReturned: ptr uint32,  # DWORD* optional, out
    lpOverlapped: ptr OVERLAPPED,  # OVERLAPPED* optional, in/out
    lpCompletionRoutine: pointer,  # LPOVERLAPPED_COMPLETION_ROUTINE optional
    ReadDirectoryNotifyInformationClass: int32  # READ_DIRECTORY_NOTIFY_INFORMATION_CLASS
): int32 {.importc: "ReadDirectoryChangesExW", stdcall, dynlib: "KERNEL32.dll".}
pragma(lib, "kernel32");
extern(Windows)
int ReadDirectoryChangesExW(
    void* hDirectory,   // HANDLE
    void* lpBuffer,   // void* out
    uint nBufferLength,   // DWORD
    int bWatchSubtree,   // BOOL
    uint dwNotifyFilter,   // FILE_NOTIFY_CHANGE
    uint* lpBytesReturned,   // DWORD* optional, out
    OVERLAPPED* lpOverlapped,   // OVERLAPPED* optional, in/out
    void* lpCompletionRoutine,   // LPOVERLAPPED_COMPLETION_ROUTINE optional
    int ReadDirectoryNotifyInformationClass   // READ_DIRECTORY_NOTIFY_INFORMATION_CLASS
);
ccall((:ReadDirectoryChangesExW, "KERNEL32.dll"), stdcall, Int32,
      (Ptr{Cvoid}, Ptr{Cvoid}, UInt32, Int32, UInt32, Ptr{UInt32}, Ptr{OVERLAPPED}, Ptr{Cvoid}, Int32),
      hDirectory, lpBuffer, nBufferLength, bWatchSubtree, dwNotifyFilter, lpBytesReturned, lpOverlapped, lpCompletionRoutine, ReadDirectoryNotifyInformationClass)
# hDirectory : HANDLE -> Ptr{Cvoid}
# lpBuffer : void* out -> Ptr{Cvoid}
# nBufferLength : DWORD -> UInt32
# bWatchSubtree : BOOL -> Int32
# dwNotifyFilter : FILE_NOTIFY_CHANGE -> UInt32
# lpBytesReturned : DWORD* optional, out -> Ptr{UInt32}
# lpOverlapped : OVERLAPPED* optional, in/out -> Ptr{OVERLAPPED}
# lpCompletionRoutine : LPOVERLAPPED_COMPLETION_ROUTINE optional -> Ptr{Cvoid}
# ReadDirectoryNotifyInformationClass : READ_DIRECTORY_NOTIFY_INFORMATION_CLASS -> Int32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t ReadDirectoryChangesExW(
    void* hDirectory,
    void* lpBuffer,
    uint32_t nBufferLength,
    int32_t bWatchSubtree,
    uint32_t dwNotifyFilter,
    uint32_t* lpBytesReturned,
    void* lpOverlapped,
    void* lpCompletionRoutine,
    int32_t ReadDirectoryNotifyInformationClass);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.ReadDirectoryChangesExW(hDirectory, lpBuffer, nBufferLength, bWatchSubtree, dwNotifyFilter, lpBytesReturned, lpOverlapped, lpCompletionRoutine, ReadDirectoryNotifyInformationClass)
-- hDirectory : HANDLE
-- lpBuffer : void* out
-- nBufferLength : DWORD
-- bWatchSubtree : BOOL
-- dwNotifyFilter : FILE_NOTIFY_CHANGE
-- lpBytesReturned : DWORD* optional, out
-- lpOverlapped : OVERLAPPED* optional, in/out
-- lpCompletionRoutine : LPOVERLAPPED_COMPLETION_ROUTINE optional
-- ReadDirectoryNotifyInformationClass : READ_DIRECTORY_NOTIFY_INFORMATION_CLASS
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const ReadDirectoryChangesExW = lib.func('__stdcall', 'ReadDirectoryChangesExW', 'int32_t', ['void *', 'void *', 'uint32_t', 'int32_t', 'uint32_t', 'uint32_t *', 'void *', 'void *', 'int32_t']);
// ReadDirectoryChangesExW(hDirectory, lpBuffer, nBufferLength, bWatchSubtree, dwNotifyFilter, lpBytesReturned, lpOverlapped, lpCompletionRoutine, ReadDirectoryNotifyInformationClass)
// hDirectory : HANDLE -> 'void *'
// lpBuffer : void* out -> 'void *'
// nBufferLength : DWORD -> 'uint32_t'
// bWatchSubtree : BOOL -> 'int32_t'
// dwNotifyFilter : FILE_NOTIFY_CHANGE -> 'uint32_t'
// lpBytesReturned : DWORD* optional, out -> 'uint32_t *'
// lpOverlapped : OVERLAPPED* optional, in/out -> 'void *'
// lpCompletionRoutine : LPOVERLAPPED_COMPLETION_ROUTINE optional -> 'void *'
// ReadDirectoryNotifyInformationClass : READ_DIRECTORY_NOTIFY_INFORMATION_CLASS -> 'int32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
// コールバック(関数ポインタ)は koffi.proto/koffi.register で型を定義して渡す(素の void* では JS 関数を渡せない)。
const lib = Deno.dlopen("KERNEL32.dll", {
  ReadDirectoryChangesExW: { parameters: ["pointer", "pointer", "u32", "i32", "u32", "pointer", "pointer", "pointer", "i32"], result: "i32" },
});
// lib.symbols.ReadDirectoryChangesExW(hDirectory, lpBuffer, nBufferLength, bWatchSubtree, dwNotifyFilter, lpBytesReturned, lpOverlapped, lpCompletionRoutine, ReadDirectoryNotifyInformationClass)
// hDirectory : HANDLE -> "pointer"
// lpBuffer : void* out -> "pointer"
// nBufferLength : DWORD -> "u32"
// bWatchSubtree : BOOL -> "i32"
// dwNotifyFilter : FILE_NOTIFY_CHANGE -> "u32"
// lpBytesReturned : DWORD* optional, out -> "pointer"
// lpOverlapped : OVERLAPPED* optional, in/out -> "pointer"
// lpCompletionRoutine : LPOVERLAPPED_COMPLETION_ROUTINE optional -> "pointer"
// ReadDirectoryNotifyInformationClass : READ_DIRECTORY_NOTIFY_INFORMATION_CLASS -> "i32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t ReadDirectoryChangesExW(
    void* hDirectory,
    void* lpBuffer,
    uint32_t nBufferLength,
    int32_t bWatchSubtree,
    uint32_t dwNotifyFilter,
    uint32_t* lpBytesReturned,
    void* lpOverlapped,
    void* lpCompletionRoutine,
    int32_t ReadDirectoryNotifyInformationClass);
C, "KERNEL32.dll");
// $ffi->ReadDirectoryChangesExW(hDirectory, lpBuffer, nBufferLength, bWatchSubtree, dwNotifyFilter, lpBytesReturned, lpOverlapped, lpCompletionRoutine, ReadDirectoryNotifyInformationClass);
// hDirectory : HANDLE
// lpBuffer : void* out
// nBufferLength : DWORD
// bWatchSubtree : BOOL
// dwNotifyFilter : FILE_NOTIFY_CHANGE
// lpBytesReturned : DWORD* optional, out
// lpOverlapped : OVERLAPPED* optional, in/out
// lpCompletionRoutine : LPOVERLAPPED_COMPLETION_ROUTINE optional
// ReadDirectoryNotifyInformationClass : READ_DIRECTORY_NOTIFY_INFORMATION_CLASS
// 構造体/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);
    boolean ReadDirectoryChangesExW(
        Pointer hDirectory,   // HANDLE
        Pointer lpBuffer,   // void* out
        int nBufferLength,   // DWORD
        boolean bWatchSubtree,   // BOOL
        int dwNotifyFilter,   // FILE_NOTIFY_CHANGE
        IntByReference lpBytesReturned,   // DWORD* optional, out
        Pointer lpOverlapped,   // OVERLAPPED* optional, in/out
        Callback lpCompletionRoutine,   // LPOVERLAPPED_COMPLETION_ROUTINE optional
        int ReadDirectoryNotifyInformationClass   // READ_DIRECTORY_NOTIFY_INFORMATION_CLASS
    );
}
@[Link("kernel32")]
lib LibKERNEL32
  fun ReadDirectoryChangesExW = ReadDirectoryChangesExW(
    hDirectory : Void*,   # HANDLE
    lpBuffer : Void*,   # void* out
    nBufferLength : UInt32,   # DWORD
    bWatchSubtree : Int32,   # BOOL
    dwNotifyFilter : UInt32,   # FILE_NOTIFY_CHANGE
    lpBytesReturned : UInt32*,   # DWORD* optional, out
    lpOverlapped : OVERLAPPED*,   # OVERLAPPED* optional, in/out
    lpCompletionRoutine : Void*,   # LPOVERLAPPED_COMPLETION_ROUTINE optional
    ReadDirectoryNotifyInformationClass : Int32   # READ_DIRECTORY_NOTIFY_INFORMATION_CLASS
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef ReadDirectoryChangesExWNative = Int32 Function(Pointer<Void>, Pointer<Void>, Uint32, Int32, Uint32, Pointer<Uint32>, Pointer<Void>, Pointer<Void>, Int32);
typedef ReadDirectoryChangesExWDart = int Function(Pointer<Void>, Pointer<Void>, int, int, int, Pointer<Uint32>, Pointer<Void>, Pointer<Void>, int);
final ReadDirectoryChangesExW = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<ReadDirectoryChangesExWNative, ReadDirectoryChangesExWDart>('ReadDirectoryChangesExW');
// hDirectory : HANDLE -> Pointer<Void>
// lpBuffer : void* out -> Pointer<Void>
// nBufferLength : DWORD -> Uint32
// bWatchSubtree : BOOL -> Int32
// dwNotifyFilter : FILE_NOTIFY_CHANGE -> Uint32
// lpBytesReturned : DWORD* optional, out -> Pointer<Uint32>
// lpOverlapped : OVERLAPPED* optional, in/out -> Pointer<Void>
// lpCompletionRoutine : LPOVERLAPPED_COMPLETION_ROUTINE optional -> Pointer<Void>
// ReadDirectoryNotifyInformationClass : READ_DIRECTORY_NOTIFY_INFORMATION_CLASS -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function ReadDirectoryChangesExW(
  hDirectory: THandle;   // HANDLE
  lpBuffer: Pointer;   // void* out
  nBufferLength: DWORD;   // DWORD
  bWatchSubtree: BOOL;   // BOOL
  dwNotifyFilter: DWORD;   // FILE_NOTIFY_CHANGE
  lpBytesReturned: Pointer;   // DWORD* optional, out
  lpOverlapped: Pointer;   // OVERLAPPED* optional, in/out
  lpCompletionRoutine: Pointer;   // LPOVERLAPPED_COMPLETION_ROUTINE optional
  ReadDirectoryNotifyInformationClass: Integer   // READ_DIRECTORY_NOTIFY_INFORMATION_CLASS
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'ReadDirectoryChangesExW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "ReadDirectoryChangesExW"
  c_ReadDirectoryChangesExW :: Ptr () -> Ptr () -> Word32 -> CInt -> Word32 -> Ptr Word32 -> Ptr () -> Ptr () -> Int32 -> IO CInt
-- hDirectory : HANDLE -> Ptr ()
-- lpBuffer : void* out -> Ptr ()
-- nBufferLength : DWORD -> Word32
-- bWatchSubtree : BOOL -> CInt
-- dwNotifyFilter : FILE_NOTIFY_CHANGE -> Word32
-- lpBytesReturned : DWORD* optional, out -> Ptr Word32
-- lpOverlapped : OVERLAPPED* optional, in/out -> Ptr ()
-- lpCompletionRoutine : LPOVERLAPPED_COMPLETION_ROUTINE optional -> Ptr ()
-- ReadDirectoryNotifyInformationClass : READ_DIRECTORY_NOTIFY_INFORMATION_CLASS -> Int32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let readdirectorychangesexw =
  foreign "ReadDirectoryChangesExW"
    ((ptr void) @-> (ptr void) @-> uint32_t @-> int32_t @-> uint32_t @-> (ptr uint32_t) @-> (ptr void) @-> (ptr void) @-> int32_t @-> returning int32_t)
(* hDirectory : HANDLE -> (ptr void) *)
(* lpBuffer : void* out -> (ptr void) *)
(* nBufferLength : DWORD -> uint32_t *)
(* bWatchSubtree : BOOL -> int32_t *)
(* dwNotifyFilter : FILE_NOTIFY_CHANGE -> uint32_t *)
(* lpBytesReturned : DWORD* optional, out -> (ptr uint32_t) *)
(* lpOverlapped : OVERLAPPED* optional, in/out -> (ptr void) *)
(* lpCompletionRoutine : LPOVERLAPPED_COMPLETION_ROUTINE optional -> (ptr void) *)
(* ReadDirectoryNotifyInformationClass : READ_DIRECTORY_NOTIFY_INFORMATION_CLASS -> int32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)

(cffi:defcfun ("ReadDirectoryChangesExW" read-directory-changes-ex-w :convention :stdcall) :int32
  (h-directory :pointer)   ; HANDLE
  (lp-buffer :pointer)   ; void* out
  (n-buffer-length :uint32)   ; DWORD
  (b-watch-subtree :int32)   ; BOOL
  (dw-notify-filter :uint32)   ; FILE_NOTIFY_CHANGE
  (lp-bytes-returned :pointer)   ; DWORD* optional, out
  (lp-overlapped :pointer)   ; OVERLAPPED* optional, in/out
  (lp-completion-routine :pointer)   ; LPOVERLAPPED_COMPLETION_ROUTINE optional
  (read-directory-notify-information-class :int32))   ; READ_DIRECTORY_NOTIFY_INFORMATION_CLASS
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $ReadDirectoryChangesExW = Win32::API::More->new('KERNEL32',
    'BOOL ReadDirectoryChangesExW(HANDLE hDirectory, LPVOID lpBuffer, DWORD nBufferLength, BOOL bWatchSubtree, DWORD dwNotifyFilter, LPVOID lpBytesReturned, LPVOID lpOverlapped, LPVOID lpCompletionRoutine, int ReadDirectoryNotifyInformationClass)');
# my $ret = $ReadDirectoryChangesExW->Call($hDirectory, $lpBuffer, $nBufferLength, $bWatchSubtree, $dwNotifyFilter, $lpBytesReturned, $lpOverlapped, $lpCompletionRoutine, $ReadDirectoryNotifyInformationClass);
# hDirectory : HANDLE -> HANDLE
# lpBuffer : void* out -> LPVOID
# nBufferLength : DWORD -> DWORD
# bWatchSubtree : BOOL -> BOOL
# dwNotifyFilter : FILE_NOTIFY_CHANGE -> DWORD
# lpBytesReturned : DWORD* optional, out -> LPVOID
# lpOverlapped : OVERLAPPED* optional, in/out -> LPVOID
# lpCompletionRoutine : LPOVERLAPPED_COMPLETION_ROUTINE optional -> LPVOID
# ReadDirectoryNotifyInformationClass : READ_DIRECTORY_NOTIFY_INFORMATION_CLASS -> int
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# コールバック(関数ポインタ)は Perl sub を直接渡せません。Win32::API::Callback を使用してください。

関連項目

公式の関連項目
使用する型