Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
3.4 KiB

  1. #include "StdAfx.h"
  2. #include "MonitorThread.h"
  3. namespace nsMonitorThread
  4. {
  5. _bstr_t GetLogFolder();
  6. }
  7. using namespace nsMonitorThread;
  8. //---------------------------------------------------------------------------
  9. // MonitorThread Class
  10. //---------------------------------------------------------------------------
  11. // Constructor
  12. CMonitorThread::CMonitorThread() :
  13. m_hMigrationLog(NULL)
  14. {
  15. SYSTEMTIME st;
  16. GetSystemTime(&st);
  17. if (!SystemTimeToFileTime(&st, &m_ftMigrationLogLastWriteTime))
  18. {
  19. m_ftMigrationLogLastWriteTime.dwLowDateTime = 0;
  20. m_ftMigrationLogLastWriteTime.dwHighDateTime = 0;
  21. }
  22. }
  23. // Destructor
  24. CMonitorThread::~CMonitorThread()
  25. {
  26. }
  27. // Start Method
  28. void CMonitorThread::Start()
  29. {
  30. CThread::StartThread();
  31. }
  32. // Stop Method
  33. void CMonitorThread::Stop()
  34. {
  35. CThread::StopThread();
  36. }
  37. // Run Method
  38. void CMonitorThread::Run()
  39. {
  40. try
  41. {
  42. _bstr_t strFolder = GetLogFolder();
  43. if (strFolder.length() > 0)
  44. {
  45. m_strMigrationLog = strFolder + _T("Migration.log");
  46. HANDLE hChange = FindFirstChangeNotification(strFolder, FALSE, FILE_NOTIFY_CHANGE_LAST_WRITE);
  47. HANDLE hHandles[2] = { StopEvent(), hChange };
  48. while (WaitForMultipleObjects(2, hHandles, FALSE, INFINITE) == (WAIT_OBJECT_0 + 1))
  49. {
  50. ProcessMigrationLog();
  51. FindNextChangeNotification(hChange);
  52. }
  53. FindCloseChangeNotification(hChange);
  54. ProcessMigrationLog();
  55. if (m_hMigrationLog)
  56. {
  57. CloseHandle(m_hMigrationLog);
  58. }
  59. }
  60. }
  61. catch (...)
  62. {
  63. ;
  64. }
  65. }
  66. // ProcessMigrationLog Method
  67. void CMonitorThread::ProcessMigrationLog()
  68. {
  69. if (m_hMigrationLog == NULL)
  70. {
  71. m_hMigrationLog = CreateFile(
  72. m_strMigrationLog,
  73. GENERIC_READ,
  74. FILE_SHARE_READ|FILE_SHARE_WRITE,
  75. NULL,
  76. OPEN_EXISTING,
  77. FILE_ATTRIBUTE_NORMAL,
  78. NULL
  79. );
  80. if (m_hMigrationLog)
  81. {
  82. _TCHAR ch;
  83. DWORD dwBytesRead;
  84. if (ReadFile(m_hMigrationLog, &ch, sizeof(ch), &dwBytesRead, NULL) && (dwBytesRead > 0))
  85. {
  86. if (ch != _T('\xFEFF'))
  87. {
  88. SetFilePointer(m_hMigrationLog, 0, NULL, FILE_BEGIN);
  89. }
  90. }
  91. }
  92. }
  93. if (m_hMigrationLog)
  94. {
  95. BY_HANDLE_FILE_INFORMATION bhfiInformation;
  96. if (GetFileInformationByHandle(m_hMigrationLog, &bhfiInformation))
  97. {
  98. if (CompareFileTime(&bhfiInformation.ftLastWriteTime, &m_ftMigrationLogLastWriteTime) == 1)
  99. {
  100. m_ftMigrationLogLastWriteTime = bhfiInformation.ftLastWriteTime;
  101. HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  102. _TCHAR szBuffer[1024];
  103. DWORD dwBytesRead;
  104. while (ReadFile(m_hMigrationLog, szBuffer, sizeof(szBuffer), &dwBytesRead, NULL) && (dwBytesRead > 0))
  105. {
  106. DWORD dwCharsWritten;
  107. WriteConsole(hStdOut, szBuffer, dwBytesRead / sizeof(_TCHAR), &dwCharsWritten, NULL);
  108. }
  109. }
  110. }
  111. }
  112. }
  113. namespace nsMonitorThread
  114. {
  115. // GetLogFolder Method
  116. _bstr_t GetLogFolder()
  117. {
  118. _bstr_t strFolder;
  119. HKEY hKey;
  120. DWORD dwError = RegOpenKey(HKEY_LOCAL_MACHINE, _T("Software\\Mission Critical Software\\DomainAdmin"), &hKey);
  121. if (dwError == ERROR_SUCCESS)
  122. {
  123. _TCHAR szPath[_MAX_PATH];
  124. DWORD cbPath = sizeof(szPath);
  125. dwError = RegQueryValueEx(hKey, _T("Directory"), NULL, NULL, (LPBYTE)szPath, &cbPath);
  126. if (dwError == ERROR_SUCCESS)
  127. {
  128. _TCHAR szDrive[_MAX_DRIVE];
  129. _TCHAR szDir[_MAX_DIR];
  130. _tsplitpath(szPath, szDrive, szDir, NULL, NULL);
  131. _tcscat(szDir, _T("Logs"));
  132. _tmakepath(szPath, szDrive, szDir, NULL, NULL);
  133. strFolder = szPath;
  134. }
  135. RegCloseKey(hKey);
  136. }
  137. return strFolder;
  138. }
  139. }