FileWatch.cpp
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:6k
源码类别:

游戏

开发平台:

Visual C++

  1. //-----------------------------------------------------------------------------
  2. // File: FileWatch.cpp
  3. //
  4. // Copyright (C) Microsoft Corporation. All Rights Reserved.
  5. //-----------------------------------------------------------------------------
  6. #include "stdafx.h"
  7. //-----------------------------------------------------------------------------
  8. // Name: 
  9. // Desc:
  10. //-----------------------------------------------------------------------------
  11. CFileWatch::CFileWatch()
  12. {
  13.     m_nFilesToWatch         = 0;
  14.     m_hFileChangeThread     = NULL;
  15.     m_dwFileChangeThreadId  = 0;
  16.     m_bFileChanged          = FALSE;
  17. }
  18. //-----------------------------------------------------------------------------
  19. // Name: 
  20. // Desc:
  21. //-----------------------------------------------------------------------------
  22. CFileWatch::~CFileWatch()
  23. {
  24.     Cleanup();
  25. }
  26. //-----------------------------------------------------------------------------
  27. // Name: 
  28. // Desc:
  29. //-----------------------------------------------------------------------------
  30. HRESULT CFileWatch::AddFileToWatch( TCHAR* strWatchFile )
  31. {
  32.     strcpy( m_strFilesToWatch[m_nFilesToWatch++], strWatchFile );
  33.     return S_OK;
  34. }
  35. //-----------------------------------------------------------------------------
  36. // Name: 
  37. // Desc:
  38. //-----------------------------------------------------------------------------
  39. HRESULT CFileWatch::Start()
  40. {
  41.     m_hFileChangeThread = CreateThread( NULL, 0, StaticFileChangeThreadFunc, 
  42.                                         this, 0, &m_dwFileChangeThreadId );
  43.     return S_OK;
  44. }
  45. //-----------------------------------------------------------------------------
  46. // Name: 
  47. // Desc:
  48. //-----------------------------------------------------------------------------
  49. BOOL CFileWatch::HaveFilesChanged( BOOL bResetChangeFlag )
  50. {
  51.     BOOL bFileChanged = m_bFileChanged;
  52.     if( bResetChangeFlag )
  53.         m_bFileChanged = FALSE;
  54.     return bFileChanged;
  55. }
  56. //-----------------------------------------------------------------------------
  57. // Name: 
  58. // Desc:
  59. //-----------------------------------------------------------------------------
  60. DWORD WINAPI CFileWatch::StaticFileChangeThreadFunc( LPVOID lpParam )
  61. {
  62.     CFileWatch* pApp = (CFileWatch*) lpParam;
  63.     return pApp->FileChangeThreadFunc();
  64. }
  65. //-----------------------------------------------------------------------------
  66. // Name: 
  67. // Desc:
  68. //-----------------------------------------------------------------------------
  69. DWORD CFileWatch::FileChangeThreadFunc()
  70. {
  71.     HANDLE      hFileHandles[MAX_WATCH_FILES];
  72.     FILETIME    aLastWriteTime[MAX_WATCH_FILES];
  73.     TCHAR       astrFileDir[MAX_WATCH_FILES][MAX_PATH];
  74.     TCHAR*      strFilePart;
  75.     FILETIME    lastWriteTime;
  76.     DWORD       dwResult;
  77.     int         i;
  78.     int         j;
  79.     HANDLE      hWatchHandles[MAX_WATCH_FILES];
  80.     int         nWatchHandles = 0;
  81.     for( i=0; i<m_nFilesToWatch; i++ )
  82.     {
  83.         hFileHandles[i] = CreateFile( m_strFilesToWatch[i], GENERIC_READ, 
  84.                                     FILE_SHARE_READ|FILE_SHARE_WRITE, 
  85.                                     NULL, OPEN_EXISTING, 0, NULL );
  86.         GetFileTime( hFileHandles[i], NULL, NULL, &aLastWriteTime[i] );
  87.         GetFullPathName( m_strFilesToWatch[i], MAX_PATH, 
  88.                          astrFileDir[i], &strFilePart );
  89.         if( strFilePart ) 
  90.             *strFilePart = 0;
  91.         BOOL bFound = FALSE;
  92.         for( j=0; j<i; j++ )
  93.         {
  94.             if( strcmp( astrFileDir[i], astrFileDir[j] ) == 0 )
  95.             {
  96.                 bFound = TRUE;
  97.                 break;
  98.             }
  99.         }
  100.         if( !bFound )
  101.         {
  102.             hWatchHandles[nWatchHandles++] = FindFirstChangeNotification( 
  103.                                                 astrFileDir[i], FALSE, 
  104.                                                 FILE_NOTIFY_CHANGE_LAST_WRITE );
  105.         }
  106.     }
  107.     
  108.     BOOL bDone = FALSE;
  109.     while( !bDone )
  110.     {   
  111.         dwResult = MsgWaitForMultipleObjects( nWatchHandles, hWatchHandles, FALSE, INFINITE, QS_ALLEVENTS );
  112.         if( dwResult == WAIT_OBJECT_0 + nWatchHandles )
  113.         {
  114.             MSG msg;
  115.             if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
  116.             {
  117.                 if ( msg.message == WM_QUIT )
  118.                     bDone = TRUE;
  119.             }
  120.         }
  121.         else
  122.         {
  123.             int nIndex = dwResult - WAIT_OBJECT_0;
  124.             assert( nIndex >= 0 || nIndex < 10 );
  125.             for( i=0; i<m_nFilesToWatch; i++ )
  126.             {
  127.                 GetFileTime( hFileHandles[i], NULL, NULL, &lastWriteTime );
  128.                 if( memcmp(&lastWriteTime, &aLastWriteTime[i], sizeof(FILETIME) ) ) 
  129.                 {
  130.                     m_bFileChanged = TRUE;
  131.                 }
  132.             }
  133.             FindNextChangeNotification( hWatchHandles[nIndex] );
  134.             FindNextChangeNotification( hWatchHandles[nIndex] ); 
  135.         }
  136.     }
  137.     for( i=0; i<nWatchHandles; i++ )
  138.         FindCloseChangeNotification( hWatchHandles[i] );
  139.     for( i=0; i<m_nFilesToWatch; i++ )
  140.         CloseHandle( hFileHandles[i] );
  141.     return 0;
  142. }
  143. //-----------------------------------------------------------------------------
  144. // Name: 
  145. // Desc:
  146. //-----------------------------------------------------------------------------
  147. HRESULT CFileWatch::Cleanup()
  148. {
  149.     if( m_hFileChangeThread )
  150.     {
  151.         PostThreadMessage( m_dwFileChangeThreadId, WM_QUIT, 0, 0 );
  152.         WaitForSingleObject( m_hFileChangeThread, INFINITE );
  153.         CloseHandle( m_hFileChangeThread );
  154.         m_hFileChangeThread = NULL;
  155.     }
  156.     return S_OK;
  157. }