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.

386 lines
9.5 KiB

  1. //*************************************************************
  2. //
  3. // Events.cpp - Routines to handle the event log
  4. //
  5. // Microsoft Confidential
  6. // Copyright (c) Microsoft Corporation 1995
  7. // All rights reserved
  8. //
  9. //*************************************************************
  10. #include "stdafx.h"
  11. #include "rsopdbg.h"
  12. #include "events.h"
  13. HANDLE hEventLog = NULL;
  14. TCHAR EventSourceName[] = TEXT("GPDAS");
  15. TCHAR MessageResourceFile[] = TEXT("%systemroot%\\system32\\rsopprov.exe");
  16. //*************************************************************
  17. //
  18. // InitializeEvents()
  19. //
  20. // Purpose: Opens the event log
  21. //
  22. // Parameters: void
  23. //
  24. // Return: TRUE if successful
  25. // FALSE if an error occurs
  26. //
  27. // Comments:
  28. //
  29. // History: Date Author Comment
  30. // 7/17/95 ericflo Created
  31. //
  32. //*************************************************************
  33. BOOL InitializeEvents (void)
  34. {
  35. //
  36. // Open the event source
  37. //
  38. hEventLog = RegisterEventSource(NULL, EventSourceName);
  39. if (hEventLog) {
  40. return TRUE;
  41. }
  42. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("InitializeEvents: Could not open event log. Error = %d"), GetLastError());
  43. return FALSE;
  44. }
  45. //*************************************************************
  46. //
  47. // Implementation of CEvents
  48. //
  49. //*************************************************************
  50. //*************************************************************
  51. // CEvents::CEvents
  52. // Purpose: Constructor
  53. //
  54. // Parameters:
  55. // bError - Error or informational
  56. // dwId - Id of the eventlog msg
  57. //
  58. //
  59. // allocates a default sized array for the messages
  60. //*************************************************************
  61. #define DEF_ARG_SIZE 10
  62. CEvents::CEvents(BOOL bError, DWORD dwId ) :
  63. m_cStrings(0), m_cAllocated(0), m_bInitialised(FALSE),
  64. m_bError(bError), m_dwId(dwId), m_bFailed(TRUE)
  65. {
  66. //
  67. // Allocate a default size for the message
  68. //
  69. m_xlpStrings = (LPTSTR *)LocalAlloc(LPTR, sizeof(LPTSTR)*DEF_ARG_SIZE);
  70. m_cAllocated = DEF_ARG_SIZE;
  71. if (!m_xlpStrings) {
  72. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CEvent::CEvent Cannot log event, failed to allocate memory, error %d"), GetLastError());
  73. return;
  74. }
  75. //
  76. // Initialise eventlog if it is not already initialised
  77. //
  78. if (!hEventLog) {
  79. if (!InitializeEvents()) {
  80. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CEvent::CEvent Cannot log event, no handle"));
  81. return;
  82. }
  83. }
  84. m_bInitialised = TRUE;
  85. m_bFailed = FALSE;
  86. }
  87. //*************************************************************
  88. // CEvents::~CEvents()
  89. //
  90. // Purpose: Destructor
  91. //
  92. // Parameters: void
  93. //
  94. // frees the memory
  95. //*************************************************************
  96. CEvents::~CEvents()
  97. {
  98. for (int i = 0; i < m_cStrings; i++)
  99. if (m_xlpStrings[i])
  100. LocalFree(m_xlpStrings[i]);
  101. }
  102. //*************************************************************
  103. //
  104. // CEvents::ReallocArgStrings
  105. //
  106. // Purpose: Reallocates the buffer for storing arguments in case
  107. // the buffer runs out
  108. //
  109. // Parameters: void
  110. //
  111. // reallocates
  112. //*************************************************************
  113. BOOL CEvents::ReallocArgStrings()
  114. {
  115. XPtrLF<LPTSTR> aStringsNew;
  116. //
  117. // first allocate a larger buffer
  118. //
  119. aStringsNew = (LPTSTR *)LocalAlloc(LPTR, sizeof(LPTSTR)*(m_cAllocated+DEF_ARG_SIZE));
  120. if (!aStringsNew) {
  121. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CEvent::CEvent Couldn't allocate memory"));
  122. m_bFailed = TRUE;
  123. return FALSE;
  124. }
  125. //
  126. // copy the arguments
  127. //
  128. for (int i = 0; i < (m_cAllocated); i++) {
  129. aStringsNew[i] = m_xlpStrings[i];
  130. }
  131. m_xlpStrings = aStringsNew.Acquire();
  132. m_cAllocated+= DEF_ARG_SIZE;
  133. return TRUE;
  134. }
  135. //*************************************************************
  136. //
  137. // CEvents::AddArg
  138. //
  139. // Purpose: Add arguments appropriately formatted
  140. //
  141. // Parameters:
  142. //
  143. //*************************************************************
  144. BOOL CEvents::AddArg(LPTSTR szArg)
  145. {
  146. if ((!m_bInitialised) || (m_bFailed)) {
  147. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CEvent::AddArg: Cannot log event, not initialised or failed before"));
  148. return FALSE;
  149. }
  150. if (m_cStrings == m_cAllocated) {
  151. if (!ReallocArgStrings())
  152. return FALSE;
  153. }
  154. DWORD dwLength = lstrlen(szArg) + 1;
  155. m_xlpStrings[m_cStrings] = (LPTSTR)LocalAlloc(LPTR, sizeof(TCHAR) * (dwLength));
  156. if (!m_xlpStrings[m_cStrings]) {
  157. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CEvent::AddArg Cannot allocate memory, error = %d"), GetLastError());
  158. m_bFailed = TRUE;
  159. return FALSE;
  160. }
  161. HRESULT hr = StringCchCopy(m_xlpStrings[m_cStrings], dwLength, szArg);
  162. if(FAILED(hr)){
  163. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CEvent::StringCchCopy failed with error = %d"), hr);
  164. m_bFailed = TRUE;
  165. return FALSE;
  166. }
  167. m_cStrings++;
  168. return TRUE;
  169. }
  170. //*************************************************************
  171. //
  172. // CEvents::AddArg
  173. //
  174. // Purpose: Add arguments appropriately formatted
  175. //
  176. // Parameters:
  177. //
  178. //*************************************************************
  179. BOOL CEvents::AddArg(DWORD dwArg)
  180. {
  181. if ((!m_bInitialised) || (m_bFailed)) {
  182. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CEvent::AddArg(dw): Cannot log event, not initialised or failed before"));
  183. return FALSE;
  184. }
  185. if (m_cStrings == m_cAllocated) {
  186. if (!ReallocArgStrings())
  187. return FALSE;
  188. }
  189. // 2^32 < 10^10
  190. DWORD dwLength = 20;
  191. m_xlpStrings[m_cStrings] = (LPTSTR)LocalAlloc(LPTR, sizeof(TCHAR) * dwLength);
  192. if (!m_xlpStrings[m_cStrings]) {
  193. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CEvent::AddArg(dw) Cannot allocate memory, error = %d"), GetLastError());
  194. m_bFailed = TRUE;
  195. return FALSE;
  196. }
  197. HRESULT hr = StringCchPrintf(m_xlpStrings[m_cStrings], dwLength, TEXT("%d"), dwArg);
  198. if(FAILED(hr)){
  199. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CEvent::StringCchPrintf failed with error = %d"), hr);
  200. m_bFailed = TRUE;
  201. return FALSE;
  202. }
  203. m_cStrings++;
  204. return TRUE;
  205. }
  206. //*************************************************************
  207. //
  208. // CEvents::AddArg
  209. //
  210. // Purpose: Add arguments appropriately formatted
  211. //
  212. // Parameters:
  213. //
  214. //*************************************************************
  215. BOOL CEvents::AddArgHex(DWORD dwArg)
  216. {
  217. if ((!m_bInitialised) || (m_bFailed)) {
  218. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CEvent::AddArgHex: Cannot log event, not initialised or failed before"));
  219. return FALSE;
  220. }
  221. if (m_cStrings == m_cAllocated) {
  222. if (!ReallocArgStrings())
  223. return FALSE;
  224. }
  225. DWORD dwLength = 20;
  226. m_xlpStrings[m_cStrings] = (LPTSTR)LocalAlloc(LPTR, sizeof(TCHAR) * dwLength);
  227. if (!m_xlpStrings[m_cStrings]) {
  228. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CEvent::AddArgHex Cannot allocate memory, error = %d"), GetLastError());
  229. m_bFailed = TRUE;
  230. return FALSE;
  231. }
  232. HRESULT hr = StringCchPrintf(m_xlpStrings[m_cStrings], dwLength, TEXT("%#x"), dwArg);
  233. if(FAILED(hr)){
  234. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CEvent::StringCchPrintf failed with error = %d"), hr);
  235. m_bFailed = TRUE;
  236. return FALSE;
  237. }
  238. m_cStrings++;
  239. return TRUE;
  240. }
  241. //*************************************************************
  242. //
  243. // CEvents::Report
  244. //
  245. // Purpose: Actually collectes all the arguments and reports it to
  246. // the eventlog
  247. //
  248. // Parameters: void
  249. //
  250. //*************************************************************
  251. BOOL CEvents::Report()
  252. {
  253. PSID pSid = NULL; // no sid being reportewd currently
  254. WORD wType=0;
  255. BOOL bResult = TRUE;
  256. if ((!m_bInitialised) || (m_bFailed)) {
  257. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CEvents::Report: Cannot log event, not initialised or failed before"));
  258. return FALSE;
  259. }
  260. if ( m_bError ) {
  261. wType = EVENTLOG_ERROR_TYPE;
  262. } else {
  263. wType = EVENTLOG_INFORMATION_TYPE;
  264. }
  265. if (!ReportEvent(hEventLog, wType, 0, m_dwId, pSid, m_cStrings, 0, (LPCTSTR *)((LPTSTR *)m_xlpStrings), NULL)) {
  266. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CEvents::Report: ReportEvent failed. Error = %d"), GetLastError());
  267. bResult = FALSE;
  268. }
  269. return bResult;
  270. }
  271. //*************************************************************
  272. //
  273. // ShutdownEvents()
  274. //
  275. // Purpose: Stops the event log
  276. //
  277. // Parameters: void
  278. //
  279. // Return: TRUE if successful
  280. // FALSE if an error occurs
  281. //
  282. // Comments:
  283. //
  284. // History: Date Author Comment
  285. // 7/17/95 ericflo Created
  286. //
  287. //*************************************************************
  288. BOOL ShutdownEvents (void)
  289. {
  290. BOOL bRetVal = TRUE;
  291. if (hEventLog) {
  292. bRetVal = DeregisterEventSource(hEventLog);
  293. hEventLog = NULL;
  294. }
  295. return bRetVal;
  296. }