Search code examples
c++windowsfile-monitoring

FindFirstChagneNotification cannot find specified file


I've searched and cannot find any answers on this. I'm trying to create a file monitoring program in C++ on Windows. The FindFirstChangeNotification function always fails to find the specified file. I've even tried hard coding the path for testing. Sample code is

LPTSTR lpDir = (LPTSTR)("C:\\Users\\userName\\Desktop");
HANDLE dwChangeHandle;

dwChangeHandle = FindFirstChangeNotification(
    lpDir,  // directory to watch
    FALSE,  // Don't watch subtree
    FILE_NOTIFY_CHANGE_SIZE);

if (dwChangeHandle == INVALID_HANDLE_VALUE) 
    ExitProcess(GetLastError());

Thanks.


Solution

  • The cast of the path looks iffy - if you compile for Unicode it won't find the path, as you're not actually pointing to the correct type of string.

    You should do LPTSTR lpDir = TEXT("C:\\Users\\userName\\Desktop");