#include <AFXPRIV.H> //For USES_CONVERSION; void ClassBlaBla::StartThread() { //m_pParams = new ThreadParams(); //m_pParams->bContinue = true; CWinThread * pThread = AfxBeginThread( ThreadRoute ,NULL/*m_pParams*/); } UINT ClassBlaBla::ThreadRoute( LPVOID param ) { USES_CONVERSION; //ThreadParams * params = (ThreadParams*)param; //::DeleteFile("c:\\tmp\\events.txt"); HANDLE hDir = CreateFile( CString("c:\\w"), // pointer to the file name FILE_LIST_DIRECTORY, // access (read/write) mode FILE_SHARE_READ | FILE_SHARE_DELETE | // share mode FILE_SHARE_WRITE, NULL, // security descriptor OPEN_EXISTING, // how to create FILE_FLAG_BACKUP_SEMANTICS, // file attributes NULL // file with attributes to copy ); BYTE buffer[102400]={0}; DWORD BytesReturned; while( ReadDirectoryChangesW( hDir, // handle to directory &buffer, // read results buffer 102400, // length of buffer TRUE, // monitoring option FILE_NOTIFY_CHANGE_SECURITY| FILE_NOTIFY_CHANGE_CREATION| FILE_NOTIFY_CHANGE_LAST_ACCESS| FILE_NOTIFY_CHANGE_LAST_WRITE| FILE_NOTIFY_CHANGE_SIZE| FILE_NOTIFY_CHANGE_ATTRIBUTES| FILE_NOTIFY_CHANGE_DIR_NAME| FILE_NOTIFY_CHANGE_FILE_NAME, // filter conditions &BytesReturned, // bytes returned NULL, // overlapped buffer NULL// completion routine )) { CTime tm = CTime::GetCurrentTime(); CString sActionText(_T("")); PFILE_NOTIFY_INFORMATION pcur = (PFILE_NOTIFY_INFORMATION) buffer; PFILE_NOTIFY_INFORMATION pOld = NULL; if( pcur != NULL ) do { switch( pcur->Action ) { case FILE_ACTION_ADDED: sActionText = "The file was added to the directory"; break; case FILE_ACTION_REMOVED: sActionText = "The file was removed from the directory"; break; case FILE_ACTION_MODIFIED: sActionText = "The file was modified. This can be a change in the time stamp or attributes."; break; case FILE_ACTION_RENAMED_OLD_NAME: sActionText = "The file was renamed and this is the old name."; break; case FILE_ACTION_RENAMED_NEW_NAME: sActionText = "The file was renamed and this is the new name."; break; }; WCHAR wcFileName[ MAX_PATH + 1] = {0};//L""; memcpy( wcFileName, pcur->FileName, min( (MAX_PATH * sizeof(WCHAR)), pcur->FileNameLength)); CString s = wcFileName; //FILE * fp = fopen("c:\\tmp\\events.txt","a+"); //if( fp ) //{ // fprintf(fp,"%s\n",s); // fclose(fp); //} TRACE("%s -> %s\n",sActionText,s); pOld = pcur; pcur = (PFILE_NOTIFY_INFORMATION) ((LPBYTE)pcur + pcur->NextEntryOffset); ASSERT( (DWORD)((BYTE*)pcur - buffer) < 102400);//make sure we haven't gone too far if( (DWORD)((BYTE*)pcur - buffer) > 102400 ) { //we've gone too far.... this data is hosed. // // This sometimes happens if the watched directory becomes deleted... remove the FILE_SHARE_DELETE flag when using CreateFile() to get the handle to the directory... pcur = pOld; } }while(pcur && pOld->NextEntryOffset != 0); } return 0; }