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

FindFirstChangeNotificationA

関数
ディレクトリの変更監視を開始し通知ハンドルを返す(ANSI版)。
DLLKERNEL32.dll文字セットANSI (-A)呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

// KERNEL32.dll  (ANSI / -A)
#include <windows.h>

HANDLE FindFirstChangeNotificationA(
    LPCSTR lpPathName,
    BOOL bWatchSubtree,
    FILE_NOTIFY_CHANGE dwNotifyFilter
);

パラメーター

名前方向説明
lpPathNameLPCSTRin

監視対象ディレクトリのフルパス。

相対パスや空文字列は指定できません。

既定では、名前は MAX_PATH 文字に制限されます。この制限を 32,767 ワイド文字まで拡張するには、パスの先頭に "\\?\" を付加します。詳細については、ファイル、パス、および名前空間の名前付けを参照してください。

ヒント

Windows 10 バージョン 1607 以降では、"\\?\" を付加せずに MAX_PATH 制限を解除するようにオプトインできます。詳細については、ファイル、パス、および名前空間の名前付けの「最大パス長の制限」セクションを参照してください。

bWatchSubtreeBOOLinこのパラメーターが TRUE の場合、関数は指定したディレクトリをルートとするディレクトリツリーを監視します。FALSE の場合、指定したディレクトリのみを監視します。
dwNotifyFilterFILE_NOTIFY_CHANGEin

変更通知の待機を満たすフィルター条件。このパラメーターには、次の値を 1 つ以上指定できます。

Value Meaning
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_SECURITY
0x00000100
監視対象ディレクトリまたはサブツリー内でセキュリティ記述子が変更されると、変更通知の待機操作が戻ります。

戻り値の型: HANDLE

公式ドキュメント

変更通知ハンドルを作成し、初期の変更通知フィルター条件を設定します。(ANSI)

戻り値

関数が成功した場合、戻り値は変更検索通知オブジェクトへのハンドルです。

関数が失敗した場合、戻り値は 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 の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// KERNEL32.dll  (ANSI / -A)
#include <windows.h>

