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.

217 lines
5.1 KiB

  1. //
  2. // MODULE: RegWEventViewer.cpp
  3. //
  4. // PURPOSE: Fully implements class CRegisterWithEventViewer
  5. //
  6. // PROJECT: Generic Troubleshooter DLL for Microsoft AnswerPoint
  7. //
  8. // COMPANY: Saltmine Creative, Inc. (206)-284-7511 [email protected]
  9. //
  10. // AUTHOR: Roman Mach
  11. //
  12. // ORIGINAL DATE: 8-2-96
  13. //
  14. // NOTES:
  15. //
  16. // Version Date By Comments
  17. //--------------------------------------------------------------------
  18. // V3.0 9/16/98 JM pulled out of APGTSCFG.CPP
  19. //
  20. #pragma warning(disable:4786)
  21. #include "stdafx.h"
  22. #include "apgts.h"
  23. #include "apgtsinf.h"
  24. #include "event.h"
  25. #include "maxbuf.h"
  26. #include "RegWEventViewer.h"
  27. #include "baseexception.h"
  28. #include <vector>
  29. using namespace std;
  30. // ------------ CRegisterWithEventViewer -------------
  31. CRegisterWithEventViewer::CRegisterWithEventViewer(HMODULE hModule)
  32. {
  33. Register( hModule );
  34. }
  35. CRegisterWithEventViewer::~CRegisterWithEventViewer()
  36. {
  37. }
  38. //
  39. // This is called only by constructor
  40. // Note that this fn makes no use of class data
  41. //
  42. // Register ourselves w/ event viewer so it can call us to get error strings
  43. VOID CRegisterWithEventViewer::Register(HMODULE hModule)
  44. {
  45. HKEY hk;
  46. DWORD dwDisposition, dwType, dwValue, dwSize;
  47. TCHAR szSubkey[MAXBUF];
  48. DWORD dwErr;
  49. // 1. check if registry has valid event viewer info
  50. // 2. if not, create it as appropriate
  51. // check presence of event log info...
  52. _stprintf(szSubkey, _T("%s\\%s"), REG_EVT_PATH, REG_EVT_ITEM_STR);
  53. dwErr = ::RegCreateKeyEx( HKEY_LOCAL_MACHINE,
  54. szSubkey,
  55. 0,
  56. TS_REG_CLASS,
  57. REG_OPTION_NON_VOLATILE,
  58. KEY_READ | KEY_WRITE,
  59. NULL,
  60. &hk,
  61. &dwDisposition);
  62. if ( dwErr == ERROR_SUCCESS )
  63. {
  64. if (dwDisposition == REG_CREATED_NEW_KEY) {
  65. // create entire registry layout for events
  66. RegisterDllPath(hk, hModule);
  67. RegisterEventTypes(hk);
  68. }
  69. else {
  70. // (REG_OPENED_EXISTING_KEY is the only other possibility)
  71. // now make sure all registry elements present
  72. TCHAR szPath[MAXBUF];
  73. dwSize = sizeof (szPath) - 1;
  74. if (::RegQueryValueEx(hk,
  75. REG_EVT_MF,
  76. 0,
  77. &dwType,
  78. (LPBYTE) szPath,
  79. &dwSize) != ERROR_SUCCESS)
  80. {
  81. RegisterDllPath(hk, hModule);
  82. }
  83. dwSize = sizeof (DWORD);
  84. if (::RegQueryValueEx(hk,
  85. REG_EVT_TS,
  86. 0,
  87. &dwType,
  88. (LPBYTE) &dwValue,
  89. &dwSize) != ERROR_SUCCESS)
  90. {
  91. RegisterEventTypes(hk);
  92. }
  93. }
  94. ::RegCloseKey(hk);
  95. }
  96. else
  97. {
  98. TCHAR szMsgBuf[MAXBUF];
  99. ::FormatMessage(
  100. FORMAT_MESSAGE_FROM_SYSTEM,
  101. NULL,
  102. dwErr,
  103. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  104. szMsgBuf,
  105. MAXBUF,
  106. NULL
  107. );
  108. // Logging won't be pretty here, because we just failed to register with the Event
  109. // Viewer, but we'll take what we can get.
  110. CBuildSrcFileLinenoStr SrcLoc( __FILE__, __LINE__ );
  111. CEvent::ReportWFEvent( SrcLoc.GetSrcFileLineStr(),
  112. SrcLoc.GetSrcFileLineStr(),
  113. _T("Error registering with Event Viewer"),
  114. szMsgBuf,
  115. dwErr );
  116. DWORD dwDummy= MAXBUF;
  117. ::GetUserName( szMsgBuf, &dwDummy );
  118. CBuildSrcFileLinenoStr SrcLoc2( __FILE__, __LINE__ );
  119. CEvent::ReportWFEvent( SrcLoc2.GetSrcFileLineStr(),
  120. SrcLoc2.GetSrcFileLineStr(),
  121. _T("User shows as:"),
  122. szMsgBuf,
  123. dwErr );
  124. }
  125. }
  126. //
  127. // Note that this fn makes no use of class data
  128. // Store a path to this DLL in registry
  129. VOID CRegisterWithEventViewer::RegisterDllPath(HKEY hk, HMODULE hModule)
  130. {
  131. TCHAR szPath[MAXBUF];
  132. DWORD len;
  133. DWORD dwErr;
  134. if (hModule)
  135. {
  136. if ((len = ::GetModuleFileName(hModule, szPath, MAXBUF-1))!=0)
  137. {
  138. szPath[len] = _T('\0');
  139. dwErr= ::RegSetValueEx( hk,
  140. REG_EVT_MF,
  141. 0,
  142. REG_EXPAND_SZ,
  143. (LPBYTE) szPath,
  144. len + sizeof(TCHAR));
  145. if (dwErr)
  146. {
  147. // Logging won't be pretty here, because we just failed to register with the
  148. // Event Viewer, but we'll take what we can get.
  149. TCHAR szMsgBuf[MAXBUF];
  150. DWORD dwDummy= MAXBUF;
  151. ::GetUserName( szMsgBuf, &dwDummy );
  152. CBuildSrcFileLinenoStr SrcLoc( __FILE__, __LINE__ );
  153. CEvent::ReportWFEvent( SrcLoc.GetSrcFileLineStr(),
  154. SrcLoc.GetSrcFileLineStr(),
  155. _T("Error registering with Event Viewer"),
  156. szMsgBuf,
  157. dwErr );
  158. }
  159. }
  160. }
  161. }
  162. //
  163. // Note that this fn makes no use of class data
  164. // Register what type of event text queries (errors, warnings, info types) this DLL supports
  165. VOID CRegisterWithEventViewer::RegisterEventTypes(HKEY hk)
  166. {
  167. DWORD dwData;
  168. DWORD dwErr;
  169. dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE |
  170. EVENTLOG_INFORMATION_TYPE;
  171. dwErr= ::RegSetValueEx(hk,
  172. REG_EVT_TS,
  173. 0,
  174. REG_DWORD,
  175. (LPBYTE) &dwData,
  176. sizeof(DWORD));
  177. if (dwErr)
  178. {
  179. // Logging won't be pretty here, because we just failed to register with the
  180. // Event Viewer, but we'll take what we can get.
  181. TCHAR szMsgBuf[MAXBUF];
  182. DWORD dwDummy= MAXBUF;
  183. ::GetUserName( szMsgBuf, &dwDummy );
  184. CBuildSrcFileLinenoStr SrcLoc( __FILE__, __LINE__ );
  185. CEvent::ReportWFEvent( SrcLoc.GetSrcFileLineStr(),
  186. SrcLoc.GetSrcFileLineStr(),
  187. _T("Error registering with Event Viewer"),
  188. szMsgBuf,
  189. dwErr );
  190. }
  191. }