Weird problem with FindFirstChangeNotification
-
The reason for this small test program was that I read on google about a possibility that you could not delete a directory that has a FindFirstChangeNotification watch on it. As it turned out, its no problem at all (at least on WinXP). But I noticed another stranger thing. When I delete the directory in Explorer, the HANDLE is set to signal state and I get a continuous flow of "Something happened". But when I delete the directory in Total Commander (new name for Windows Commander) I get error 5 (Access is denied.) from FindNextChangeNotification. And the program ends. This is not really a question, since the correct implementation should check that the director (or file) you are watching still exists. But what really bothered me were the different results from Explorer and Total Commander. So I wondered if some one else has had this problem or if some one knows why this happens. Here is the small test program I ran while deleting the directory (its written in Visual Studio 6.0 sp5):
int main( int argc, char* argv[] ) { HANDLE h = ::FindFirstChangeNotification( "d:\\test\\magnus", FALSE, FILE_NOTIFY_CHANGE_FILE_NAME ); if( h == INVALID_HANDLE_VALUE ) { std::cout << "Error on FFCN: " << ::GetLastError() << std::endl; return 0; } std::cout << "Entering loop" << std::endl; for( ;; ) { DWORD dwWaitState = ::WaitForSingleObject( h, INFINITE ); switch( dwWaitState ) { case WAIT_OBJECT_0: std::cout << "Something happened" << std::endl; break; case WAIT_ABANDONED: std::cout << "Error WAIT_ABANDONED" << std::endl; break; case WAIT_TIMEOUT: std::cout << "Error WAIT_TIMEOUT" << std::endl; break; default: std::cout << "Error DEFAULT" << std::endl; break; } if( dwWaitState != WAIT_OBJECT_0 ) break; if( ::FindNextChangeNotification( h ) == 0 ) { std::cout << "Error on FNCN: " << ::GetLastError() << std::endl; break; } } ::FindCloseChangeNotification( h ); return 0; }
Magnus