HANDLE FindFirstChangeNotificationA(
    LPCSTR lpPathName,
    BOOL bWatchSubtree,
    FILE_NOTIFY_CHANGE dwNotifyFilter
);
[DllImport("KERNEL32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern IntPtr FindFirstChangeNotificationA(
    [MarshalAs(UnmanagedType.LPStr)] string lpPathName,   // LPCSTR
    bool bWatchSubtree,   // BOOL
    uint dwNotifyFilter   // FILE_NOTIFY_CHANGE
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function FindFirstChangeNotificationA(
    <MarshalAs(UnmanagedType.LPStr)> lpPathName As String,   ' LPCSTR
    bWatchSubtree As Boolean,   ' BOOL
    dwNotifyFilter As UInteger   ' FILE_NOTIFY_CHANGE
) As IntPtr
End Function
' lpPathName : LPCSTR
' bWatchSubtree : BOOL
' dwNotifyFilter : FILE_NOTIFY_CHANGE
Declare PtrSafe Function FindFirstChangeNotificationA Lib "kernel32" ( _
    ByVal lpPathName As String, _
    ByVal bWatchSubtree As Long, _
    ByVal dwNotifyFilter As Long) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

FindFirstChangeNotificationA = ctypes.windll.kernel32.FindFirstChangeNotificationA
FindFirstChangeNotificationA.restype = ctypes.c_void_p
FindFirstChangeNotificationA.argtypes = [
    wintypes.LPCSTR,  # lpPathName : LPCSTR
    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')
FindFirstChangeNotificationA = Fiddle::Function.new(
  lib['FindFirstChangeNotificationA'],
  [
    Fiddle::TYPE_VOIDP,  # lpPathName : LPCSTR
    Fiddle::TYPE_INT,  # bWatchSubtree : BOOL
    -Fiddle::TYPE_INT,  # dwNotifyFilter : FILE_NOTIFY_CHANGE
  ],
  Fiddle::TYPE_VOIDP)
#[link(name = "kernel32")]
extern "system" {
    fn FindFirstChangeNotificationA(
        lpPathName: *const u8,  // LPCSTR
        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.Ansi, SetLastError = true)]
public static extern IntPtr FindFirstChangeNotificationA([MarshalAs(UnmanagedType.LPStr)] string lpPathName, bool bWatchSubtree, uint dwNotifyFilter);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_FindFirstChangeNotificationA' -Namespace Win32 -PassThru
# $api::FindFirstChangeNotificationA(lpPathName, bWatchSubtree, dwNotifyFilter)
#uselib "KERNEL32.dll"
#func global FindFirstChangeNotificationA "FindFirstChangeNotificationA" sptr, sptr, sptr
; FindFirstChangeNotificationA lpPathName, bWatchSubtree, dwNotifyFilter   ; 戻り値は stat
; lpPathName : LPCSTR -> "sptr"
; bWatchSubtree : BOOL -> "sptr"
; dwNotifyFilter : FILE_NOTIFY_CHANGE -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "KERNEL32.dll"
#cfunc global FindFirstChangeNotificationA "FindFirstChangeNotificationA" str, int, int
; res = FindFirstChangeNotificationA(lpPathName, bWatchSubtree, dwNotifyFilter)
; lpPathName : LPCSTR -> "str"
; bWatchSubtree : BOOL -> "int"
; dwNotifyFilter : FILE_NOTIFY_CHANGE -> "int"
; HANDLE FindFirstChangeNotificationA(LPCSTR lpPathName, BOOL bWatchSubtree, FILE_NOTIFY_CHANGE dwNotifyFilter)
#uselib "KERNEL32.dll"
#cfunc global FindFirstChangeNotificationA "FindFirstChangeNotificationA" str, int, int
; res = FindFirstChangeNotificationA(lpPathName, bWatchSubtree, dwNotifyFilter)
; lpPathName : LPCSTR -> "str"
; bWatchSubtree : BOOL -> "int"
; dwNotifyFilter : FILE_NOTIFY_CHANGE -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procFindFirstChangeNotificationA = kernel32.NewProc("FindFirstChangeNotificationA")
)

// lpPathName (LPCSTR), bWatchSubtree (BOOL), dwNotifyFilter (FILE_NOTIFY_CHANGE)
r1, _, err := procFindFirstChangeNotificationA.Call(
	uintptr(unsafe.Pointer(windows.BytePtrFromString(lpPathName))),
	uintptr(bWatchSubtree),
	uintptr(dwNotifyFilter),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HANDLE
function FindFirstChangeNotificationA(
  lpPathName: PAnsiChar;   // LPCSTR
  bWatchSubtree: BOOL;   // BOOL
  dwNotifyFilter: DWORD   // FILE_NOTIFY_CHANGE
): THandle; stdcall;
  external 'KERNEL32.dll' name 'FindFirstChangeNotificationA';
result := DllCall("KERNEL32\FindFirstChangeNotificationA"
    , "AStr", lpPathName   ; LPCSTR
    , "Int", bWatchSubtree   ; BOOL
    , "UInt", dwNotifyFilter   ; FILE_NOTIFY_CHANGE
    , "Ptr")   ; return: HANDLE
●FindFirstChangeNotificationA(lpPathName, bWatchSubtree, dwNotifyFilter) = DLL("KERNEL32.dll", "void* FindFirstChangeNotificationA(char*, bool, dword)")
# 呼び出し: FindFirstChangeNotificationA(lpPathName, bWatchSubtree, dwNotifyFilter)
# lpPathName : LPCSTR -> "char*"
# bWatchSubtree : BOOL -> "bool"
# dwNotifyFilter : FILE_NOTIFY_CHANGE -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "kernel32" fn FindFirstChangeNotificationA(
    lpPathName: [*c]const u8, // LPCSTR
    bWatchSubtree: i32, // BOOL
    dwNotifyFilter: u32 // FILE_NOTIFY_CHANGE
) callconv(std.os.windows.WINAPI) ?*anyopaque;
proc FindFirstChangeNotificationA(
    lpPathName: cstring,  # LPCSTR
    bWatchSubtree: int32,  # BOOL
    dwNotifyFilter: uint32  # FILE_NOTIFY_CHANGE
): pointer {.importc: "FindFirstChangeNotificationA", stdcall, dynlib: "KERNEL32.dll".}
pragma(lib, "kernel32");
extern(Windows)
void* FindFirstChangeNotificationA(
    const(char)* lpPathName,   // LPCSTR
    int bWatchSubtree,   // BOOL
    uint dwNotifyFilter   // FILE_NOTIFY_CHANGE
);
ccall((:FindFirstChangeNotificationA, "KERNEL32.dll"), stdcall, Ptr{Cvoid},
      (Cstring, Int32, UInt32),
      lpPathName, bWatchSubtree, dwNotifyFilter)
# lpPathName : LPCSTR -> Cstring
# bWatchSubtree : BOOL -> Int32
# dwNotifyFilter : FILE_NOTIFY_CHANGE -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
void* FindFirstChangeNotificationA(
    const char* lpPathName,
    int32_t bWatchSubtree,
    uint32_t dwNotifyFilter);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.FindFirstChangeNotificationA(lpPathName, bWatchSubtree, dwNotifyFilter)
-- lpPathName : LPCSTR
-- bWatchSubtree : BOOL
-- dwNotifyFilter : FILE_NOTIFY_CHANGE
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const FindFirstChangeNotificationA = lib.func('__stdcall', 'FindFirstChangeNotificationA', 'void *', ['str', 'int32_t', 'uint32_t']);
// FindFirstChangeNotificationA(lpPathName, bWatchSubtree, dwNotifyFilter)
// lpPathName : LPCSTR -> 'str'
// bWatchSubtree : BOOL -> 'int32_t'
// dwNotifyFilter : FILE_NOTIFY_CHANGE -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("KERNEL32.dll", {
  FindFirstChangeNotificationA: { parameters: ["buffer", "i32", "u32"], result: "pointer" },
});
// lib.symbols.FindFirstChangeNotificationA(lpPathName, bWatchSubtree, dwNotifyFilter)
// lpPathName : LPCSTR -> "buffer"
// bWatchSubtree : BOOL -> "i32"
// dwNotifyFilter : FILE_NOTIFY_CHANGE -> "u32"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
void* FindFirstChangeNotificationA(
    const char* lpPathName,
    int32_t bWatchSubtree,
    uint32_t dwNotifyFilter);
