Int main()
{
HANDLE hChangeHandle;
// знаходимо першу зміну в каталозі
hChangeHandle = FindFirstChangeNotification(
L"C:\\demo_dir", // ім'я каталога
TRUE, // відстежувати також підкаталоги
FILE_NOTIFY_CHANGE_FILE_NAME // відстежувати зміну імені каталога
| FILE_NOTIFY_CHANGE_SIZE // та зміну його розміру
);
if (hChangeHandle == INVALID_HANDLE_VALUE)
{
std::cerr << "Find first change notification failed." << std::endl
<< "The last error code: " << GetLastError() << std::endl;
std::cout << "Press any key to finish.";
std::cin.get();
return 0;
}
// потрібно змінити каталог
std::cout << "Wait for changes in the directory." << std::endl;
// чекаємо першої зміни в каталозі
if (WaitForSingleObject(hChangeHandle, INFINITE) == WAIT_OBJECT_0)
std::cout << "First notification: the directory was changed." << std::endl;
else
{
std::cerr << "Wait for single object failed." << std::endl
<< "The last error code: " << GetLastError() << std::endl;
std::cout << "Press any key to finish.";
std::cin.get();
return 0;
}
// знаходимо другу зміну в каталозі
if (!FindNextChangeNotification(hChangeHandle))
{
std::cerr << "Find next change notification failed." << std::endl
<< "The last error code: " << GetLastError() << std::endl;
std::cout << "Press any key to finish.";
std::cin.get();
return 0;
}
// чекаємо другої зміни в каталозі
if (WaitForSingleObject(hChangeHandle, INFINITE) == WAIT_OBJECT_0)
{
std::cout << "Next notification: the directory was changed." << std::endl;
std::cin.get();
}
else
{
std::cerr << "Wait for single object failed." << std::endl
<< "The last error code: " << GetLastError() << std::endl;
std::cout << "Press any key to finish.";
std::cin.get();
return 0;
}
// закриваємо дескриптор пошуку
FindCloseChangeNotification(hChangeHandle);
std::cin.get();
return 0;
}
