Bootstrap

ReadDirectoryChangesW

HANDLE   hDir   =   NULL;
HANDLE   hThread   =   NULL;
bool   bStop   =   false;
bool   bExit   =   false;
char   Buff[1024];
OVERLAPPED   ov;

DWORD   WINAPI   ThreadProc(LPVOID   lpParameter)
{
             DWORD   Read   =   0;
             memset(&ov,   0,   sizeof(ov));
             ov.hEvent   =   CreateEvent(NULL,   false,   NULL,   NULL);

             while   (!bStop)
             {
                         if   (!ReadDirectoryChangesW(hDir,
                                     Buff,
                                     1024,
                                     false,
                                     FILE_NOTIFY_CHANGE_FILE_NAME,
                                     &Read,
                                     &ov,
                                     NULL))
                         {
                                     break;
                         }
                         if   (WaitForSingleObject(ov.hEvent,   INFINITE)   ==   WAIT_OBJECT_0      )
                         {
                                     if   (!bStop)
                                     {
                                                 ShowMessage( "Change ");
                                     }
                         }
             }
             CloseHandle(ov.hEvent);
             bExit   =   true;
}
void   __fastcall   TForm1::Button1Click(TObject   *Sender)
{
             hDir   =   CreateFile(    "c:\\xxx ",   //文件目录
                                   FILE_LIST_DIRECTORY,
                                     FILE_SHARE_READ   |FILE_SHARE_WRITE   |FILE_SHARE_DELETE,
                                     NULL,
                                     OPEN_EXISTING,
                                     FILE_FLAG_BACKUP_SEMANTICS   |FILE_FLAG_OVERLAPPED,
                                     NULL);
             if   (hDir   ==   INVALID_HANDLE_VALUE   )
             {
                         return;
             }

             hThread   =   CreateThread(NULL,   0,   ThreadProc,   NULL,   0   ,   NULL);
}
//---------------------------------------------------------------------------

void   __fastcall   TForm1::Button2Click(TObject   *Sender)
{
             bStop   =   true;
             SetEvent(ov.hEvent);      //使可以退出
           while   (!bExit)
             {
                         Application-> ProcessMessages();
                         Sleep(100);
             }
             CloseHandle(hThread);
             CloseHandle(hDir);
}
;