How to debug file change notifications obtained by FindFirstChangeNotification?
-
20-08-2019 - |
Question
So, the question is: I get some notifications I don't want to get. But I don't know for what file/dir I got them. Is there a way to know why given notification was fired?
If you think about ReadDirectoryChangesW, please include a meaningful code sample.
Solution
If you would like Windows to tell you what specific file or subdirectory changed, you will need to use ReadDirectoryChangesW. The asynchronous mode is fairly simple if you use a completion routine.
On the other hand, you will probably get better performance by using the slightly more complicated I/O completion ports approach. I would recommend downloading Wes Jones' excellent CDirectoryChangeWatcher source code as a starting point. There are several gotchas that his code will help you avoid, particularly in parsing the FILE_NOTIFY_INFORMATION
records.
OTHER TIPS
~pseudocode
HANDLE handles[MAX_HANDLES];
std::string dir_array[MAX_HANDLES];
for i from 0 to MAX_HANDLES:
h[i] = FindFirstChangeNotification(dir_array[i]...);
nCount = MAX_HANDLES;
ret = WaitForMultipleObjects(handles, nCount ...);
// check if ret returns something between WAIT_OBJECT_0 and WAIT_OBJECT_0+nCount-1
if "so":
ret -= WAIT_OBJECT_0;
cout << "Directory " << dir_array[ret] << " changed" << endl;
See: http://msdn.microsoft.com/en-us/library/ms687025(VS.85).aspx
See on professional Win32 api newsgroup news://comp.os.ms-windows.programmer.win32 for well-known MS samples (C code) (since 90's...)