I'm sure I am just doing something really dumb and not seeing it but can anyone tell me why the following code would not be picking up changes in the passed in directory?
When calling this code, creating and modifying files or directories in the passed in m_directory is ignored. But if I call
PostQueuedCompletionStatus( m_hCompletionPort, 0, 0, NULL );
then GetQueuedCompletionStatus correctly exits. Does anyone see what I am doing wrong? Here is the code:
// create handle to log dir
HANDLE logDirHandle = CreateFile(m_directory,
FILE_LIST_DIRECTORY,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS |
FILE_FLAG_OVERLAPPED,
NULL);
// create the completion port
m_hCompletionPort = CreateIoCompletionPort(logDirHandle, NULL, 0, 1);
// initial registration for changes
DWORD dwBufLength;
FILE_NOTIFY_INFORMATION notifyInfo;
OVERLAPPED overlapped;
ReadDirectoryChangesW(logDirHandle,
¬ifyInfo,
MAX_BUFFER,
TRUE,
FILE_NOTIFY_CHANGE_LAST_WRITE|FILE_NOTIFY_CHANGE_CREATION,
&dwBufLength,
&overlapped,
NULL);
// wait for a change
LPOVERLAPPED lpOverLapped;
ULONG key;
DWORD numBytes;
GetQueuedCompletionStatus(m_hCompletionPort, &numBytes, &key, &lpOverLapped, INFINITE))
EDIT:
Ok, I thought for sure ReadDirectoryChangesW() was returning TRUE but it is not. GetLastError() tells me the error is ERROR_INVALID_HANDLE (6). The strange thing is that logDirHandle appears to be valid and no error occurred in the call to CreateFile().
Also I refactored slightly to call the synchronous ReadDirectoryChangesW keeping everything the same except losing the OVERLAPPED stuff and it worked fine. So I am guessing I am implementing the OVERLAPPED stuff incorrectly?
Did you check the return values? And is this over a network?
Edit: You probably want to initialize OVERLAPPED to zero.