FindFirstChangeNotificationW
関数シグネチャ
// KERNEL32.dll (Unicode / -W)
#include <windows.h>
HANDLE FindFirstChangeNotificationW(
LPCWSTR lpPathName,
BOOL bWatchSubtree,
FILE_NOTIFY_CHANGE dwNotifyFilter
);パラメーター
| 名前 | 型 | 方向 | 説明 | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| lpPathName | LPCWSTR | in | 監視するディレクトリのフルパスです。 相対パスや空の文字列を指定することはできません。 既定では、名前は MAX_PATH 文字に制限されます。この制限をワイド文字 32,767 文字まで拡張するには、パスの先頭に "\\?\" を付加します。詳しくは、ファイル、パス、および名前空間の名前付けを参照してください。 ヒント
Windows 10 バージョン 1607 以降では、"\\?\" を付加せずに MAX_PATH の制限を解除することをオプトインできます。詳しくは、ファイル、パス、および名前空間の名前付けの「最大パス長の制限」セクションを参照してください。 | ||||||||||||||
| bWatchSubtree | BOOL | in | このパラメーターが TRUE の場合、指定したディレクトリをルートとするディレクトリ ツリーを監視します。FALSE の場合は、指定したディレクトリのみを監視します。 | ||||||||||||||
| dwNotifyFilter | FILE_NOTIFY_CHANGE | in | 変更通知の待機を満たすフィルター条件です。このパラメーターには、次の値のうち 1 つ以上を指定できます。
|
戻り値の型: HANDLE
公式ドキュメント
変更通知ハンドルを作成し、変更通知フィルターの初期条件を設定します。(Unicode)
戻り値
関数が成功すると、戻り値は変更検索通知オブジェクトのハンドルになります。
関数が失敗すると、戻り値は INVALID_HANDLE_VALUE になります。拡張エラー情報を取得するには、 GetLastError を呼び出します。
解説(Remarks)
待機関数は、 FindFirstChangeNotification 関数が返すハンドルを使用して、指定したディレクトリまたはサブツリーを監視できます。監視対象のディレクトリまたはサブツリーでフィルター条件のいずれかが発生すると、待機が満たされます。
待機が満たされた後、アプリケーションはこの状態に応答し、 FindNextChangeNotification 関数と適切な待機関数を呼び出すことで、ディレクトリの監視を継続できます。ハンドルが不要になったら、 FindCloseChangeNotification 関数を使用して閉じることができます。
リモート ファイル システムに対して FindFirstChangeNotification を呼び出した場合、通知が返されないことがあります。
シンボリック リンクの動作 — パスがシンボリック リンクを指している場合、通知ハンドルはリンク先に対して作成されます。
シンボリック リンクを含むディレクトリの変更通知を受け取るようにアプリケーションが登録されている場合、アプリケーションはシンボリック リンク自体が変更されたときにのみ通知され、リンク先のファイルが変更されても通知されません。
Windows 8 および Windows Server 2012 では、この関数は次のテクノロジによってサポートされます。
| Technology | Supported |
|---|---|
| Server Message Block (SMB) 3.0 プロトコル | はい |
| SMB 3.0 Transparent Failover (TFO) | はい |
| SMB 3.0 with Scale-out File Shares (SO) | はい |
| Cluster Shared Volume File System (CsvFS) | 備考を参照 |
| Resilient File System (ReFS) | はい |
CsvFs の一時停止/再開時に、アプリケーションで誤検知が発生することがあります。
Examples
例については、 ディレクトリ変更通知の取得を参照してください。
fileapi.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして FindFirstChangeNotification を定義します。エンコード非依存のエイリアスを、エンコード非依存ではないコードと混在させて使用すると、不整合が生じ、コンパイル エラーまたは実行時エラーを引き起こすことがあります。詳しくは、関数プロトタイプの規則を参照してください。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// KERNEL32.dll (Unicode / -W)
#include <windows.h>
HANDLE FindFirstChangeNotificationW(
LPCWSTR lpPathName,
BOOL bWatchSubtree,
FILE_NOTIFY_CHANGE dwNotifyFilter
);[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern IntPtr FindFirstChangeNotificationW(
[MarshalAs(UnmanagedType.LPWStr)] string lpPathName, // LPCWSTR
bool bWatchSubtree, // BOOL
uint dwNotifyFilter // FILE_NOTIFY_CHANGE
);<DllImport("KERNEL32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function FindFirstChangeNotificationW(
<MarshalAs(UnmanagedType.LPWStr)> lpPathName As String, ' LPCWSTR
bWatchSubtree As Boolean, ' BOOL
dwNotifyFilter As UInteger ' FILE_NOTIFY_CHANGE
) As IntPtr
End Function' lpPathName : LPCWSTR
' bWatchSubtree : BOOL
' dwNotifyFilter : FILE_NOTIFY_CHANGE
Declare PtrSafe Function FindFirstChangeNotificationW Lib "kernel32" ( _
ByVal lpPathName As LongPtr, _
ByVal bWatchSubtree As Long, _
ByVal dwNotifyFilter As Long) As LongPtr
' Unicode(W): 文字列は ByVal As LongPtr とし StrPtr(unicodeStr) を渡す
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
FindFirstChangeNotificationW = ctypes.windll.kernel32.FindFirstChangeNotificationW
FindFirstChangeNotificationW.restype = ctypes.c_void_p
FindFirstChangeNotificationW.argtypes = [
wintypes.LPCWSTR, # lpPathName : LPCWSTR
wintypes.BOOL, # bWatchSubtree : BOOL
wintypes.DWORD, # dwNotifyFilter : FILE_NOTIFY_CHANGE
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('KERNEL32.dll')
FindFirstChangeNotificationW = Fiddle::Function.new(
lib['FindFirstChangeNotificationW'],
[
Fiddle::TYPE_VOIDP, # lpPathName : LPCWSTR
Fiddle::TYPE_INT, # bWatchSubtree : BOOL
-Fiddle::TYPE_INT, # dwNotifyFilter : FILE_NOTIFY_CHANGE
],
Fiddle::TYPE_VOIDP)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "kernel32")]
extern "system" {
fn FindFirstChangeNotificationW(
lpPathName: *const u16, // LPCWSTR
bWatchSubtree: i32, // BOOL
dwNotifyFilter: u32 // FILE_NOTIFY_CHANGE
) -> *mut core::ffi::c_void;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern IntPtr FindFirstChangeNotificationW([MarshalAs(UnmanagedType.LPWStr)] string lpPathName, bool bWatchSubtree, uint dwNotifyFilter);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_FindFirstChangeNotificationW' -Namespace Win32 -PassThru
# $api::FindFirstChangeNotificationW(lpPathName, bWatchSubtree, dwNotifyFilter)#uselib "KERNEL32.dll"
#func global FindFirstChangeNotificationW "FindFirstChangeNotificationW" wptr, wptr, wptr
; FindFirstChangeNotificationW lpPathName, bWatchSubtree, dwNotifyFilter ; 戻り値は stat
; lpPathName : LPCWSTR -> "wptr"
; bWatchSubtree : BOOL -> "wptr"
; dwNotifyFilter : FILE_NOTIFY_CHANGE -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "KERNEL32.dll"
#cfunc global FindFirstChangeNotificationW "FindFirstChangeNotificationW" wstr, int, int
; res = FindFirstChangeNotificationW(lpPathName, bWatchSubtree, dwNotifyFilter)
; lpPathName : LPCWSTR -> "wstr"
; bWatchSubtree : BOOL -> "int"
; dwNotifyFilter : FILE_NOTIFY_CHANGE -> "int"; HANDLE FindFirstChangeNotificationW(LPCWSTR lpPathName, BOOL bWatchSubtree, FILE_NOTIFY_CHANGE dwNotifyFilter)
#uselib "KERNEL32.dll"
#cfunc global FindFirstChangeNotificationW "FindFirstChangeNotificationW" wstr, int, int
; res = FindFirstChangeNotificationW(lpPathName, bWatchSubtree, dwNotifyFilter)
; lpPathName : LPCWSTR -> "wstr"
; bWatchSubtree : BOOL -> "int"
; dwNotifyFilter : FILE_NOTIFY_CHANGE -> "int"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
procFindFirstChangeNotificationW = kernel32.NewProc("FindFirstChangeNotificationW")
)
// lpPathName (LPCWSTR), bWatchSubtree (BOOL), dwNotifyFilter (FILE_NOTIFY_CHANGE)
r1, _, err := procFindFirstChangeNotificationW.Call(
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpPathName))),
uintptr(bWatchSubtree),
uintptr(dwNotifyFilter),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // HANDLEfunction FindFirstChangeNotificationW(
lpPathName: PWideChar; // LPCWSTR
bWatchSubtree: BOOL; // BOOL
dwNotifyFilter: DWORD // FILE_NOTIFY_CHANGE
): THandle; stdcall;
external 'KERNEL32.dll' name 'FindFirstChangeNotificationW';result := DllCall("KERNEL32\FindFirstChangeNotificationW"
, "WStr", lpPathName ; LPCWSTR
, "Int", bWatchSubtree ; BOOL
, "UInt", dwNotifyFilter ; FILE_NOTIFY_CHANGE
, "Ptr") ; return: HANDLE●FindFirstChangeNotificationW(lpPathName, bWatchSubtree, dwNotifyFilter) = DLL("KERNEL32.dll", "void* FindFirstChangeNotificationW(char*, bool, dword)")
# 呼び出し: FindFirstChangeNotificationW(lpPathName, bWatchSubtree, dwNotifyFilter)
# lpPathName : LPCWSTR -> "char*"
# bWatchSubtree : BOOL -> "bool"
# dwNotifyFilter : FILE_NOTIFY_CHANGE -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。const std = @import("std");
extern "kernel32" fn FindFirstChangeNotificationW(
lpPathName: [*c]const u16, // LPCWSTR
bWatchSubtree: i32, // BOOL
dwNotifyFilter: u32 // FILE_NOTIFY_CHANGE
) callconv(std.os.windows.WINAPI) ?*anyopaque;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc FindFirstChangeNotificationW(
lpPathName: WideCString, # LPCWSTR
bWatchSubtree: int32, # BOOL
dwNotifyFilter: uint32 # FILE_NOTIFY_CHANGE
): pointer {.importc: "FindFirstChangeNotificationW", stdcall, dynlib: "KERNEL32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "kernel32");
extern(Windows)
void* FindFirstChangeNotificationW(
const(wchar)* lpPathName, // LPCWSTR
int bWatchSubtree, // BOOL
uint dwNotifyFilter // FILE_NOTIFY_CHANGE
);ccall((:FindFirstChangeNotificationW, "KERNEL32.dll"), stdcall, Ptr{Cvoid},
(Cwstring, Int32, UInt32),
lpPathName, bWatchSubtree, dwNotifyFilter)
# lpPathName : LPCWSTR -> Cwstring
# bWatchSubtree : BOOL -> Int32
# dwNotifyFilter : FILE_NOTIFY_CHANGE -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
void* FindFirstChangeNotificationW(
const uint16_t* lpPathName,
int32_t bWatchSubtree,
uint32_t dwNotifyFilter);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.FindFirstChangeNotificationW(lpPathName, bWatchSubtree, dwNotifyFilter)
-- lpPathName : LPCWSTR
-- bWatchSubtree : BOOL
-- dwNotifyFilter : FILE_NOTIFY_CHANGE
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const FindFirstChangeNotificationW = lib.func('__stdcall', 'FindFirstChangeNotificationW', 'void *', ['str16', 'int32_t', 'uint32_t']);
// FindFirstChangeNotificationW(lpPathName, bWatchSubtree, dwNotifyFilter)
// lpPathName : LPCWSTR -> 'str16'
// bWatchSubtree : BOOL -> 'int32_t'
// dwNotifyFilter : FILE_NOTIFY_CHANGE -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("KERNEL32.dll", {
FindFirstChangeNotificationW: { parameters: ["buffer", "i32", "u32"], result: "pointer" },
});
// lib.symbols.FindFirstChangeNotificationW(lpPathName, bWatchSubtree, dwNotifyFilter)
// lpPathName : LPCWSTR -> "buffer"
// bWatchSubtree : BOOL -> "i32"
// dwNotifyFilter : FILE_NOTIFY_CHANGE -> "u32"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
void* FindFirstChangeNotificationW(
const uint16_t* lpPathName,
int32_t bWatchSubtree,
uint32_t dwNotifyFilter);
C, "KERNEL32.dll");
// $ffi->FindFirstChangeNotificationW(lpPathName, bWatchSubtree, dwNotifyFilter);
// lpPathName : LPCWSTR
// bWatchSubtree : BOOL
// dwNotifyFilter : FILE_NOTIFY_CHANGE
// 構造体/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, W32APIOptions.UNICODE_OPTIONS);
Pointer FindFirstChangeNotificationW(
WString lpPathName, // LPCWSTR
boolean bWatchSubtree, // BOOL
int dwNotifyFilter // FILE_NOTIFY_CHANGE
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("kernel32")]
lib LibKERNEL32
fun FindFirstChangeNotificationW = FindFirstChangeNotificationW(
lpPathName : UInt16*, # LPCWSTR
bWatchSubtree : Int32, # BOOL
dwNotifyFilter : UInt32 # FILE_NOTIFY_CHANGE
) : Void*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef FindFirstChangeNotificationWNative = Pointer<Void> Function(Pointer<Utf16>, Int32, Uint32);
typedef FindFirstChangeNotificationWDart = Pointer<Void> Function(Pointer<Utf16>, int, int);
final FindFirstChangeNotificationW = DynamicLibrary.open('KERNEL32.dll')
.lookupFunction<FindFirstChangeNotificationWNative, FindFirstChangeNotificationWDart>('FindFirstChangeNotificationW');
// lpPathName : LPCWSTR -> Pointer<Utf16>
// bWatchSubtree : BOOL -> Int32
// dwNotifyFilter : FILE_NOTIFY_CHANGE -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function FindFirstChangeNotificationW(
lpPathName: PWideChar; // LPCWSTR
bWatchSubtree: BOOL; // BOOL
dwNotifyFilter: DWORD // FILE_NOTIFY_CHANGE
): THandle; stdcall;
external 'KERNEL32.dll' name 'FindFirstChangeNotificationW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "FindFirstChangeNotificationW"
c_FindFirstChangeNotificationW :: CWString -> CInt -> Word32 -> IO (Ptr ())
-- lpPathName : LPCWSTR -> CWString
-- bWatchSubtree : BOOL -> CInt
-- dwNotifyFilter : FILE_NOTIFY_CHANGE -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let findfirstchangenotificationw =
foreign "FindFirstChangeNotificationW"
((ptr uint16_t) @-> int32_t @-> uint32_t @-> returning (ptr void))
(* lpPathName : LPCWSTR -> (ptr uint16_t) *)
(* bWatchSubtree : BOOL -> int32_t *)
(* dwNotifyFilter : FILE_NOTIFY_CHANGE -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)
(cffi:defcfun ("FindFirstChangeNotificationW" find-first-change-notification-w :convention :stdcall) :pointer
(lp-path-name (:string :encoding :utf-16le)) ; LPCWSTR
(b-watch-subtree :int32) ; BOOL
(dw-notify-filter :uint32)) ; FILE_NOTIFY_CHANGE
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $FindFirstChangeNotificationW = Win32::API::More->new('KERNEL32',
'HANDLE FindFirstChangeNotificationW(LPCWSTR lpPathName, BOOL bWatchSubtree, DWORD dwNotifyFilter)');
# my $ret = $FindFirstChangeNotificationW->Call($lpPathName, $bWatchSubtree, $dwNotifyFilter);
# lpPathName : LPCWSTR -> LPCWSTR
# bWatchSubtree : BOOL -> BOOL
# dwNotifyFilter : FILE_NOTIFY_CHANGE -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
- f FindFirstChangeNotificationA (ANSI版) — ディレクトリの変更監視を開始し通知ハンドルを返す(ANSI版)。
- f FindCloseChangeNotification — 変更通知ハンドルを閉じて監視を終了する。
- f FindNextChangeNotification — 変更通知を受信した後に次の変更監視を再開する。
- f ReadDirectoryChangesW — ディレクトリの変更内容を監視して通知を受け取る。