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.

235 lines
5.3 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. Eventlog.cpp
  5. Abstract:
  6. Implementation of the event log API
  7. wrapper class.
  8. Notes:
  9. Unicode only.
  10. History:
  11. 03/02/2001 rparsons Created
  12. --*/
  13. #include "eventlog.h"
  14. /*++
  15. Routine Description:
  16. Adds the specified event source to the registry
  17. Arguments:
  18. lpwSourceFile - The path & name of the file that
  19. contains the event log strings
  20. lpwSourceName - The name of the event log source
  21. dwLogType - The log that the source should be
  22. added to
  23. Return Value:
  24. TRUE if the source was added successfully, FALSE otherwise
  25. --*/
  26. BOOL
  27. CEventLog::CreateEventSource(
  28. IN LPCWSTR lpwSourceFile,
  29. IN LPCWSTR lpwSourceName,
  30. IN DWORD dwLogType
  31. )
  32. {
  33. HKEY hLogKey = NULL;
  34. DWORD dwTypes = 7L;
  35. DWORD cCount = 0L;
  36. BOOL fResult = FALSE;
  37. WCHAR wszRegPath[MAX_PATH] = L"";
  38. __try {
  39. //
  40. // Determine the log type - application, system,
  41. // or security - and build the path in the registry
  42. //
  43. switch (dwLogType) {
  44. case dwApplication:
  45. wsprintf(wszRegPath, L"%s\\%s", APP_LOG_REG_PATH, lpwSourceName);
  46. break;
  47. case dwSystem:
  48. wsprintf(wszRegPath, L"%s\\%s", SYS_LOG_REG_PATH, lpwSourceName);
  49. break;
  50. case dwSecurity:
  51. wsprintf(wszRegPath, L"%s\\%s", SEC_LOG_REG_PATH, lpwSourceName);
  52. break;
  53. }
  54. //
  55. // Open the source key - if it doesn't exist,
  56. // it will be created
  57. //
  58. if (RegCreateKeyEx(HKEY_LOCAL_MACHINE,
  59. wszRegPath,
  60. 0,
  61. NULL,
  62. REG_OPTION_NON_VOLATILE,
  63. KEY_SET_VALUE,
  64. NULL,
  65. &hLogKey,
  66. 0) != ERROR_SUCCESS) __leave;
  67. //
  68. // Write the path to our message file
  69. //
  70. if (RegSetValueEx(hLogKey,
  71. L"EventMessageFile",
  72. 0L,
  73. REG_SZ,
  74. (LPBYTE) lpwSourceFile,
  75. (wcslen(lpwSourceFile)+1)
  76. *sizeof(WCHAR)) != ERROR_SUCCESS) __leave;
  77. //
  78. // Write the number of event types supported
  79. //
  80. if (RegSetValueEx(hLogKey,
  81. L"TypesSupported",
  82. 0L,
  83. REG_DWORD,
  84. (LPBYTE) &dwTypes,
  85. sizeof(DWORD)) != ERROR_SUCCESS) __leave;
  86. //
  87. // Write the number of event categories supported
  88. //
  89. if (RegSetValueEx(hLogKey,
  90. L"CategoryCount",
  91. 0L,
  92. REG_DWORD,
  93. (LPBYTE) &cCount,
  94. sizeof(DWORD)) != ERROR_SUCCESS) __leave;
  95. fResult = TRUE;
  96. } // try
  97. __finally {
  98. if (hLogKey) {
  99. RegCloseKey(hLogKey);
  100. }
  101. } // finally
  102. return (fResult);
  103. }
  104. /*++
  105. Routine Description:
  106. Logs an event to the event log
  107. Arguments:
  108. lpwSourceName - Name of the source in the registry
  109. lpwUNCServerName - UNC server name or NULL for local
  110. wType - Type of event to report
  111. dwEventID - Event identifier
  112. wNumStrings - Number of insertion strings contained
  113. in lpwStrings array
  114. *lpwStrings - Array of insertion strings. Can be NULL
  115. if no strings are being used
  116. Return Value:
  117. None
  118. --*/
  119. BOOL
  120. CEventLog::LogEvent(
  121. IN LPCWSTR lpwSourceName,
  122. IN LPCWSTR lpwUNCServerName,
  123. IN WORD wType,
  124. IN DWORD dwEventID,
  125. IN WORD wNumStrings,
  126. IN LPCWSTR *lpwStrings OPTIONAL
  127. )
  128. {
  129. HANDLE hES = NULL;
  130. LPVOID lpMsgBuf = NULL;
  131. BOOL fResult = FALSE;
  132. __try {
  133. //
  134. // Obtain a handle to our event source
  135. //
  136. hES = RegisterEventSource(lpwUNCServerName, lpwSourceName);
  137. if (NULL == hES) {
  138. __leave;
  139. }
  140. if (wNumStrings) {
  141. //
  142. // Report the event with insertion strings
  143. //
  144. fResult = ReportEvent(hES,
  145. wType,
  146. 0,
  147. dwEventID,
  148. NULL,
  149. wNumStrings,
  150. 0,
  151. lpwStrings,
  152. 0);
  153. } else {
  154. //
  155. // Report the event with no strings
  156. //
  157. fResult = ReportEvent(hES,
  158. wType,
  159. 0,
  160. dwEventID,
  161. NULL,
  162. 0,
  163. 0L,
  164. NULL,
  165. 0);
  166. }
  167. } // try
  168. __finally {
  169. if (hES) {
  170. DeregisterEventSource(hES);
  171. }
  172. } // finally
  173. return (fResult);
  174. }