C, "KERNEL32.dll");
// $ffi->FindFirstChangeNotificationA(lpPathName, bWatchSubtree, dwNotifyFilter);
// lpPathName : LPCSTR
// 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.ASCII_OPTIONS);
    Pointer FindFirstChangeNotificationA(
        String lpPathName,   // LPCSTR
        boolean bWatchSubtree,   // BOOL
        int dwNotifyFilter   // FILE_NOTIFY_CHANGE
    );
}
@[Link("kernel32")]
lib LibKERNEL32
  fun FindFirstChangeNotificationA = FindFirstChangeNotificationA(
    lpPathName : UInt8*,   # LPCSTR
    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 FindFirstChangeNotificationANative = Pointer<Void> Function(Pointer<Utf8>, Int32, Uint32);
typedef FindFirstChangeNotificationADart = Pointer<Void> Function(Pointer<Utf8>, int, int);
final FindFirstChangeNotificationA = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<FindFirstChangeNotificationANative, FindFirstChangeNotificationADart>('FindFirstChangeNotificationA');
// lpPathName : LPCSTR -> Pointer<Utf8>
// bWatchSubtree : BOOL -> Int32
// dwNotifyFilter : FILE_NOTIFY_CHANGE -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function FindFirstChangeNotificationA(
  lpPathName: PAnsiChar;   // LPCSTR
  bWatchSubtree: BOOL;   // BOOL
  dwNotifyFilter: DWORD   // FILE_NOTIFY_CHANGE
): THandle; stdcall;
  external 'KERNEL32.dll' name 'FindFirstChangeNotificationA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "FindFirstChangeNotificationA"
  c_FindFirstChangeNotificationA :: CString -> CInt -> Word32 -> IO (Ptr ())
-- lpPathName : LPCSTR -> CString
-- bWatchSubtree : BOOL -> CInt
-- dwNotifyFilter : FILE_NOTIFY_CHANGE -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let findfirstchangenotificationa =
  foreign "FindFirstChangeNotificationA"
    (string @-> int32_t @-> uint32_t @-> returning (ptr void))
(* lpPathName : LPCSTR -> string *)
(* 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 ("FindFirstChangeNotificationA" find-first-change-notification-a :convention :stdcall) :pointer
  (lp-path-name :string)   ; LPCSTR
  (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 $FindFirstChangeNotificationA = Win32::API::More->new('KERNEL32',
    'HANDLE FindFirstChangeNotificationA(LPCSTR lpPathName, BOOL bWatchSubtree, DWORD dwNotifyFilter)');
# my $ret = $FindFirstChangeNotificationA->Call($lpPathName, $bWatchSubtree, $dwNotifyFilter);
# lpPathName : LPCSTR -> LPCSTR
# bWatchSubtree : BOOL -> BOOL
# dwNotifyFilter : FILE_NOTIFY_CHANGE -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

文字セット違い
公式の関連項目
使用する型