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
lpBuffervoid*out
nBufferLengthDWORDin
bWatchSubtreeBOOLin
dwNotifyFilterFILE_NOTIFY_CHANGEin
lpBytesReturnedDWORD*outoptional
lpOverlappedOVERLAPPED*inoutoptional
lpCompletionRoutineLPOVERLAPPED_COMPLETION_ROUTINEinoptional
ReadDirectoryNotifyInformationClassREAD_DIRECTORY_NOTIFY_INFORMATION_CLASSin

戻り値の型: BOOL

各言語での呼び出し定義

// 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 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)。