Click here to Skip to main content
15,884,047 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to cerate an simple application that is listening to a folder, and everytime a new file is created, moved or copyed to that folder i want to get a notification.
So i created a simple application that creates a seperate thread where i call ReadDirectoryChangesW.
To do this infinitely i have done it into a while(true) loop.

The code is pretty simple (i use ReadDirectoryChangesW in synchronus mode)
C++
while(TRUE)
{
	BYTE						byBuffer[32 * 1024]; // 32 kB
	DWORD						dwOffset = 0;
	TCHAR						cFile[MAX_PATH];
	DWORD						dwBytesRet;
	PFILE_NOTIFY_INFORMATION	pNotify;

	ReadDirectoryChangesW(pWatcher->m_hDir, byBuffer, sizeof(byBuffer), 0, pWatcher->m_dwNotifyFilter, &dwBytesRet, NULL, NULL);

	do
	{
		pNotify = (PFILE_NOTIFY_INFORMATION) &byBuffer[dwOffset];
		dwOffset += pNotify->NextEntryOffset;

		INT nCount = WideCharToMultiByte(CP_ACP, 0, pNotify->FileName, pNotify->FileNameLength / sizeof(WCHAR), cFile, MAX_PATH - 1, NULL, NULL);
		cFile[nCount] = TEXT('\0');

		// Notify me
	}
	while(pNotify->NextEntryOffset != 0);
}


The problem is now, that for every new created file i get two notifications.

It do it in the following order:

  1. ReadDirectoryChangesW returns with the new created file
  2. move into the do-while loop
  3. get the pNotify-pointer
  4. calculate the offset
  5. Copy the buffer into a TCHAR-array
  6. Notify me
  7. up to this point everything is ok


  8. NextEntryOffset == 0, so jump outside the do-while loop
  9. now loop begins again and run to ReadDirectoryChangesW
  10. ReadDirectoryChangesW returns immediately
  11. Steps 2-8 again
  12. Execution hold on ReadDirectoryChangesW until a new file is created


I have searched at the internet and found this looks lika a common bug. So i have tried to work around the bug with a second string. I compare the second string to the string stored in cFile[] and if they are different, i save cFile to my second string and notify.
That work's fine if only one file is created at a time. But if i copy a bundle of files (for example 4) the solution won't work, because the files doesn't come ordered (if i say ordered i mean File 1, File 1, File 2, File 2, ... they come more like File 1, File 2, File 3, File 4, File 3, File 1, File 4).

So i hope you guys from CodeProject could help me; is there any usable (and pretty) solution for this problem out there?

I' so happy to hear some solutions or ideas from you!
Thank you so much in advance!
Posted

1 solution

I would use the built-in functionality of Windows to register a folder change notification: [^] : why re-invent the available ?

In C# this is wrapped in the FileSystemWatcher Class: [^].
 
Share this answer
 
Comments
C3D1 21-Oct-14 2:05am    
Yes, i know in C# this is pretty simple.
My first attempt was in C#, but poorly i have to implement it in VC++/MFC.
C3D1 21-Oct-14 5:57am    
I have it done now with FindFirstChangeNotification and FindNextChangeNotification. But the question is still there.
It seems to be a general problem with ReadDirectoryChangesW. So IMHO there should be any solution (or maybe just a workaround) out there.
It's not that important anymore for me to find 'THE' solution for this problem, because i have solved it with your Link, but i'm still interested to solve it.
At the moment i think it's okay to accept your solution, because it solved my problem. But i'm glad to see if anyone knows 'THE' solution for ReadDirectoryChangesW, and then i will mark his (/or her) Answer as solution.
Thank you verry much for any new answers.
BillWoodruff 21-Oct-14 8:37am    
Looking at the comments on the MSDN page on ReadDirectoryChangesW

http://msdn.microsoft.com/en-us/library/windows/desktop/aa365465(v=vs.85).aspx

Looks like a lot of people have problems with it.

There is a CP article on this API call, but it's quite old:

http://www.codeproject.com/Articles/950/CDirectoryChangeWatcher-ReadDirectoryChangesW-all

Using these API's directly is not something I have worked with ... I use C# ... but I find the idea of an "infinite" while loop enclosing the call to ReadDirectoryChangesW strange: I thought that API was something you used after an asynchronous notification from the other file-directory-change API's.

good luck, Bill

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900