Leaked source code of windows server 2003
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.

949 lines
24 KiB

  1. //***************************************************************************
  2. //
  3. // NTEVTTHRD.CPP
  4. //
  5. // Module: WBEM NT EVENT PROVIDER
  6. //
  7. // Purpose: Contains the thread which listens for events and processes
  8. // them.
  9. //
  10. // Copyright (c) 1996-2001 Microsoft Corporation, All Rights Reserved
  11. //
  12. //***************************************************************************
  13. #include "precomp.h"
  14. #include <winperf.h>
  15. #include <time.h>
  16. #include <wbemtime.h>
  17. #define NUM_THREADS 1
  18. const DWORD CEventLogMonitor::m_PollTimeOut = 60000; // 10 minute poll period
  19. extern CCriticalSection g_ProvLock;
  20. CEventProviderManager::CEventProviderManager() : m_IsFirstSinceLogon (FALSE), m_monitorArray (NULL)
  21. {
  22. ProvThreadObject::Startup();
  23. }
  24. CEventProviderManager::~CEventProviderManager()
  25. {
  26. ProvThreadObject::Closedown();
  27. }
  28. BOOL CEventProviderManager::Register(CNTEventProvider* prov)
  29. {
  30. DebugOut(
  31. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine(_T(__FILE__),__LINE__,
  32. L"CEventProviderManager::Register\r\n");
  33. )
  34. BOOL retVal = FALSE;
  35. BOOL bUnReg = FALSE;
  36. {
  37. ScopeLock<CCriticalSection> sl(m_MonitorLock);
  38. {
  39. ScopeLock<CCriticalSection> sl(m_ControlObjects.m_Lock);
  40. prov->AddRefAll();
  41. m_ControlObjects.SetAt((UINT_PTR)prov, prov);
  42. }
  43. bUnReg = TRUE;
  44. if (NULL == m_monitorArray)
  45. {
  46. InitialiseMonitorArray();
  47. }
  48. if (NULL != m_monitorArray)
  49. {
  50. for (int x=0; x < NUM_THREADS; x++)
  51. {
  52. if ( m_monitorArray[x]->IsMonitoring() )
  53. {
  54. //at least one monitor is working
  55. retVal = TRUE;
  56. DebugOut(
  57. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  58. _T(__FILE__),__LINE__,
  59. L"CEventProviderManager::Register:Successfully monitoring monitor %lx : \r\n" ,
  60. x
  61. ) ;
  62. )
  63. break;
  64. }
  65. else
  66. {
  67. DebugOut(
  68. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  69. _T(__FILE__),__LINE__,
  70. L"CEventProviderManager::Register:Not monitoring monitor %lx : \r\n" ,
  71. x
  72. ) ;
  73. )
  74. }
  75. }
  76. }
  77. }
  78. if ((!retVal) && (bUnReg))
  79. {
  80. UnRegister(prov);
  81. }
  82. return retVal;
  83. }
  84. void CEventProviderManager::UnRegister(CNTEventProvider* prov)
  85. {
  86. DebugOut(
  87. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine(_T(__FILE__),__LINE__,
  88. L"CEventProviderManager::UnRegister\r\n");
  89. )
  90. BOOL bDestroyMonitorArray = FALSE;
  91. {
  92. ScopeLock<CCriticalSection> sl(m_MonitorLock);
  93. {
  94. {
  95. ScopeLock<CCriticalSection> sl(m_ControlObjects.m_Lock);
  96. if (m_ControlObjects.RemoveKey((UINT_PTR)prov))
  97. {
  98. prov->ReleaseAll();
  99. if (m_ControlObjects.IsEmpty() && (NULL != m_monitorArray))
  100. {
  101. bDestroyMonitorArray = TRUE;
  102. }
  103. }
  104. }
  105. if (bDestroyMonitorArray)
  106. {
  107. DestroyMonitorArray();
  108. m_monitorArray = NULL;
  109. }
  110. }
  111. }
  112. }
  113. IWbemServices* CEventProviderManager::GetNamespacePtr()
  114. {
  115. DebugOut(
  116. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine(_T(__FILE__),__LINE__,
  117. L"CEventProviderManager::GetNamespacePtr\r\n");
  118. )
  119. IWbemServices* retVal = NULL;
  120. {
  121. ScopeLock<CCriticalSection> sl(m_ControlObjects.m_Lock);
  122. POSITION pos = m_ControlObjects.GetStartPosition();
  123. if (pos)
  124. {
  125. CNTEventProvider* pCntrl;
  126. UINT_PTR key;
  127. m_ControlObjects.GetNextAssoc(pos, key, pCntrl);
  128. retVal = pCntrl->GetNamespace();
  129. }
  130. }
  131. return retVal;
  132. }
  133. void CEventProviderManager::SendEvent(IWbemClassObject* evtObj)
  134. {
  135. DebugOut(
  136. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine(_T(__FILE__),__LINE__,
  137. L"CEventProviderManager::SendEvent\r\n");
  138. )
  139. if (evtObj == NULL)
  140. {
  141. return;
  142. }
  143. //copy the list of control objects to minimize amount of work
  144. //done in locked section of code. also cannot call into webm
  145. //in blocked code may cause deadlock if webnm calls back.
  146. CList<CNTEventProvider*, CNTEventProvider*> controlObjects;
  147. {
  148. ScopeLock<CCriticalSection> sl(m_ControlObjects.m_Lock);
  149. POSITION pos = m_ControlObjects.GetStartPosition();
  150. while (NULL != pos)
  151. {
  152. CNTEventProvider* pCntrl;
  153. UINT_PTR key;
  154. m_ControlObjects.GetNextAssoc(pos, key, pCntrl);
  155. pCntrl->AddRefAll();
  156. controlObjects.AddTail(pCntrl);
  157. }
  158. }
  159. //loop through the different control objects and send the event to each
  160. while (!controlObjects.IsEmpty())
  161. {
  162. CNTEventProvider* pCntrl = controlObjects.RemoveTail();
  163. IWbemServices* ns = pCntrl->GetNamespace();
  164. IWbemObjectSink* es = pCntrl->GetEventSink();
  165. es->Indicate(1, &evtObj);
  166. es->Release();
  167. ns->Release();
  168. pCntrl->ReleaseAll();
  169. }
  170. }
  171. BOOL CEventProviderManager::InitialiseMonitorArray()
  172. {
  173. DebugOut(
  174. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine(_T(__FILE__),__LINE__,
  175. L"CEventProviderManager::InitialiseMonitorArray\r\n");
  176. )
  177. // open registry for log names
  178. HKEY hkResult;
  179. LONG status = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  180. EVENTLOG_BASE, 0,
  181. KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS,
  182. &hkResult);
  183. if (status != ERROR_SUCCESS)
  184. {
  185. DWORD t_LastError = GetLastError () ;
  186. DebugOut(
  187. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  188. _T(__FILE__),__LINE__,
  189. L"CEventProviderManager::InitialiseMonitorArray:Failed to open registry key %s , %lx: \r\n" ,
  190. EVENTLOG_BASE ,
  191. status
  192. ) ;
  193. )
  194. // indicate error
  195. return FALSE;
  196. }
  197. DWORD iValue = 0;
  198. WCHAR logname[MAX_PATH+1];
  199. DWORD lognameSize = MAX_PATH+1;
  200. CArray<CStringW*, CStringW*> logfiles;
  201. //usually three logfiles, grow in 10s!
  202. logfiles.SetSize(3, 10);
  203. // read all entries under this key to find all logfiles...
  204. while ((status = RegEnumKey(hkResult, iValue, logname, lognameSize)) != ERROR_NO_MORE_ITEMS)
  205. {
  206. // if error during read
  207. if (status != ERROR_SUCCESS)
  208. {
  209. RegCloseKey(hkResult);
  210. DebugOut(
  211. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  212. _T(__FILE__),__LINE__,
  213. L"CEventProviderManager::InitialiseMonitorArray:Failed to enumerate registry subkeys %s : \r\n" ,
  214. EVENTLOG_BASE
  215. ) ;
  216. )
  217. // indicate error
  218. return FALSE;
  219. }
  220. //store the logfilename
  221. CStringW* logfile = new CStringW(logname);
  222. logfiles.SetAtGrow(iValue, logfile);
  223. // read next parameter
  224. iValue++;
  225. } // end while
  226. RegCloseKey(hkResult);
  227. m_monitorArray = new CEventLogMonitor*[NUM_THREADS];
  228. memset(m_monitorArray, 0, NUM_THREADS * sizeof(CEventLogMonitor*));
  229. //use the array
  230. #if NUM_THREADS > 1
  231. //multi-threaded monitor
  232. //TO DO: Partition the eventlogs to the monitors
  233. //and start each monitor
  234. #else
  235. //single threaded monitor
  236. try
  237. {
  238. m_monitorArray[0] = new CEventLogMonitor(this, logfiles);
  239. m_monitorArray[0]->AddRef();
  240. (*m_monitorArray)->BeginThread();
  241. (*m_monitorArray)->WaitForStartup();
  242. (*m_monitorArray)->StartMonitoring();
  243. }
  244. catch (...)
  245. {
  246. if (m_monitorArray[0])
  247. {
  248. m_monitorArray[0]->Release();
  249. }
  250. delete [] m_monitorArray;
  251. m_monitorArray = NULL;
  252. throw;
  253. }
  254. #endif
  255. //delete array contents AFTER threads startup!
  256. LONG count = logfiles.GetSize();
  257. if (count > 0)
  258. {
  259. for (LONG x = 0; x < count; x++)
  260. {
  261. delete logfiles[x];
  262. }
  263. logfiles.RemoveAll();
  264. }
  265. return TRUE;
  266. }
  267. void CEventProviderManager::DestroyMonitorArray()
  268. {
  269. DebugOut(
  270. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine(_T(__FILE__),__LINE__,
  271. L"CEventProviderManager::DestroyMonitorArray\r\n");
  272. )
  273. if (NULL != m_monitorArray)
  274. {
  275. for (int x=0; x < NUM_THREADS; x++)
  276. {
  277. m_monitorArray[x]->PostSignalThreadShutdown();
  278. m_monitorArray[x]->Release();
  279. m_monitorArray[x] = NULL;
  280. }
  281. delete [] m_monitorArray;
  282. m_monitorArray = NULL;
  283. }
  284. }
  285. BSTR CEventProviderManager::GetLastBootTime()
  286. {
  287. DebugOut(
  288. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine(_T(__FILE__),__LINE__,
  289. L"CEventProviderManager::GetLastBootTime\r\n");
  290. )
  291. if (!m_BootTimeString.IsEmpty())
  292. {
  293. DebugOut(
  294. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine(_T(__FILE__),__LINE__,
  295. L"CEventProviderManager::GetLastBootTime returning %s\r\n",
  296. m_BootTimeString
  297. );
  298. )
  299. return m_BootTimeString.AllocSysString();
  300. }
  301. HKEY hKeyPerflib009;
  302. BSTR retStr = NULL;
  303. SYSTEM_TIMEOFDAY_INFORMATION t_TODInformation;
  304. if ( NT_SUCCESS(NtQuerySystemInformation(SystemTimeOfDayInformation,
  305. &t_TODInformation,
  306. sizeof(t_TODInformation),
  307. NULL)) )
  308. {
  309. FILETIME t_ft;
  310. memcpy(&t_ft, &t_TODInformation.BootTime, sizeof(t_TODInformation.BootTime));
  311. WBEMTime wbemboottime(t_ft);
  312. retStr = wbemboottime.GetDMTF(TRUE);
  313. m_BootTimeString = (LPCWSTR)(retStr);
  314. }
  315. DebugOut(
  316. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  317. _T(__FILE__),__LINE__,
  318. L"Returning from CEventProviderManager::GetLastBootTime with %s",
  319. (retStr == NULL ? L"NULL": retStr)
  320. ) ;
  321. )
  322. return retStr;
  323. }
  324. void CEventProviderManager :: SetFirstSinceLogon(IWbemServices* ns, IWbemContext *pCtx)
  325. {
  326. BSTR boottmStr = GetLastBootTime();
  327. if (boottmStr == NULL)
  328. {
  329. return;
  330. }
  331. IWbemClassObject* pObj = NULL;
  332. HRESULT hr = WBEM_E_OUT_OF_MEMORY ;
  333. BSTR bstrPath = SysAllocString (CONFIG_INSTANCE);
  334. if ( bstrPath )
  335. {
  336. hr = ns->GetObject(bstrPath, 0, pCtx, &pObj, NULL);
  337. SysFreeString(bstrPath);
  338. }
  339. if ( hr == WBEM_E_NOT_FOUND )
  340. {
  341. hr = WBEM_E_OUT_OF_MEMORY ;
  342. bstrPath = SysAllocString (CONFIG_CLASS);
  343. if ( bstrPath )
  344. {
  345. hr = ns->GetObject(bstrPath, 0, pCtx, &pObj, NULL);
  346. SysFreeString(bstrPath);
  347. }
  348. if (FAILED(hr))
  349. {
  350. DebugOut(
  351. CNTEventProvider::g_NTEvtDebugLog->Write (
  352. L"\r\n"
  353. ) ;
  354. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  355. _T(__FILE__),__LINE__,
  356. L"CEventProviderManager :: IsFirstSinceLogon: Failed to get config class"
  357. ) ;
  358. )
  359. }
  360. else
  361. {
  362. IWbemClassObject* pInst = NULL;
  363. hr = pObj->SpawnInstance(0, &pInst);
  364. pObj->Release();
  365. if (FAILED(hr))
  366. {
  367. DebugOut(
  368. CNTEventProvider::g_NTEvtDebugLog->Write (
  369. L"\r\n"
  370. ) ;
  371. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  372. _T(__FILE__),__LINE__,
  373. L"CEventProviderManager :: IsFirstSinceLogon: Failed to spawn config instance"
  374. ) ;
  375. )
  376. }
  377. else
  378. {
  379. VARIANT v;
  380. VariantInit(&v);
  381. hr = WBEM_E_OUT_OF_MEMORY ;
  382. v.bstrVal = SysAllocString(boottmStr);;
  383. if ( v.bstrVal )
  384. {
  385. v.vt = VT_BSTR;
  386. hr = pInst->Put(LAST_BOOT_PROP, 0, &v, 0);
  387. }
  388. VariantClear(&v);
  389. if (FAILED(hr))
  390. {
  391. DebugOut(
  392. CNTEventProvider::g_NTEvtDebugLog->Write (
  393. L"\r\n"
  394. ) ;
  395. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  396. _T(__FILE__),__LINE__,
  397. L"CEventProviderManager :: IsFirstSinceLogon: Failed to put config property"
  398. ) ;
  399. )
  400. }
  401. else
  402. {
  403. hr = ns->PutInstance(pInst, WBEM_FLAG_CREATE_ONLY, pCtx, NULL);
  404. if (FAILED(hr))
  405. {
  406. DebugOut(
  407. CNTEventProvider::g_NTEvtDebugLog->Write (
  408. L"\r\n"
  409. ) ;
  410. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  411. _T(__FILE__),__LINE__,
  412. L"CEventProviderManager :: IsFirstSinceLogon: Failed to put new config instance"
  413. ) ;
  414. )
  415. }
  416. else
  417. {
  418. m_IsFirstSinceLogon = TRUE;
  419. }
  420. }
  421. pInst->Release();
  422. }
  423. }
  424. }
  425. else
  426. {
  427. VARIANT v;
  428. hr = pObj->Get(LAST_BOOT_PROP, 0, &v, NULL, NULL);
  429. if (FAILED(hr))
  430. {
  431. DebugOut(
  432. CNTEventProvider::g_NTEvtDebugLog->Write (
  433. L"\r\n"
  434. ) ;
  435. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  436. _T(__FILE__),__LINE__,
  437. L"CEventProviderManager :: IsFirstSinceLogon: Failed to get config's last boot time from instance"
  438. ) ;
  439. )
  440. }
  441. else
  442. {
  443. if (v.vt == VT_BSTR)
  444. {
  445. if (wcscmp(v.bstrVal, boottmStr) == 0)
  446. {
  447. }
  448. else
  449. {
  450. VariantClear(&v);
  451. VariantInit(&v);
  452. hr = WBEM_E_OUT_OF_MEMORY ;
  453. v.bstrVal = SysAllocString(boottmStr);
  454. if ( v.bstrVal )
  455. {
  456. v.vt = VT_BSTR;
  457. hr = pObj->Put(LAST_BOOT_PROP, 0, &v, 0);
  458. }
  459. VariantClear (&v) ;
  460. if (FAILED(hr))
  461. {
  462. DebugOut(
  463. CNTEventProvider::g_NTEvtDebugLog->Write (
  464. L"\r\n"
  465. ) ;
  466. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  467. _T(__FILE__),__LINE__,
  468. L"CEventProviderManager :: IsFirstSinceLogon: Failed to put config property in instance"
  469. ) ;
  470. )
  471. }
  472. else
  473. {
  474. hr = ns->PutInstance(pObj, WBEM_FLAG_UPDATE_ONLY, pCtx, NULL);
  475. if (FAILED(hr))
  476. {
  477. DebugOut(
  478. CNTEventProvider::g_NTEvtDebugLog->Write (
  479. L"\r\n"
  480. ) ;
  481. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  482. _T(__FILE__),__LINE__,
  483. L"CEventProviderManager :: IsFirstSinceLogon: Failed to put config instance"
  484. ) ;
  485. )
  486. }
  487. else
  488. {
  489. m_IsFirstSinceLogon = TRUE;
  490. }
  491. }
  492. }
  493. }
  494. else
  495. {
  496. DebugOut(
  497. CNTEventProvider::g_NTEvtDebugLog->Write (
  498. L"\r\n"
  499. ) ;
  500. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  501. _T(__FILE__),__LINE__,
  502. L"CEventProviderManager :: IsFirstSinceLogon: one (maybe both) boot times are of wrong type"
  503. ) ;
  504. )
  505. }
  506. VariantClear(&v);
  507. }
  508. pObj->Release();
  509. }
  510. SysFreeString(boottmStr);
  511. }
  512. CEventLogMonitor::CEventLogMonitor(CEventProviderManager* parentptr, CArray<CStringW*, CStringW*>& logs)
  513. : ProvThreadObject(EVENTTHREADNAME, m_PollTimeOut), m_LogCount(logs.GetSize()), m_Logs(NULL),
  514. m_bMonitoring(FALSE), m_pParent(parentptr), m_Ref(0)
  515. {
  516. InterlockedIncrement(&(CNTEventProviderClassFactory::objectsInProgress));
  517. // create array from argument
  518. if (m_LogCount > 0)
  519. {
  520. //usually three logfiles, grow in 10s!
  521. m_LogNames.SetSize(3, 10);
  522. for (LONG x = 0; x < m_LogCount; x++)
  523. {
  524. CStringW* logfile = new CStringW( * logs.GetAt ( x ) );
  525. m_LogNames.SetAtGrow(x, logfile);
  526. }
  527. }
  528. }
  529. CEventLogMonitor::~CEventLogMonitor()
  530. {
  531. DebugOut(
  532. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine(_T(__FILE__),__LINE__,
  533. L"CEventLogMonitor::~CEventLogMonitor\r\n");
  534. )
  535. InterlockedDecrement(&(CNTEventProviderClassFactory::objectsInProgress));
  536. //delete array contents
  537. LONG count = m_LogNames.GetSize();
  538. if (count > 0)
  539. {
  540. for (LONG x = 0; x < count; x++)
  541. {
  542. delete m_LogNames[x];
  543. }
  544. m_LogNames.RemoveAll();
  545. }
  546. }
  547. void CEventLogMonitor::Poll()
  548. {
  549. DebugOut(
  550. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine(_T(__FILE__),__LINE__,
  551. L"CEventLogMonitor::Poll\r\n");
  552. )
  553. if (m_Logs != NULL)
  554. {
  555. for (ULONG x = 0; x < m_LogCount; x++)
  556. {
  557. if (m_Logs[x]->IsValid())
  558. {
  559. m_Logs[x]->Process();
  560. }
  561. }
  562. }
  563. DebugOut(
  564. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine(_T(__FILE__),__LINE__,
  565. L"leaving CEventLogMonitor::Poll\r\n");
  566. )
  567. }
  568. void CEventLogMonitor::TimedOut()
  569. {
  570. if (m_bMonitoring)
  571. {
  572. Poll();
  573. }
  574. }
  575. void CEventLogMonitor::StartMonitoring()
  576. {
  577. DebugOut(
  578. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  579. _T(__FILE__),__LINE__,
  580. L"CEventLogMonitor::StartMonitoring()"
  581. ) ;
  582. )
  583. //single threaded monitor
  584. //cheat, check for logon event
  585. //if we go multi-threaded this will
  586. //have to be done by the manager
  587. if (m_Logs)
  588. {
  589. DWORD logtime = 0;
  590. if (m_pParent->IsFirstSinceLogon())
  591. {
  592. //find the System log
  593. for (ULONG x = 0; x < m_LogCount; x++)
  594. {
  595. if ((m_Logs[x]->IsValid()) && (0 == _wcsicmp(L"System", m_Logs[x]->GetLogName())))
  596. {
  597. DWORD dwRecID;
  598. logtime = m_Logs[x]->FindOldEvent(LOGON_EVTID, LOGON_SOURCE, &dwRecID, LOGON_TIME);
  599. if (0 != logtime)
  600. {
  601. m_Logs[x]->SetProcessRecord(dwRecID);
  602. m_Logs[x]->Process();
  603. }
  604. else
  605. {
  606. if (!m_Logs[x]->IsValid())
  607. {
  608. m_Logs[x]->RefreshHandle();
  609. if (m_Logs[x]->IsValid())
  610. {
  611. m_Logs[x]->ReadLastRecord();
  612. }
  613. }
  614. else
  615. {
  616. m_Logs[x]->ReadLastRecord();
  617. }
  618. }
  619. break;
  620. }
  621. }
  622. if (0 != logtime)
  623. {
  624. time_t tm;
  625. time(&tm);
  626. for (x = 0; x < m_LogCount; x++)
  627. {
  628. if ((m_Logs[x]->IsValid()) && (0 != _wcsicmp(L"System", m_Logs[x]->GetLogName())))
  629. {
  630. DWORD dwRecID;
  631. m_Logs[x]->FindOldEvent(0, NULL, &dwRecID, tm - logtime);
  632. if (m_Logs[x]->IsValid())
  633. {
  634. m_Logs[x]->SetProcessRecord(dwRecID);
  635. m_Logs[x]->Process();
  636. }
  637. }
  638. }
  639. }
  640. }
  641. //now start the monitors monitoring!
  642. for (ULONG x = 0; x < m_LogCount; x++)
  643. {
  644. if (m_Logs[x]->IsValid())
  645. {
  646. DebugOut(
  647. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  648. _T(__FILE__),__LINE__,
  649. L"CEventLogMonitor::StartMonitoring() monitoring log %d of %d logs\r\n",
  650. x, m_LogCount
  651. ) ;
  652. )
  653. ScheduleTask(*(m_Logs[x]));
  654. }
  655. }
  656. }
  657. m_bMonitoring = TRUE;
  658. DebugOut(
  659. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  660. _T(__FILE__),__LINE__,
  661. L"leaving CEventLogMonitor::StartMonitoring()\r\n"
  662. ) ;
  663. )
  664. }
  665. void CEventLogMonitor::Initialise()
  666. {
  667. DebugOut(
  668. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  669. _T(__FILE__),__LINE__,
  670. L"CEventLogMonitor::Initialise\r\n"
  671. ) ;
  672. )
  673. AddRef();
  674. InitializeCom();
  675. //for each logfilename create a logfile
  676. if (m_LogCount != 0)
  677. {
  678. m_Logs = new CMonitoredEventLogFile*[m_LogCount];
  679. BOOL bValid = FALSE;
  680. for (ULONG x = 0; x < m_LogCount; x++)
  681. {
  682. CStringW* tmp = m_LogNames.GetAt(x);
  683. m_Logs[x] = new CMonitoredEventLogFile(m_pParent, *tmp);
  684. if ( !( (m_Logs[x])->IsValid() ) )
  685. {
  686. DebugOut(
  687. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  688. _T(__FILE__),__LINE__,
  689. L"CEventLogMonitor::Initialise logfile %d named %s is invalid\r\n",
  690. x, *tmp
  691. ) ;
  692. )
  693. }
  694. else
  695. {
  696. DebugOut(
  697. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  698. _T(__FILE__),__LINE__,
  699. L"CEventLogMonitor::Initialise logfile %d named %s is valid\r\n",
  700. x, *tmp
  701. ) ;
  702. )
  703. bValid = TRUE;
  704. }
  705. }
  706. if (!bValid)
  707. {
  708. delete [] m_Logs;
  709. m_Logs = NULL;
  710. }
  711. }
  712. else
  713. {
  714. //should never happen
  715. DebugOut(
  716. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  717. _T(__FILE__),__LINE__,
  718. L"CEventLogMonitor::Initialise() !!!NO LOGFILES TO MONITOR!!!\r\n"
  719. ) ;
  720. )
  721. }
  722. DebugOut(
  723. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  724. _T(__FILE__),__LINE__,
  725. L"leaving CEventLogMonitor::Initialise()\r\n"
  726. ) ;
  727. )
  728. }
  729. void CEventLogMonitor::Uninitialise()
  730. {
  731. DebugOut(
  732. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine(_T(__FILE__),__LINE__,
  733. L"CEventLogMonitor::Uninitialise\r\n");
  734. )
  735. if (m_Logs != NULL)
  736. {
  737. for (ULONG x = 0; x < m_LogCount; x++)
  738. {
  739. delete m_Logs[x];
  740. }
  741. delete [] m_Logs;
  742. }
  743. CoUninitialize();
  744. Release();
  745. DebugOut(
  746. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine(_T(__FILE__),__LINE__,
  747. L"CEventLogMonitor::Uninitialise: Leaving method\r\n");
  748. )
  749. }
  750. LONG CEventLogMonitor ::AddRef(void)
  751. {
  752. return InterlockedIncrement ( & m_Ref ) ;
  753. }
  754. LONG CEventLogMonitor ::Release(void)
  755. {
  756. LONG t_Ref ;
  757. if ( ( t_Ref = InterlockedDecrement ( & m_Ref ) ) == 0 )
  758. {
  759. delete this ;
  760. return 0 ;
  761. }
  762. else
  763. {
  764. return t_Ref ;
  765. }
  766. }