   Monitor directory with "ReadDirectoryChangesW " [solved]
  Hello,I'm trying to get the ReadDirectory to work asynchronously.I have the following code, create a global instance of DirectoryMonitorand pass a directory it should watch.However if I modify something in the directory (eg rename a file, edit&save or copy a new file I don't get the "monitorRoutine"-callback...My main program is just running a message loop, with more or less onlya sleep in it... Any idea what I'm doing wrong?Regards,  Roger  void _startMonitoring(sMonitorDirectory* toMonitor); // fwd declVOID CALLBACK monitorRoutine(DWORD error, DWORD numBytes, LPOVERLAPPED overlapped)  {    OutputDebugString ("MonitorRoutine!\n");    // <todo>    // Check what changed and call the user-callback// with appropriate params// continue monitoring    _startMonitoring(toMonitor);  }   void _startMonitoring(sMonitorDirectory* toMonitor)  {    if (toMonitor)    { toMonitor->overlapped_buffer.hEvent = (void *) toMonitor; BOOL result = ReadDirectoryChangesW (toMonitor->handle, toMonitor->directory, sizeof(toMonitor->directory), toMonitor->recursive,FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_SIZE,NULL, &toMonitor->overlapped_buffer, monitorRoutine); assert (result);    }else { assert (0 && "_startMonitoring() null-pointer?!"); }  } #endif  // static member, for simplicity  sMonitorDirectory DirectoryMonitor::dirs [MONITOR_DIR_MAX];  U32 DirectoryMonitor::monitorDirectory(const String& dir, directory_monitor_callback cbOnChange, bool recursive)  {    // Find free slot to use    for (U32 i=0; i<MONITOR_DIR_MAX; i++)    { if (!strlen(DirectoryMonitor::dirs.directory)) {   // Open directory with for async (overlapped) usage   DirectoryMonitor::dirs.handle = CreateFile(  dir.c_str(),    // pointer to the file name  FILE_LIST_DIRECTORY, // access (read-write) mode  FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,  // share mode  NULL, // security descriptor  OPEN_EXISTING,  // how to create  FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, // file attributes  NULL  // file with attributes to copy   );   // If CreateFile succeeded, we start monitoring    if (DirectoryMonitor::dirs.handle!=INVALID_HANDLE_VALUE)   {strncpy (dirs.directory, dir.c_str(), sizeof(dirs.directory)-1);dirs.recursive = recursive;dirs.callback = cbOnChange;_startMonitoring(&dirs); return i;   } }    }    return MONITOR_DIR_MAX;  }[Edited by - Kitt3n on December 15, 2005 7:28:00 AM]
  Functions that end with W are UNICODE versions. Are you sure you're using UNICODE? Usually, you're not really supposed the specify W or A at the end of a WinAPI function, that will be "added automagically" depending on whether it's a UNICODE build or not.   ;  I don't have the UNICODE define set...However apparently there is are no functions"ReadDirectoryChanges" or "ReadDirectoryChangesA",rather only the "ReadDirectoryChangesW" one...I tried adding the unicode-define to the .cpp,(had to fix a few function-calls), but didn't getmy callback either;  Such overlapped callbacks are only dispatched when your thread is in an alertable wait state (see SleepEx). What I do is have directory notifications posted to a completion port; the main loop polls it and when changes are detected, the relevant files are reloaded.   ;  The SleepEx did the trick, I'm getting my callback now.;  Cool.   