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.

240 lines
6.0 KiB

  1. // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2. //
  3. // EVNTLOG.CPP
  4. //
  5. // Event logging
  6. //
  7. // Copyright 1986-1997 Microsoft Corporation, All Rights Reserved
  8. //
  9. #include "_evntlog.h" // Precompiled header
  10. // _EVNTLOG headers
  11. //
  12. #include <eventlog.h> // Event logging interface
  13. #include <ex\reg.h> // Registry access
  14. static DWORD DwCreateAppLogSubkey( LPCWSTR lpwszDllPath, DWORD dwCategories = 0 );
  15. static DWORD DwDeleteAppLogSubkey();
  16. static const WCHAR gsc_wszAppLogRegKey[] =
  17. L"SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\";
  18. // ------------------------------------------------------------------------
  19. //
  20. // DWCreateAppLogSubkey()
  21. //
  22. DWORD
  23. DwCreateAppLogSubkey( LPCWSTR lpwszDllPath, DWORD dwCategories )
  24. {
  25. CRegKey regkey;
  26. DWORD dwResult = ERROR_SUCCESS;
  27. //
  28. // Create a key for this source under the application log
  29. //
  30. {
  31. WCHAR lpwszKey[256];
  32. swprintf( lpwszKey, L"%ls%ls", gsc_wszAppLogRegKey, gc_wszSignature );
  33. dwResult = regkey.DwCreate( HKEY_LOCAL_MACHINE, lpwszKey );
  34. if ( ERROR_SUCCESS != dwResult )
  35. {
  36. DebugTrace( "DwCreateAppLogSubkey() - Error creating application log registry key (%d)\n", dwResult );
  37. goto ret;
  38. }
  39. }
  40. //
  41. // Set EventMessageFile to the full DLL path
  42. //
  43. dwResult = regkey.DwSetValue( L"EventMessageFile",
  44. REG_EXPAND_SZ,
  45. reinterpret_cast<const BYTE *>(lpwszDllPath),
  46. static_cast<DWORD>(sizeof(WCHAR) * (wcslen(lpwszDllPath) + 1)) );
  47. if ( ERROR_SUCCESS != dwResult )
  48. {
  49. DebugTrace( "DwCreateAppLogSubkey() - Error setting EventMessageFile value (%d)\n", dwResult );
  50. goto ret;
  51. }
  52. //
  53. // Set TypesSupported -- Error, warning and information only
  54. //
  55. {
  56. DWORD dwTypesSupported = EVENTLOG_ERROR_TYPE |
  57. EVENTLOG_WARNING_TYPE |
  58. EVENTLOG_INFORMATION_TYPE;
  59. dwResult = regkey.DwSetValue( L"TypesSupported",
  60. REG_DWORD,
  61. reinterpret_cast<LPBYTE>(&dwTypesSupported),
  62. sizeof(DWORD) );
  63. if ( ERROR_SUCCESS != dwResult )
  64. {
  65. DebugTrace( "DwCreateAppLogSubkey() - Error setting TypesSupported value (%d)\n", dwResult );
  66. goto ret;
  67. }
  68. }
  69. if (dwCategories)
  70. {
  71. //
  72. // Set CategoryMessageFile to the full DLL path
  73. //
  74. dwResult = regkey.DwSetValue( L"CategoryMessageFile",
  75. REG_EXPAND_SZ,
  76. reinterpret_cast<const BYTE *>(lpwszDllPath),
  77. static_cast<DWORD>(sizeof(WCHAR) * (wcslen(lpwszDllPath) + 1)) );
  78. if ( ERROR_SUCCESS != dwResult )
  79. {
  80. DebugTrace( "DwCreateAppLogSubkey() - Error setting CategoryMessageFile value (%d)\n", dwResult );
  81. goto ret;
  82. }
  83. //
  84. // Set CategoryCount (to the specified one)
  85. //
  86. {
  87. dwResult = regkey.DwSetValue( L"CategoryCount",
  88. REG_DWORD,
  89. reinterpret_cast<LPBYTE>(&dwCategories),
  90. sizeof(DWORD) );
  91. if ( ERROR_SUCCESS != dwResult )
  92. {
  93. DebugTrace( "DwCreateAppLogSubkey() - Error setting CategoryCount value (%d)\n", dwResult );
  94. goto ret;
  95. }
  96. }
  97. }
  98. ret:
  99. return dwResult;
  100. }
  101. // ------------------------------------------------------------------------
  102. //
  103. // DwDeleteAppLogSubkey()
  104. //
  105. DWORD
  106. DwDeleteAppLogSubkey()
  107. {
  108. WCHAR lpwszKey[256];
  109. swprintf( lpwszKey, L"%ls%ls", gsc_wszAppLogRegKey, gc_wszSignature );
  110. DWORD dwResult = RegDeleteKeyW( HKEY_LOCAL_MACHINE, lpwszKey );
  111. if ( dwResult != ERROR_SUCCESS )
  112. DebugTrace( "FDeleteAppLogSubkey() - RegDeleteKeyW() failed (%d)\n", dwResult );
  113. return dwResult;
  114. }
  115. // ========================================================================
  116. //
  117. // PUBLIC INTERFACE
  118. //
  119. // ------------------------------------------------------------------------
  120. //
  121. // LogEvent()
  122. //
  123. VOID
  124. LogEvent( DWORD dwEventID,
  125. WORD wEventType,
  126. WORD wcDataStrings,
  127. LPCSTR * plpszDataStrings,
  128. DWORD dwcbRawData,
  129. LPVOID lpvRawData,
  130. WORD wEventCategory )
  131. {
  132. HANDLE hEventLog = RegisterEventSourceW( NULL, gc_wszSignature );
  133. if ( NULL != hEventLog )
  134. {
  135. if ( !ReportEventA( hEventLog,
  136. wEventType,
  137. wEventCategory, // Category
  138. dwEventID,
  139. NULL, // Optional security ID
  140. wcDataStrings,
  141. dwcbRawData,
  142. plpszDataStrings,
  143. lpvRawData ) )
  144. {
  145. DebugTrace( "LogEvent() - ReportEventA() failed (%d)\n", GetLastError() );
  146. }
  147. if ( !DeregisterEventSource( hEventLog ) )
  148. {
  149. DebugTrace( "LogEvent() - DeregisterEventSource() failed (%d)\n", GetLastError() );
  150. }
  151. }
  152. else
  153. {
  154. DebugTrace( "LogEvent() - RegisterEventSource() failed (%d)\n", GetLastError() );
  155. }
  156. }
  157. // ------------------------------------------------------------------------
  158. //
  159. // LogEventW()
  160. //
  161. VOID
  162. LogEventW( DWORD dwEventID,
  163. WORD wEventType,
  164. WORD wcDataStrings,
  165. LPCWSTR * plpwszDataStrings,
  166. DWORD dwcbRawData,
  167. LPVOID lpvRawData,
  168. WORD wEventCategory )
  169. {
  170. HANDLE hEventLog = RegisterEventSourceW( NULL, gc_wszSignature );
  171. if ( NULL != hEventLog )
  172. {
  173. if ( !ReportEventW( hEventLog,
  174. wEventType,
  175. wEventCategory, // Category
  176. dwEventID,
  177. NULL, // Optional security ID
  178. wcDataStrings,
  179. dwcbRawData,
  180. plpwszDataStrings,
  181. lpvRawData ) )
  182. {
  183. DebugTrace( "LogEventW() - ReportEventW() failed (%d)\n", GetLastError() );
  184. }
  185. if ( !DeregisterEventSource( hEventLog ) )
  186. {
  187. DebugTrace( "LogEventW() - DeregisterEventSource() failed (%d)\n", GetLastError() );
  188. }
  189. }
  190. else
  191. {
  192. DebugTrace( "LogEventW() - RegisterEventSource() failed (%d)\n", GetLastError() );
  193. }
  194. }
  195. // ------------------------------------------------------------------------
  196. //
  197. // EventLogDllRegisterServer()
  198. //
  199. STDAPI EventLogDllRegisterServer( LPCWSTR lpwszDllPath, DWORD dwCategories )
  200. {
  201. return HRESULT_FROM_WIN32(DwCreateAppLogSubkey(lpwszDllPath, dwCategories));
  202. }
  203. // ------------------------------------------------------------------------
  204. //
  205. // EventLogDllUnregisterServer()
  206. //
  207. STDAPI EventLogDllUnregisterServer()
  208. {
  209. return HRESULT_FROM_WIN32(DwDeleteAppLogSubkey());
  210. }