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.

1699 lines
47 KiB

  1. //*************************************************************
  2. // File name: Events.CPP
  3. //
  4. // Description: Event log entries for RSOP
  5. //
  6. //
  7. // Microsoft Confidential
  8. // Copyright (c) Microsoft Corporation 2000
  9. // All rights reserved
  10. //
  11. //*************************************************************
  12. #include "main.h"
  13. #include "rsoputil.h"
  14. EVENTLOGENTRY ExceptionEventEntries[] =
  15. {
  16. {1036, {0}, TEXT("Application"), TEXT("userenv"), NULL, NULL},
  17. {1037, {0}, TEXT("Application"), TEXT("userenv"), NULL, NULL},
  18. {1038, {0}, TEXT("Application"), TEXT("userenv"), NULL, NULL},
  19. {1039, {0}, TEXT("Application"), TEXT("userenv"), NULL, NULL},
  20. {1040, {0}, TEXT("Application"), TEXT("userenv"), NULL, NULL},
  21. {1041, {0}, TEXT("Application"), TEXT("userenv"), NULL, NULL},
  22. {1085, {0}, TEXT("Application"), TEXT("userenv"), NULL, NULL}
  23. };
  24. DWORD dwExceptionEventEntriesSize = 7;
  25. ///////////////////////////////////////////////////////////////////////////////
  26. // //
  27. // CEvents implementation //
  28. // //
  29. ///////////////////////////////////////////////////////////////////////////////
  30. CEvents::CEvents(void)
  31. {
  32. InterlockedIncrement(&g_cRefThisDll);
  33. m_pEventEntries = NULL;
  34. }
  35. CEvents::~CEvents()
  36. {
  37. FreeData();
  38. InterlockedDecrement(&g_cRefThisDll);
  39. }
  40. BOOL CEvents::AddEntry(LPTSTR lpEventLogName, LPTSTR lpEventSourceName, LPTSTR lpText,
  41. DWORD dwEventID, FILETIME *ftTime)
  42. {
  43. DWORD dwSize;
  44. LPEVENTLOGENTRY lpItem, lpTemp, pPrev;
  45. HRESULT hr;
  46. ULONG ulNoChars;
  47. //
  48. // Check if this entry exists already
  49. //
  50. lpTemp = m_pEventEntries;
  51. while (lpTemp)
  52. {
  53. if (dwEventID == lpTemp->dwEventID)
  54. {
  55. if (!lstrcmpi(lpEventLogName, lpTemp->lpEventLogName))
  56. {
  57. if (!lstrcmpi(lpEventSourceName, lpTemp->lpEventSourceName))
  58. {
  59. if (ftTime->dwLowDateTime == lpTemp->ftEventTime.dwLowDateTime)
  60. {
  61. if (ftTime->dwHighDateTime == lpTemp->ftEventTime.dwHighDateTime)
  62. {
  63. return TRUE;
  64. }
  65. }
  66. }
  67. }
  68. }
  69. lpTemp = lpTemp->pNext;
  70. }
  71. //
  72. // Calculate the size of the new item
  73. //
  74. dwSize = sizeof (EVENTLOGENTRY);
  75. dwSize += ((lstrlen(lpEventLogName) + 1) * sizeof(TCHAR));
  76. dwSize += ((lstrlen(lpEventSourceName) + 1) * sizeof(TCHAR));
  77. dwSize += ((lstrlen(lpText) + 1) * sizeof(TCHAR));
  78. //
  79. // Allocate space for it
  80. //
  81. lpItem = (LPEVENTLOGENTRY) LocalAlloc (LPTR, dwSize);
  82. if (!lpItem) {
  83. DebugMsg((DM_WARNING, TEXT("CEvents::AddEntry: Failed to allocate memory with %d"),
  84. GetLastError()));
  85. return FALSE;
  86. }
  87. //
  88. // Fill in item
  89. //
  90. ulNoChars = (dwSize - sizeof(EVENTLOGENTRY))/sizeof(WCHAR);
  91. lpItem->lpEventLogName = (LPTSTR)(((LPBYTE)lpItem) + sizeof(EVENTLOGENTRY));
  92. hr = StringCchCopy (lpItem->lpEventLogName, ulNoChars, lpEventLogName);
  93. if (SUCCEEDED(hr))
  94. {
  95. ulNoChars = ulNoChars - (lstrlen (lpItem->lpEventLogName) + 1);
  96. lpItem->lpEventSourceName = lpItem->lpEventLogName + lstrlen (lpItem->lpEventLogName) + 1;
  97. hr = StringCchCopy (lpItem->lpEventSourceName, ulNoChars, lpEventSourceName);
  98. }
  99. if (SUCCEEDED(hr))
  100. {
  101. ulNoChars = ulNoChars - (lstrlen (lpItem->lpEventSourceName) + 1);
  102. lpItem->lpText = lpItem->lpEventSourceName + lstrlen (lpItem->lpEventSourceName) + 1;
  103. hr = StringCchCopy (lpItem->lpText, ulNoChars, lpText);
  104. }
  105. if (FAILED(hr))
  106. {
  107. DebugMsg((DM_WARNING, TEXT("CEvents::AddEntry: Failed to copy event item with %d"), hr));
  108. LocalFree(lpItem);
  109. return FALSE;
  110. }
  111. lpItem->dwEventID = dwEventID;
  112. CopyMemory ((LPBYTE)&lpItem->ftEventTime, ftTime, sizeof(FILETIME));
  113. //
  114. // Add item to the link list
  115. //
  116. if (m_pEventEntries)
  117. {
  118. if (CompareFileTime(ftTime, &m_pEventEntries->ftEventTime) < 0)
  119. {
  120. lpItem->pNext = m_pEventEntries;
  121. m_pEventEntries = lpItem;
  122. }
  123. else
  124. {
  125. pPrev = m_pEventEntries;
  126. lpTemp = m_pEventEntries->pNext;
  127. while (lpTemp)
  128. {
  129. if (lpTemp->pNext)
  130. {
  131. if ((CompareFileTime(ftTime, &lpTemp->ftEventTime) >= 0) &&
  132. (CompareFileTime(ftTime, &lpTemp->pNext->ftEventTime) <= 0))
  133. {
  134. lpItem->pNext = lpTemp->pNext;
  135. lpTemp->pNext = lpItem;
  136. break;
  137. }
  138. }
  139. pPrev = lpTemp;
  140. lpTemp = lpTemp->pNext;
  141. }
  142. if (!lpTemp)
  143. {
  144. pPrev->pNext = lpItem;
  145. }
  146. }
  147. }
  148. else
  149. {
  150. m_pEventEntries = lpItem;
  151. }
  152. return TRUE;
  153. }
  154. VOID CEvents::FreeData()
  155. {
  156. if (m_pEventEntries)
  157. {
  158. LPEVENTLOGENTRY lpTemp;
  159. do {
  160. lpTemp = m_pEventEntries->pNext;
  161. LocalFree (m_pEventEntries);
  162. m_pEventEntries = lpTemp;
  163. } while (lpTemp);
  164. }
  165. }
  166. STDMETHODIMP CEvents::SecondsSince1970ToFileTime(DWORD dwSecondsSince1970,
  167. FILETIME *pftTime)
  168. {
  169. // Seconds since the start of 1970 -> 64 bit Time value
  170. LARGE_INTEGER liTime;
  171. RtlSecondsSince1970ToTime(dwSecondsSince1970, &liTime);
  172. //
  173. // The time is in UTC
  174. //
  175. pftTime->dwLowDateTime = liTime.LowPart;
  176. pftTime->dwHighDateTime = liTime.HighPart;
  177. return S_OK;
  178. }
  179. LPTSTR * CEvents::BuildStringArray(LPTSTR lpStrings, DWORD dwStringCount)
  180. {
  181. LPTSTR lpTemp;
  182. LPTSTR *lpResult;
  183. if (!lpStrings || !dwStringCount)
  184. {
  185. return NULL;
  186. }
  187. //
  188. // Allocate a new array to hold the pointers
  189. //
  190. lpResult = (LPTSTR *) LocalAlloc (LPTR, dwStringCount * sizeof(LPTSTR));
  191. if (lpResult)
  192. {
  193. //
  194. // Save the pointers
  195. //
  196. lpTemp = lpStrings;
  197. for ( DWORD dwIndex = 0; dwIndex < dwStringCount; dwIndex++)
  198. {
  199. lpResult[dwIndex] = lpTemp;
  200. lpTemp += lstrlen(lpTemp) + 1;
  201. }
  202. }
  203. return lpResult;
  204. }
  205. LPTSTR CEvents::BuildMessage(LPTSTR lpMsg, LPTSTR *lpStrings, DWORD dwStringCount,
  206. HMODULE hParamFile)
  207. {
  208. LPTSTR lpFullMsg = NULL;
  209. LPTSTR lpSrcIndex;
  210. LPTSTR lpTemp, lpNum;
  211. TCHAR cChar, cTemp;
  212. TCHAR cCharStr[2] = {0,0};
  213. DWORD dwCharCount = 1, dwTemp;
  214. BOOL bAdd;
  215. TCHAR szNumStr[10];
  216. DWORD dwIndex;
  217. LPTSTR lpParamMsg;
  218. HRESULT hr;
  219. if ( !lpMsg || (dwStringCount && !lpStrings) )
  220. {
  221. return 0;
  222. }
  223. lpFullMsg = (LPTSTR) LocalAlloc (LPTR, dwCharCount * sizeof(TCHAR));
  224. if (!lpFullMsg)
  225. {
  226. return NULL;
  227. }
  228. lpSrcIndex = lpMsg;
  229. while (*lpSrcIndex)
  230. {
  231. bAdd = TRUE;
  232. cChar = *lpSrcIndex;
  233. if (cChar == TEXT('%'))
  234. {
  235. cTemp = *(lpSrcIndex + 1);
  236. if (ISDIGIT (cTemp))
  237. {
  238. if (dwStringCount == 0)
  239. {
  240. goto LoopAgain;
  241. }
  242. //
  243. // Found a replaceable parameter from the passed in strings
  244. //
  245. lpNum = lpSrcIndex + 1;
  246. //
  247. // Pull the string index off
  248. //
  249. ZeroMemory (szNumStr, sizeof(szNumStr));
  250. while (ISDIGIT(*lpNum))
  251. {
  252. cCharStr[0] = *lpNum;
  253. hr = StringCchCat (szNumStr, ARRAYSIZE(szNumStr),cCharStr );
  254. if (FAILED(hr))
  255. {
  256. LocalFree(lpFullMsg);
  257. lpFullMsg = NULL;
  258. goto Exit;
  259. }
  260. if (lstrlen (szNumStr) == (ARRAYSIZE(szNumStr) - 2))
  261. {
  262. goto LoopAgain;
  263. }
  264. lpNum++;
  265. }
  266. //
  267. // Convert the string index to a dword
  268. //
  269. dwIndex = 0;
  270. StringToNum(szNumStr, (UINT *)&dwIndex);
  271. //
  272. // Subtrack 1 to make it zero based
  273. //
  274. if (dwIndex)
  275. {
  276. dwIndex--;
  277. }
  278. if (dwIndex > dwStringCount)
  279. {
  280. goto LoopAgain;
  281. }
  282. // shorter strings will go to the else condition
  283. if ( (*(lpStrings[dwIndex]) == TEXT('%')) && (*(lpStrings[dwIndex]+1) == TEXT('%')) ) {
  284. DWORD dwArgIndex;
  285. LPTSTR lpArgString;
  286. LPTSTR lpEnd;
  287. TCHAR szNumArg[10];
  288. lpArgString = lpStrings[dwIndex]+2;
  289. lpEnd = szNumArg;
  290. for(int i=0; i<9 && ISDIGIT(*lpArgString); i++)
  291. {
  292. *lpEnd = *lpArgString;
  293. lpEnd++;
  294. lpArgString++;
  295. }
  296. *lpEnd = TEXT('\0');
  297. //
  298. // Convert the string index to a dword
  299. //
  300. dwArgIndex = 0;
  301. StringToNum(szNumArg, (UINT *)&dwArgIndex);
  302. //
  303. // Convert the string number to a dword
  304. //
  305. lpParamMsg = NULL;
  306. if (hParamFile)
  307. {
  308. FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE |
  309. FORMAT_MESSAGE_IGNORE_INSERTS, (LPCVOID) hParamFile,
  310. dwArgIndex, 0, (LPTSTR)&lpParamMsg, 1, NULL);
  311. }
  312. else
  313. {
  314. FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
  315. FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
  316. dwArgIndex, 0, (LPTSTR)&lpParamMsg, 1, NULL);
  317. }
  318. LPWSTR szMsgArg;
  319. if (lpParamMsg) {
  320. szMsgArg = lpParamMsg;
  321. }
  322. else {
  323. szMsgArg = lpStrings[dwIndex];
  324. }
  325. dwTemp = lstrlen (szMsgArg) + dwCharCount;
  326. lpTemp = (LPTSTR) LocalReAlloc (lpFullMsg, dwTemp * sizeof(TCHAR),
  327. LMEM_MOVEABLE | LMEM_ZEROINIT);
  328. if (!lpTemp)
  329. {
  330. LocalFree (lpFullMsg);
  331. lpFullMsg = NULL;
  332. goto Exit;
  333. }
  334. dwCharCount = dwTemp;
  335. lpFullMsg = lpTemp;
  336. hr = StringCchCat (lpFullMsg, dwCharCount, szMsgArg);
  337. if (FAILED(hr))
  338. {
  339. LocalFree(lpFullMsg);
  340. lpFullMsg = NULL;
  341. goto Exit;
  342. }
  343. if (lpParamMsg) {
  344. LocalFree(lpParamMsg);
  345. }
  346. }
  347. else {
  348. //
  349. // Add the string to the buffer
  350. //
  351. dwTemp = lstrlen (lpStrings[dwIndex]) + dwCharCount;
  352. lpTemp = (LPTSTR) LocalReAlloc (lpFullMsg, dwTemp * sizeof(TCHAR),
  353. LMEM_MOVEABLE | LMEM_ZEROINIT);
  354. if (!lpTemp)
  355. {
  356. LocalFree (lpFullMsg);
  357. lpFullMsg = NULL;
  358. goto Exit;
  359. }
  360. dwCharCount = dwTemp;
  361. lpFullMsg = lpTemp;
  362. hr = StringCchCat (lpFullMsg, dwCharCount, lpStrings[dwIndex]);
  363. if (FAILED(hr))
  364. {
  365. LocalFree(lpFullMsg);
  366. lpFullMsg = NULL;
  367. goto Exit;
  368. }
  369. }
  370. lpSrcIndex = lpNum - 1;
  371. bAdd = FALSE;
  372. }
  373. else if (cTemp == TEXT('%'))
  374. {
  375. cTemp = *(lpSrcIndex + 2);
  376. if (cTemp == TEXT('%'))
  377. {
  378. //
  379. // Found a replacable parameter from the parameter file
  380. //
  381. lpNum = lpSrcIndex + 3;
  382. //
  383. // Pull the string index off
  384. //
  385. ZeroMemory (szNumStr, sizeof(szNumStr));
  386. while (ISDIGIT(*lpNum))
  387. {
  388. cCharStr[0] = *lpNum;
  389. hr = StringCchCat (szNumStr, ARRAYSIZE(szNumStr), cCharStr);
  390. if (FAILED(hr))
  391. {
  392. LocalFree(lpFullMsg);
  393. lpFullMsg = NULL;
  394. goto Exit;
  395. }
  396. if (lstrlen (szNumStr) == (ARRAYSIZE(szNumStr) - 2))
  397. {
  398. goto LoopAgain;
  399. }
  400. lpNum++;
  401. }
  402. //
  403. // Convert the string index to a dword
  404. //
  405. dwIndex = 0;
  406. StringToNum(szNumStr, (UINT *)&dwIndex);
  407. //
  408. // Subtrack 1 to make it zero based
  409. //
  410. if (dwIndex)
  411. {
  412. dwIndex--;
  413. }
  414. if (dwIndex > dwStringCount)
  415. {
  416. goto LoopAgain;
  417. }
  418. //
  419. // Convert the string number to a dword
  420. //
  421. StringToNum(lpStrings[dwIndex], (UINT *)&dwIndex);
  422. lpParamMsg = NULL;
  423. if (hParamFile)
  424. {
  425. FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE |
  426. FORMAT_MESSAGE_IGNORE_INSERTS, (LPCVOID) hParamFile,
  427. dwIndex, 0, (LPTSTR)&lpParamMsg, 1, NULL);
  428. }
  429. else
  430. {
  431. FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
  432. FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
  433. dwIndex, 0, (LPTSTR)&lpParamMsg, 1, NULL);
  434. }
  435. if (lpParamMsg)
  436. {
  437. lpTemp = lpParamMsg + lstrlen(lpParamMsg) - 2;
  438. *lpTemp = TEXT('\0');
  439. //
  440. // Add the string to the buffer
  441. //
  442. dwTemp = lstrlen (lpParamMsg) + dwCharCount;
  443. lpTemp = (LPTSTR) LocalReAlloc (lpFullMsg, dwTemp * sizeof(TCHAR),
  444. LMEM_MOVEABLE | LMEM_ZEROINIT);
  445. if (!lpTemp)
  446. {
  447. LocalFree (lpFullMsg);
  448. lpFullMsg = NULL;
  449. goto Exit;
  450. }
  451. dwCharCount = dwTemp;
  452. lpFullMsg = lpTemp;
  453. hr = StringCchCat (lpFullMsg, dwCharCount, lpParamMsg);
  454. if (FAILED(hr))
  455. {
  456. LocalFree(lpFullMsg);
  457. lpFullMsg = NULL;
  458. goto Exit;
  459. }
  460. lpSrcIndex = lpNum - 1;
  461. bAdd = FALSE;
  462. LocalFree (lpParamMsg);
  463. }
  464. }
  465. }
  466. }
  467. LoopAgain:
  468. if (bAdd)
  469. {
  470. //
  471. // Add this character to the buffer
  472. //
  473. dwCharCount++;
  474. lpTemp = (LPTSTR) LocalReAlloc (lpFullMsg, dwCharCount * sizeof(TCHAR),
  475. LMEM_MOVEABLE | LMEM_ZEROINIT);
  476. if (!lpTemp)
  477. {
  478. LocalFree (lpFullMsg);
  479. lpFullMsg = NULL;
  480. goto Exit;
  481. }
  482. lpFullMsg = lpTemp;
  483. cCharStr[0] = cChar;
  484. hr = StringCchCat (lpFullMsg, dwCharCount, cCharStr);
  485. if (FAILED(hr))
  486. {
  487. LocalFree(lpFullMsg);
  488. lpFullMsg = NULL;
  489. goto Exit;
  490. }
  491. }
  492. lpSrcIndex++;
  493. }
  494. Exit:
  495. return lpFullMsg;
  496. }
  497. STDMETHODIMP CEvents::SaveEventLogEntry (PEVENTLOGRECORD pEntry,
  498. LPTSTR lpEventLogName,
  499. LPTSTR lpEventSourceName,
  500. FILETIME *ftEntry)
  501. {
  502. LPTSTR lpRegKey = NULL;
  503. HKEY hKey = NULL;
  504. TCHAR szEventFile[MAX_PATH];
  505. TCHAR szExpEventFile[MAX_PATH];
  506. TCHAR szParamFile[MAX_PATH] = {0};
  507. TCHAR szExpParamFile[MAX_PATH] = {0};
  508. HRESULT hr = S_OK;
  509. DWORD dwType, dwSize;
  510. HMODULE hEventFile = NULL;
  511. HMODULE hParamFile = NULL;
  512. LPTSTR lpMsg, *lpStrings, lpFullMsg;
  513. LPBYTE lpData;
  514. ULONG ulNoChars;
  515. ulNoChars = lstrlen(lpEventLogName) + lstrlen(lpEventSourceName) + 60;
  516. lpRegKey = new TCHAR [ulNoChars];
  517. if (!lpRegKey)
  518. {
  519. DebugMsg((DM_WARNING, TEXT("CEvents::SaveEventLogEntry: Failed to alloc memory for key name")));
  520. hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
  521. goto Exit;
  522. }
  523. hr = StringCchPrintf(lpRegKey,
  524. ulNoChars,
  525. TEXT("SYSTEM\\CurrentControlSet\\Services\\EventLog\\%s\\%s"),
  526. lpEventLogName,
  527. lpEventSourceName);
  528. if (FAILED(hr))
  529. {
  530. DebugMsg((DM_WARNING, TEXT("CEvents::SaveEventLogEntry: Failed to copy registry key name")));
  531. goto Exit;
  532. }
  533. if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, lpRegKey, 0, KEY_READ, &hKey) != ERROR_SUCCESS)
  534. {
  535. DebugMsg((DM_WARNING, TEXT("CEvents::SaveEventLogEntry: Failed to open reg key for %s"), lpRegKey));
  536. hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
  537. goto Exit;
  538. }
  539. dwSize = sizeof(szEventFile);
  540. if (RegQueryValueEx (hKey, TEXT("EventMessageFile"), NULL, &dwType, (LPBYTE) szEventFile,
  541. &dwSize) != ERROR_SUCCESS)
  542. {
  543. DebugMsg((DM_WARNING, TEXT("CEvents::SaveEventLogEntry: Failed to query dll pathname for %s"), lpRegKey));
  544. hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
  545. goto Exit;
  546. }
  547. ExpandEnvironmentStrings (szEventFile, szExpEventFile, ARRAYSIZE(szExpEventFile));
  548. dwSize = sizeof(szParamFile);
  549. if (RegQueryValueEx (hKey, TEXT("ParameterMessageFile"), NULL, &dwType, (LPBYTE) szParamFile,
  550. &dwSize) == ERROR_SUCCESS)
  551. {
  552. ExpandEnvironmentStrings (szParamFile, szExpParamFile, ARRAYSIZE(szExpParamFile));
  553. }
  554. hEventFile = LoadLibraryEx (szExpEventFile, NULL, LOAD_LIBRARY_AS_DATAFILE);
  555. if (!hEventFile)
  556. {
  557. DebugMsg((DM_WARNING, TEXT("CEvents::SaveEventLogEntry: Failed to loadlibrary dll %s with %d"), szExpEventFile, GetLastError()));
  558. hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
  559. goto Exit;
  560. }
  561. if (szExpParamFile[0] != TEXT('\0'))
  562. {
  563. if (!StrStrI(szExpParamFile, TEXT("kernel32")))
  564. {
  565. hParamFile = LoadLibraryEx (szExpParamFile, NULL, LOAD_LIBRARY_AS_DATAFILE);
  566. }
  567. }
  568. lpData = (LPBYTE)((LPBYTE)pEntry + pEntry->StringOffset);
  569. lpStrings = BuildStringArray((LPTSTR) lpData, pEntry->NumStrings);
  570. lpMsg = NULL;
  571. if (FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE |
  572. FORMAT_MESSAGE_IGNORE_INSERTS, (LPCVOID) hEventFile,
  573. pEntry->EventID, 0, (LPTSTR)&lpMsg, 1, NULL))
  574. {
  575. lpFullMsg = BuildMessage(lpMsg, lpStrings, pEntry->NumStrings, hParamFile);
  576. if (lpFullMsg)
  577. {
  578. AddEntry(lpEventLogName, lpEventSourceName, lpFullMsg, pEntry->EventID, ftEntry);
  579. LocalFree (lpFullMsg);
  580. }
  581. LocalFree (lpMsg);
  582. }
  583. if (lpStrings)
  584. {
  585. LocalFree (lpStrings);
  586. }
  587. Exit:
  588. if (hEventFile)
  589. {
  590. FreeLibrary (hEventFile);
  591. }
  592. if (hParamFile)
  593. {
  594. FreeLibrary (hParamFile);
  595. }
  596. if (hKey)
  597. {
  598. RegCloseKey (hKey);
  599. }
  600. if (lpRegKey)
  601. {
  602. delete [] lpRegKey;
  603. }
  604. return S_OK;
  605. }
  606. STDMETHODIMP CEvents::ParseEventLogRecords (PEVENTLOGRECORD lpEntries,
  607. DWORD dwEntriesSize,
  608. LPTSTR lpEventLogName,
  609. LPTSTR lpEventSourceName,
  610. DWORD dwEventID,
  611. FILETIME * pBeginTime,
  612. FILETIME * pEndTime)
  613. {
  614. PEVENTLOGRECORD pEntry = lpEntries;
  615. FILETIME ftEntry;
  616. LONG lResult;
  617. LPTSTR lpSource;
  618. TCHAR szCurrentTime[100];
  619. DWORD dwTotal = 0;
  620. while (dwTotal < dwEntriesSize)
  621. {
  622. if (pEntry->EventType != EVENTLOG_INFORMATION_TYPE)
  623. {
  624. SecondsSince1970ToFileTime (pEntry->TimeWritten, &ftEntry);
  625. lpSource = (LPTSTR)(((LPBYTE)pEntry) + sizeof(EVENTLOGRECORD));
  626. // DebugMsg((DM_VERBOSE, TEXT("CEvents::ParseEventLogRecords: Found %s at %s"),
  627. // lpSource, ConvertTimeToDisplayTime (NULL, &ftEntry, szCurrentTime)));
  628. if ((CompareFileTime (&ftEntry, pBeginTime) >= 0) &&
  629. (CompareFileTime (&ftEntry, pEndTime) <= 0))
  630. {
  631. if (!lstrcmpi(lpSource, lpEventSourceName))
  632. {
  633. //
  634. // The dwEventID parameter is optional. If it is non-zero, then
  635. // we're looking for a specific event message. If it is zero,
  636. // consider the id to be a wildcard and grab all the events that
  637. // the remaining criteria.
  638. //
  639. if (dwEventID)
  640. {
  641. if (dwEventID == pEntry->EventID)
  642. {
  643. SaveEventLogEntry (pEntry, lpEventLogName, lpEventSourceName, &ftEntry);
  644. }
  645. }
  646. else
  647. {
  648. SaveEventLogEntry (pEntry, lpEventLogName, lpEventSourceName, &ftEntry);
  649. }
  650. }
  651. }
  652. }
  653. dwTotal += pEntry->Length;
  654. pEntry = (PEVENTLOGRECORD)(((LPBYTE)pEntry) + pEntry->Length);
  655. }
  656. return S_OK;
  657. }
  658. STDMETHODIMP CEvents::QueryForEventLogEntries (LPTSTR lpComputerName,
  659. LPTSTR lpEventLogName,
  660. LPTSTR lpEventSourceName,
  661. DWORD dwEventID,
  662. SYSTEMTIME * pBeginTime,
  663. SYSTEMTIME * pEndTime)
  664. {
  665. LPTSTR lpServerName, lpTemp = lpComputerName;
  666. HANDLE hLog;
  667. ULONG ulSize;
  668. TCHAR szBuffer[300];
  669. LPBYTE lpEntries;
  670. DWORD dwEntriesBufferSize = 4096;
  671. DWORD dwBytesRead, dwBytesNeeded;
  672. FILETIME ftBeginTime, ftEndTime;
  673. TCHAR szBeginTime[100];
  674. TCHAR szEndTime[100];
  675. HRESULT hr;
  676. ULONG ulNoChars;
  677. DebugMsg((DM_VERBOSE, TEXT("CEvents::QueryForEventLogEntries: Entering for %s,%s between %s and %s"),
  678. lpEventLogName, lpEventSourceName,
  679. ConvertTimeToDisplayTime (pBeginTime, NULL, szBeginTime, ARRAYSIZE(szBeginTime)),
  680. ConvertTimeToDisplayTime (pEndTime, NULL, szEndTime, ARRAYSIZE(szEndTime))));
  681. //
  682. // Check if this is the local machine
  683. //
  684. if (!lstrcmpi(lpComputerName, TEXT(".")))
  685. {
  686. ulSize = ARRAYSIZE(szBuffer);
  687. if ( !GetComputerNameEx (ComputerNameNetBIOS, szBuffer, &ulSize) )
  688. {
  689. DebugMsg((DM_WARNING, TEXT("CEvents::QueryForEventLogEntries: GetComputerNameEx() failed.")));
  690. return HRESULT_FROM_WIN32(GetLastError());
  691. }
  692. lpTemp = szBuffer;
  693. }
  694. ulNoChars = lstrlen(lpTemp) + 3;
  695. lpServerName = new TCHAR [ulNoChars];
  696. if (!lpServerName)
  697. {
  698. DebugMsg((DM_WARNING, TEXT("CEvents::QueryForEventLogEntries: Failed to alloc memory for server name")));
  699. return HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
  700. }
  701. hr = StringCchCopy (lpServerName, ulNoChars, TEXT("\\\\"));
  702. if (SUCCEEDED(hr))
  703. {
  704. hr = StringCchCat (lpServerName, ulNoChars, lpTemp);
  705. }
  706. if (FAILED(hr))
  707. {
  708. delete [] lpServerName;
  709. return hr;
  710. }
  711. //
  712. // Open the event log
  713. //
  714. hLog = OpenEventLog (lpServerName, lpEventLogName);
  715. if (!hLog)
  716. {
  717. DebugMsg((DM_WARNING, TEXT("CEvents::QueryForEventLogEntries: Failed to open event log on %s with %d"),
  718. lpServerName, GetLastError()));
  719. }
  720. delete [] lpServerName;
  721. if (!hLog)
  722. return HRESULT_FROM_WIN32(GetLastError());
  723. //
  724. // Allocate a buffer to read the entries into
  725. //
  726. lpEntries = (LPBYTE) LocalAlloc (LPTR, dwEntriesBufferSize);
  727. if (!lpEntries)
  728. {
  729. DebugMsg((DM_WARNING, TEXT("CEvents::QueryForEventLogEntries: Failed to alloc memory for server name")));
  730. CloseEventLog (hLog);
  731. return HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
  732. }
  733. SystemTimeToFileTime (pBeginTime, &ftBeginTime);
  734. SystemTimeToFileTime (pEndTime, &ftEndTime);
  735. while (TRUE)
  736. {
  737. ZeroMemory (lpEntries, dwEntriesBufferSize);
  738. if (ReadEventLog (hLog, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_FORWARDS_READ, 0, lpEntries, dwEntriesBufferSize,
  739. &dwBytesRead, &dwBytesNeeded))
  740. {
  741. ParseEventLogRecords ((PEVENTLOGRECORD) lpEntries, dwBytesRead, lpEventLogName, lpEventSourceName, dwEventID, &ftBeginTime, &ftEndTime);
  742. }
  743. else
  744. {
  745. if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
  746. {
  747. dwEntriesBufferSize = dwBytesNeeded;
  748. LocalFree (lpEntries);
  749. lpEntries = (LPBYTE) LocalAlloc (LPTR, dwEntriesBufferSize);
  750. if (!lpEntries)
  751. {
  752. DebugMsg((DM_WARNING, TEXT("CEvents::QueryForEventLogEntries: Failed to alloc memory")));
  753. CloseEventLog (hLog);
  754. return HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
  755. }
  756. }
  757. else
  758. {
  759. break;
  760. }
  761. }
  762. }
  763. LocalFree (lpEntries);
  764. CloseEventLog (hLog);
  765. DebugMsg((DM_VERBOSE, TEXT("CEvents::QueryForEventLogEntries: Leaving ===")));
  766. return S_OK;
  767. }
  768. STDMETHODIMP CEvents::GetEventLogEntryText (LPOLESTR pszEventSource,
  769. LPOLESTR pszEventLogName,
  770. LPOLESTR pszEventTime,
  771. DWORD dwEventID,
  772. LPOLESTR *ppszText)
  773. {
  774. XBStr xbstrWbemTime = pszEventTime;
  775. SYSTEMTIME EventTime;
  776. FILETIME ftLower, ftUpper;
  777. ULARGE_INTEGER ulTime;
  778. LPEVENTLOGENTRY lpTemp;
  779. LPOLESTR lpMsg = NULL, lpTempMsg;
  780. ULONG ulSize;
  781. TCHAR szLowerTime[100];
  782. TCHAR szUpperTime[100];
  783. HRESULT hr;
  784. if (!ppszText)
  785. {
  786. return HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER);
  787. }
  788. WbemTimeToSystemTime(xbstrWbemTime, EventTime);
  789. //
  790. // Subtrack 1 second to EventTime to get the lower end of the range
  791. //
  792. SystemTimeToFileTime (&EventTime, &ftLower);
  793. ulTime.LowPart = ftLower.dwLowDateTime;
  794. ulTime.HighPart = ftLower.dwHighDateTime;
  795. ulTime.QuadPart = ulTime.QuadPart - (10000000 * 1); // 1 second
  796. ftLower.dwLowDateTime = ulTime.LowPart;
  797. ftLower.dwHighDateTime = ulTime.HighPart;
  798. //
  799. // Add 2 seconds to determine the upper bounds
  800. //
  801. ulTime.QuadPart = ulTime.QuadPart + (10000000 * 2); // 2 second
  802. ftUpper.dwLowDateTime = ulTime.LowPart;
  803. ftUpper.dwHighDateTime = ulTime.HighPart;
  804. DebugMsg((DM_VERBOSE, TEXT("CEvents::GetEventLogEntryText: Entering for %s,%s,%d between %s and %s"),
  805. pszEventLogName, pszEventSource, dwEventID,
  806. ConvertTimeToDisplayTime (NULL, &ftLower, szLowerTime, ARRAYSIZE(szLowerTime)),
  807. ConvertTimeToDisplayTime (NULL, &ftUpper, szUpperTime, ARRAYSIZE(szUpperTime))));
  808. //
  809. // Loop through the entries looking for matches
  810. //
  811. lpTemp = m_pEventEntries;
  812. while (lpTemp)
  813. {
  814. if (lpTemp->dwEventID == dwEventID)
  815. {
  816. if (!lstrcmpi(lpTemp->lpEventLogName, pszEventLogName))
  817. {
  818. if (!lstrcmpi(lpTemp->lpEventSourceName, pszEventSource))
  819. {
  820. if ((CompareFileTime (&lpTemp->ftEventTime, &ftLower) >= 0) &&
  821. (CompareFileTime (&lpTemp->ftEventTime, &ftUpper) <= 0))
  822. {
  823. if (lpMsg)
  824. {
  825. ulSize = lstrlen(lpMsg);
  826. ulSize += lstrlen(lpTemp->lpText) + 3;
  827. lpTempMsg = (LPOLESTR) CoTaskMemRealloc (lpMsg, ulSize * sizeof(TCHAR));
  828. if (!lpTempMsg)
  829. {
  830. CoTaskMemFree (lpMsg);
  831. return HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
  832. }
  833. lpMsg = lpTempMsg;
  834. hr = StringCchCat (lpMsg, ulSize, TEXT("\r\n"));
  835. if (SUCCEEDED(hr))
  836. {
  837. hr = StringCchCat (lpMsg, ulSize, lpTemp->lpText);
  838. }
  839. if (FAILED(hr))
  840. {
  841. CoTaskMemFree (lpMsg);
  842. return hr;
  843. }
  844. }
  845. else
  846. {
  847. ulSize = lstrlen(lpTemp->lpText) + 1;
  848. lpMsg = (LPOLESTR) CoTaskMemAlloc (ulSize * sizeof(TCHAR));
  849. if (!lpMsg)
  850. {
  851. return HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
  852. }
  853. hr = StringCchCopy (lpMsg, ulSize, lpTemp->lpText);
  854. if (FAILED(hr))
  855. {
  856. CoTaskMemFree(lpMsg);
  857. return hr;
  858. }
  859. }
  860. }
  861. }
  862. }
  863. }
  864. lpTemp = lpTemp->pNext;
  865. }
  866. if (!lpMsg)
  867. {
  868. return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
  869. }
  870. *ppszText = lpMsg;
  871. return S_OK;
  872. }
  873. BOOL CEvents::IsEntryInEventSourceList (LPEVENTLOGENTRY lpEntry, LPSOURCEENTRY lpEventSources)
  874. {
  875. LPSOURCEENTRY lpTemp;
  876. if (!lpEventSources)
  877. {
  878. return FALSE;
  879. }
  880. lpTemp = lpEventSources;
  881. while (lpTemp)
  882. {
  883. if (!lstrcmpi(lpTemp->lpEventLogName, lpEntry->lpEventLogName))
  884. {
  885. if (!lstrcmpi(lpTemp->lpEventSourceName, lpEntry->lpEventSourceName))
  886. {
  887. return TRUE;
  888. }
  889. }
  890. lpTemp = lpTemp->pNext;
  891. }
  892. return FALSE;
  893. }
  894. BOOL CEvents::IsEntryInExceptionList (LPEVENTLOGENTRY lpEntry)
  895. {
  896. LPEVENTLOGENTRY lpTemp;
  897. DWORD i;
  898. for (i = 0; i < dwExceptionEventEntriesSize; i++) {
  899. lpTemp = ExceptionEventEntries+i;
  900. if (!lstrcmpi(lpTemp->lpEventLogName, lpEntry->lpEventLogName))
  901. {
  902. if (!lstrcmpi(lpTemp->lpEventSourceName, lpEntry->lpEventSourceName))
  903. {
  904. if (LOWORD(lpTemp->dwEventID) == LOWORD(lpEntry->dwEventID)) {
  905. DebugMsg((DM_VERBOSE, TEXT("Skipping event id")));
  906. DebugMsg((DM_VERBOSE, TEXT("Event Log: %s"), lpEntry->lpEventLogName));
  907. DebugMsg((DM_VERBOSE, TEXT("Event Source: %s"), lpEntry->lpEventSourceName));
  908. DebugMsg((DM_VERBOSE, TEXT("Event ID: %d"), LOWORD(lpEntry->dwEventID)));
  909. return TRUE;
  910. }
  911. }
  912. }
  913. }
  914. return FALSE;
  915. }
  916. STDMETHODIMP CEvents::GetCSEEntries(SYSTEMTIME * pBeginTime, SYSTEMTIME * pEndTime,
  917. LPSOURCEENTRY lpEventSources, LPOLESTR *ppszText,
  918. BOOL bGPCore)
  919. {
  920. LPEVENTLOGENTRY lpTemp;
  921. FILETIME ftBeginTime, ftEndTime;
  922. LPOLESTR lpMsg = NULL, lpTempMsg;
  923. ULONG ulSize;
  924. HRESULT hr;
  925. SystemTimeToFileTime (pBeginTime, &ftBeginTime);
  926. SystemTimeToFileTime (pEndTime, &ftEndTime);
  927. //
  928. // Loop through the entries looking for matches
  929. //
  930. lpTemp = m_pEventEntries;
  931. while (lpTemp)
  932. {
  933. if ((CompareFileTime (&lpTemp->ftEventTime, &ftBeginTime) >= 0) &&
  934. (CompareFileTime (&lpTemp->ftEventTime, &ftEndTime) <= 0))
  935. {
  936. if (IsEntryInEventSourceList (lpTemp, lpEventSources))
  937. {
  938. if ((bGPCore) || (!IsEntryInExceptionList(lpTemp))) {
  939. if (lpMsg)
  940. {
  941. ulSize = lstrlen(lpMsg);
  942. ulSize += lstrlen(lpTemp->lpText) + 3;
  943. lpTempMsg = (LPOLESTR) CoTaskMemRealloc (lpMsg, ulSize * sizeof(TCHAR));
  944. if (!lpTempMsg)
  945. {
  946. CoTaskMemFree (lpMsg);
  947. return HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
  948. }
  949. lpMsg = lpTempMsg;
  950. hr = StringCchCat (lpMsg, ulSize, TEXT("\r\n"));
  951. if (SUCCEEDED(hr))
  952. {
  953. hr = StringCchCat (lpMsg, ulSize, lpTemp->lpText);
  954. }
  955. if (FAILED(hr))
  956. {
  957. CoTaskMemFree(lpMsg);
  958. return hr;
  959. }
  960. }
  961. else
  962. {
  963. ulSize = lstrlen(lpTemp->lpText) + 1;
  964. lpMsg = (LPOLESTR) CoTaskMemAlloc (ulSize * sizeof(TCHAR));
  965. if (!lpMsg)
  966. {
  967. return HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
  968. }
  969. hr = StringCchCopy (lpMsg, ulSize, lpTemp->lpText);
  970. if (FAILED(hr))
  971. {
  972. CoTaskMemFree (lpMsg);
  973. return hr;
  974. }
  975. }
  976. }
  977. }
  978. }
  979. lpTemp = lpTemp->pNext;
  980. }
  981. if (!lpMsg)
  982. {
  983. return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
  984. }
  985. *ppszText = lpMsg;
  986. return S_OK;
  987. }
  988. STDMETHODIMP CEvents::DumpDebugInfo (void)
  989. {
  990. LPEVENTLOGENTRY lpTemp;
  991. FILETIME ftLocal;
  992. SYSTEMTIME systime;
  993. TCHAR szDateTime[100];
  994. lpTemp = m_pEventEntries;
  995. if (lpTemp)
  996. {
  997. DebugMsg((DM_VERBOSE, TEXT(" ")));
  998. DebugMsg((DM_VERBOSE, TEXT("Event log entries:")));
  999. }
  1000. while (lpTemp)
  1001. {
  1002. ConvertTimeToDisplayTime (NULL, &lpTemp->ftEventTime, szDateTime, ARRAYSIZE(szDateTime));
  1003. DebugMsg((DM_VERBOSE, TEXT(" ")));
  1004. DebugMsg((DM_VERBOSE, TEXT("Event Time: %s"), szDateTime));
  1005. DebugMsg((DM_VERBOSE, TEXT("Event Log: %s"), lpTemp->lpEventLogName));
  1006. DebugMsg((DM_VERBOSE, TEXT("Event Source: %s"), lpTemp->lpEventSourceName));
  1007. DebugMsg((DM_VERBOSE, TEXT("Event ID: %d"), LOWORD(lpTemp->dwEventID)));
  1008. DebugMsg((DM_VERBOSE, TEXT("Message: %s"), lpTemp->lpText));
  1009. lpTemp = lpTemp->pNext;
  1010. }
  1011. return S_OK;
  1012. }
  1013. LPTSTR CEvents::ConvertTimeToDisplayTime (SYSTEMTIME *pSysTime,
  1014. FILETIME *pFileTime,
  1015. LPTSTR szBuffer,
  1016. ULONG ulNoChars)
  1017. {
  1018. FILETIME ftTime, ftLocal;
  1019. SYSTEMTIME systime;
  1020. if (pSysTime)
  1021. {
  1022. SystemTimeToFileTime (pSysTime, &ftTime);
  1023. }
  1024. else if ( pFileTime )
  1025. {
  1026. CopyMemory (&ftTime, pFileTime, sizeof(FILETIME));
  1027. }
  1028. else
  1029. {
  1030. //
  1031. // No time was specified, so just return an empty string
  1032. // in the buffer
  1033. //
  1034. if ( ulNoChars > 0 )
  1035. {
  1036. *szBuffer = L'\0';
  1037. }
  1038. return szBuffer;
  1039. }
  1040. FileTimeToLocalFileTime (&ftTime, &ftLocal);
  1041. FileTimeToSystemTime (&ftLocal, &systime);
  1042. (void) StringCchPrintf (szBuffer,
  1043. ulNoChars,
  1044. TEXT("%d/%d/%d %02d:%02d:%02d:%03d"),
  1045. systime.wMonth,
  1046. systime.wDay,
  1047. systime.wYear,
  1048. systime.wHour,
  1049. systime.wMinute,
  1050. systime.wSecond,
  1051. systime.wMilliseconds);
  1052. return szBuffer;
  1053. }
  1054. STDMETHODIMP CEvents::AddSourceEntry (LPTSTR lpEventLogName,
  1055. LPTSTR lpEventSourceName,
  1056. LPSOURCEENTRY *lpList)
  1057. {
  1058. LPSOURCEENTRY lpItem;
  1059. DWORD dwSize;
  1060. ULONG ulNoChars;
  1061. HRESULT hr;
  1062. //
  1063. // Calculate the size of the new item
  1064. //
  1065. dwSize = sizeof (SOURCEENTRY);
  1066. dwSize += ((lstrlen(lpEventLogName) + 1) * sizeof(TCHAR));
  1067. dwSize += ((lstrlen(lpEventSourceName) + 1) * sizeof(TCHAR));
  1068. //
  1069. // Allocate space for it
  1070. //
  1071. lpItem = (LPSOURCEENTRY) LocalAlloc (LPTR, dwSize);
  1072. if (!lpItem) {
  1073. DebugMsg((DM_WARNING, TEXT("CEvents::AddSourceEntry: Failed to allocate memory with %d"),
  1074. GetLastError()));
  1075. return E_FAIL;
  1076. }
  1077. //
  1078. // Fill in item
  1079. //
  1080. ulNoChars = (dwSize - sizeof(SOURCEENTRY))/sizeof(WCHAR);
  1081. lpItem->lpEventLogName = (LPTSTR)(((LPBYTE)lpItem) + sizeof(SOURCEENTRY));
  1082. hr = StringCchCopy (lpItem->lpEventLogName, ulNoChars, lpEventLogName);
  1083. if (SUCCEEDED(hr))
  1084. {
  1085. ulNoChars = ulNoChars - (lstrlen (lpItem->lpEventLogName) + 1);
  1086. lpItem->lpEventSourceName = lpItem->lpEventLogName + lstrlen (lpItem->lpEventLogName) + 1;
  1087. hr = StringCchCopy (lpItem->lpEventSourceName, ulNoChars, lpEventSourceName);
  1088. }
  1089. if (FAILED(hr))
  1090. {
  1091. DebugMsg((DM_WARNING, TEXT("CEvents::AddSourceEntry: Failed to copy event log name with %d"), hr));
  1092. LocalFree(lpItem);
  1093. return hr;
  1094. }
  1095. //
  1096. // Add it to the list
  1097. //
  1098. if (*lpList)
  1099. {
  1100. lpItem->pNext = *lpList;
  1101. }
  1102. *lpList = lpItem;
  1103. return S_OK;
  1104. }
  1105. VOID CEvents::FreeSourceData(LPSOURCEENTRY lpList)
  1106. {
  1107. LPSOURCEENTRY lpTemp;
  1108. if (lpList)
  1109. {
  1110. do {
  1111. lpTemp = lpList->pNext;
  1112. LocalFree (lpList);
  1113. lpList = lpTemp;
  1114. } while (lpTemp);
  1115. }
  1116. }
  1117. STDMETHODIMP CEvents::SaveEntriesToStream (IStream *pStm)
  1118. {
  1119. HRESULT hr;
  1120. DWORD dwCount = 0;
  1121. LPEVENTLOGENTRY lpTemp;
  1122. ULONG nBytesWritten;
  1123. //
  1124. // First count how many entries are in the link list
  1125. //
  1126. lpTemp = m_pEventEntries;
  1127. while (lpTemp)
  1128. {
  1129. dwCount++;
  1130. lpTemp = lpTemp->pNext;
  1131. }
  1132. //
  1133. // Save the count to the stream
  1134. //
  1135. hr = pStm->Write(&dwCount, sizeof(dwCount), &nBytesWritten);
  1136. if ((hr != S_OK) || (nBytesWritten != sizeof(dwCount)))
  1137. {
  1138. DebugMsg((DM_WARNING, TEXT("CEvents::SaveEntriesToStream: Failed to write entry count with %d."), hr));
  1139. hr = E_FAIL;
  1140. goto Exit;
  1141. }
  1142. //
  1143. // Now loop through each item saving each field in the node
  1144. //
  1145. lpTemp = m_pEventEntries;
  1146. while (lpTemp)
  1147. {
  1148. //
  1149. // Save the event id
  1150. //
  1151. hr = pStm->Write(&lpTemp->dwEventID, sizeof(DWORD), &nBytesWritten);
  1152. if ((hr != S_OK) || (nBytesWritten != sizeof(DWORD)))
  1153. {
  1154. DebugMsg((DM_WARNING, TEXT("CEvents::SaveEntriesToStream: Failed to write event id with %d."), hr));
  1155. hr = E_FAIL;
  1156. goto Exit;
  1157. }
  1158. //
  1159. // Save the event time
  1160. //
  1161. hr = pStm->Write(&lpTemp->ftEventTime, sizeof(FILETIME), &nBytesWritten);
  1162. if ((hr != S_OK) || (nBytesWritten != sizeof(FILETIME)))
  1163. {
  1164. DebugMsg((DM_WARNING, TEXT("CEvents::SaveEntriesToStream: Failed to write file time with %d."), hr));
  1165. hr = E_FAIL;
  1166. goto Exit;
  1167. }
  1168. //
  1169. // Save the event log name
  1170. //
  1171. hr = SaveString (pStm, lpTemp->lpEventLogName);
  1172. if (hr != S_OK)
  1173. {
  1174. DebugMsg((DM_WARNING, TEXT("CEvents::SaveEntriesToStream: Failed to save event log name with %d."), hr));
  1175. goto Exit;
  1176. }
  1177. //
  1178. // Save the event source name
  1179. //
  1180. hr = SaveString (pStm, lpTemp->lpEventSourceName);
  1181. if (hr != S_OK)
  1182. {
  1183. DebugMsg((DM_WARNING, TEXT("CEvents::SaveEntriesToStream: Failed to save event source name with %d."), hr));
  1184. goto Exit;
  1185. }
  1186. //
  1187. // Save the event text
  1188. //
  1189. hr = SaveString (pStm, lpTemp->lpText);
  1190. if (hr != S_OK)
  1191. {
  1192. DebugMsg((DM_WARNING, TEXT("CEvents::SaveEntriesToStream: Failed to save event text with %d."), hr));
  1193. goto Exit;
  1194. }
  1195. lpTemp = lpTemp->pNext;
  1196. }
  1197. Exit:
  1198. return hr;
  1199. }
  1200. STDMETHODIMP CEvents::LoadEntriesFromStream (IStream *pStm)
  1201. {
  1202. HRESULT hr;
  1203. DWORD dwCount = 0, dwIndex, dwEventID;
  1204. LPEVENTLOGENTRY lpTemp;
  1205. ULONG nBytesRead;
  1206. FILETIME ftEventTime;
  1207. LPTSTR lpEventLogName = NULL;
  1208. LPTSTR lpEventSourceName = NULL;
  1209. LPTSTR lpText = NULL;
  1210. //
  1211. // Read in the entry count
  1212. //
  1213. hr = pStm->Read(&dwCount, sizeof(dwCount), &nBytesRead);
  1214. if ((hr != S_OK) || (nBytesRead != sizeof(dwCount)))
  1215. {
  1216. DebugMsg((DM_WARNING, TEXT("CEvents::LoadEntriesFromStream: Failed to read event count with 0x%x."), hr));
  1217. hr = E_FAIL;
  1218. goto Exit;
  1219. }
  1220. //
  1221. // Loop through the items
  1222. //
  1223. for (dwIndex = 0; dwIndex < dwCount; dwIndex++)
  1224. {
  1225. //
  1226. // Read in the event id
  1227. //
  1228. hr = pStm->Read(&dwEventID, sizeof(dwEventID), &nBytesRead);
  1229. if ((hr != S_OK) || (nBytesRead != sizeof(dwEventID)))
  1230. {
  1231. DebugMsg((DM_WARNING, TEXT("CEvents::LoadEntriesFromStream: Failed to read event id with 0x%x."), hr));
  1232. hr = E_FAIL;
  1233. goto Exit;
  1234. }
  1235. //
  1236. // Read in the event time
  1237. //
  1238. hr = pStm->Read(&ftEventTime, sizeof(FILETIME), &nBytesRead);
  1239. if ((hr != S_OK) || (nBytesRead != sizeof(FILETIME)))
  1240. {
  1241. DebugMsg((DM_WARNING, TEXT("CEvents::LoadEntriesFromStream: Failed to read event time with 0x%x."), hr));
  1242. hr = E_FAIL;
  1243. goto Exit;
  1244. }
  1245. //
  1246. // Read the event log name
  1247. //
  1248. hr = ReadString (pStm, &lpEventLogName);
  1249. if (hr != S_OK)
  1250. {
  1251. DebugMsg((DM_WARNING, TEXT("CEvents::LoadEntriesFromStream: Failed to read the event log name with 0x%x."), hr));
  1252. goto Exit;
  1253. }
  1254. //
  1255. // Read the event source name
  1256. //
  1257. hr = ReadString (pStm, &lpEventSourceName);
  1258. if (hr != S_OK)
  1259. {
  1260. DebugMsg((DM_WARNING, TEXT("CEvents::LoadEntriesFromStream: Failed to read the event source name with 0x%x."), hr));
  1261. goto Exit;
  1262. }
  1263. //
  1264. // Read the event text
  1265. //
  1266. hr = ReadString (pStm, &lpText);
  1267. if (hr != S_OK)
  1268. {
  1269. DebugMsg((DM_WARNING, TEXT("CEvents::LoadEntriesFromStream: Failed to read the event text with 0x%x."), hr));
  1270. goto Exit;
  1271. }
  1272. //
  1273. // Add this entry to the link list
  1274. //
  1275. if (!AddEntry (lpEventLogName, lpEventSourceName, lpText, dwEventID, &ftEventTime))
  1276. {
  1277. DebugMsg((DM_WARNING, TEXT("CEvents::LoadEntriesFromStream: Failed to add the entry.")));
  1278. hr = E_FAIL;
  1279. goto Exit;
  1280. }
  1281. //
  1282. // Clean up for next item
  1283. //
  1284. delete [] lpEventLogName;
  1285. lpEventLogName = NULL;
  1286. delete [] lpEventSourceName;
  1287. lpEventSourceName = NULL;
  1288. delete [] lpText;
  1289. lpText = NULL;
  1290. }
  1291. Exit:
  1292. if (lpEventLogName)
  1293. {
  1294. delete [] lpEventLogName;
  1295. }
  1296. if (lpEventSourceName)
  1297. {
  1298. delete [] lpEventSourceName;
  1299. }
  1300. if (lpText)
  1301. {
  1302. delete [] lpText;
  1303. }
  1304. return hr;
  1305. }