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.

528 lines
12 KiB

  1. /*++
  2. Copyright (c) 1994-2002 Microsoft Corporation
  3. Module Name :
  4. msg.h
  5. Abstract:
  6. Message Functions Definitions
  7. Author:
  8. Ronald Meijer (ronaldm)
  9. Sergei Antonov (sergeia)
  10. Project:
  11. Internet Services Manager
  12. Revision History:
  13. --*/
  14. #ifndef _MSG_H
  15. #define _MSG_H
  16. //
  17. // Slightly easier syntax to register a facility
  18. //
  19. #define REGISTER_FACILITY(dwCode, lpSource)\
  20. CError::RegisterFacility(dwCode, lpSource)
  21. //
  22. // Helper Function
  23. //
  24. HRESULT GetLastHRESULT();
  25. BOOL InitErrorFunctionality();
  26. void TerminateErrorFunctionality();
  27. typedef struct tagFACILITY
  28. {
  29. LPCTSTR lpszDll;
  30. UINT nTextID;
  31. } FACILITY;
  32. typedef CMap<DWORD, DWORD &, CString, CString &> CMapDWORDtoCString;
  33. typedef CMap<HRESULT, HRESULT &, UINT, UINT &> CMapHRESULTtoUINT;
  34. class COMDLL CError
  35. /*++
  36. Class Description:
  37. Error handling class, works for both HRESULT and old-style DWORD
  38. error codes. Construct with or assign a DWORD or HRESULT error
  39. return code, and the object can then be used to determine success
  40. or failure, and the object provides text for the error code either
  41. directly, in a message, or formatted with additional text. Also,
  42. the CError object understands the range of winsock errors and
  43. lanman errors, and looks for them in the appropriate places.
  44. The object can be referenced as a BOOL, a DWORD, an HRESULT, or
  45. a LPCTSTR as a success/failure, a WIN32 error, and HRESULT or
  46. the text equivalent respectively.
  47. Example of typical programme flow:
  48. CError err(FunctionWhichReturnsHresult());
  49. //
  50. // Use IDS_MY_ERROR for access denied errors for the
  51. // duration of this scope.
  52. //
  53. err.AddOverride(ERROR_ACCESS_DENIED, IDS_MY_ERROR);
  54. if (!err.MessageBoxOnFailure())
  55. {
  56. //
  57. // If we failed, this already displayed the error
  58. // message in a messagebox. Only when we succeed
  59. // we get here.
  60. //
  61. ... stuff ...
  62. }
  63. SomeWinApiWhichSetsLastError();
  64. err.GetLastWinError();
  65. if (err.Failed())
  66. {
  67. printf("WIN32 Error code %ld\nHRESULT %ld\nText: %s\n",
  68. (DWORD)err,
  69. (HRESULT)err,
  70. (LPCTSTR)err
  71. );
  72. }
  73. Public Interface:
  74. TextFromHRESULT : Convert HRESULT to text
  75. TextFromHRESULTExpand : Expand %h string to error text, %H to error code
  76. MessageBox : Display error in a messagebox
  77. MessageBoxFormat : Use %h string as format in messagebox
  78. MessageBoxOnFailure : Display message if error is a failure
  79. AddOverride : Add message override with string ID
  80. RemoveOverride : Remove message override
  81. RegisterFacility : Register facility
  82. UnregisterFacility : Unregister facility
  83. Succeeded : Determine if the error code indicates a success
  84. Failed : Determine if the error code indicates a failure
  85. CError : Constructors
  86. Reset : Reset error code
  87. GetLastWinError : Assign internal code to GetLastError
  88. SetLastWinError : Set last error from internal code
  89. operator = : Assignment operators
  90. operator == : Comparison operators
  91. operator != : Comparison operators
  92. operator LPOLESTR : Conversion operator
  93. operator LPCTSTR : Conversion operator
  94. operator HRESULT : Conversion operator
  95. operator DWORD : Conversion operator
  96. operator BOOL : Conversion operator
  97. --*/
  98. {
  99. #define IS_HRESULT(hr) (hr & 0xffff0000)
  100. #define REMOVE_OVERRIDE ((UINT)-1)
  101. #define NO_HELP_CONTEXT ((UINT)-1)
  102. #define USE_LAST_ERROR (TRUE)
  103. //
  104. // Private Internal FACILITY codes
  105. //
  106. #define FACILITY_WINSOCK (0xffe)
  107. #define FACILITY_LANMAN (0xfff)
  108. //
  109. // Static Helpers
  110. //
  111. public:
  112. //
  113. // Success/Failure determinants, works regardless
  114. // of whether hrCode is a DWORD or HRESULT
  115. //
  116. static BOOL Succeeded(HRESULT hrCode);
  117. static BOOL Failed(HRESULT hrCode);
  118. //
  119. // Guarantee return is WIN32 error code
  120. //
  121. static DWORD Win32Error(HRESULT hrCode) { return HRESULT_CODE(hrCode); }
  122. //
  123. // Guarantee return is a true HRESULT
  124. //
  125. static HRESULT HResult(HRESULT hrCode) { return HRESULT_FROM_WIN32(hrCode); }
  126. //
  127. // Register a DLL for a given facility code.
  128. // Use NULL to unregister the facility
  129. //
  130. static void RegisterFacility(
  131. DWORD dwFacility,
  132. LPCSTR lpDLL = NULL
  133. );
  134. static void UnregisterFacility(
  135. DWORD dwFacility
  136. );
  137. //
  138. // Constructor/Destructor
  139. //
  140. public:
  141. //
  142. // If constructed with TRUE, the object is initialized to
  143. // last error. It's set to ERROR_SUCCESS otherwise (default case)
  144. //
  145. CError();
  146. CError(HRESULT hrCode);
  147. CError(DWORD dwCode);
  148. ~CError();
  149. //
  150. // Helpers
  151. //
  152. public:
  153. BOOL Succeeded() const { return SUCCEEDED(m_hrCode); }
  154. BOOL Failed() const { return FAILED(m_hrCode); }
  155. HRESULT TextFromHRESULT(
  156. LPTSTR szBuffer,
  157. DWORD cchBuffer
  158. ) const;
  159. HRESULT TextFromHRESULT(
  160. CString & strMsg
  161. ) const;
  162. LPCTSTR TextFromHRESULTExpand(
  163. LPTSTR szBuffer,
  164. DWORD cchBuffer,
  165. HRESULT * phResult = NULL
  166. ) const;
  167. LPCTSTR TextFromHRESULTExpand(
  168. CString & strBuffer
  169. ) const;
  170. int MessageBox(
  171. HWND hWnd = NULL,
  172. UINT nType = MB_OK | MB_ICONWARNING,
  173. UINT nHelpContext = NO_HELP_CONTEXT
  174. ) const;
  175. BOOL MessageBoxOnFailure(
  176. HWND hWnd = NULL,
  177. UINT nType = MB_OK | MB_ICONWARNING,
  178. UINT nHelpContext = NO_HELP_CONTEXT
  179. ) const;
  180. int MessageBoxFormat(
  181. HWND hWnd,
  182. UINT nFmt,
  183. UINT nType,
  184. UINT nHelpContext,
  185. ...
  186. ) const;
  187. void Reset();
  188. void GetLastWinError();
  189. void SetLastWinError() const;
  190. DWORD Win32Error() const;
  191. HRESULT HResult() const { return m_hrCode; }
  192. //
  193. // Add override for specific error code.
  194. // Use -1 to remove the override. This function
  195. // will return the previous override (or -1)
  196. //
  197. UINT AddOverride(
  198. HRESULT hrCode,
  199. UINT nMessage = REMOVE_OVERRIDE
  200. );
  201. void RemoveOverride(
  202. HRESULT hrCode
  203. );
  204. void RemoveAllOverrides();
  205. protected:
  206. //
  207. // Expand escape code
  208. //
  209. BOOL ExpandEscapeCode(
  210. LPTSTR szBuffer,
  211. DWORD cchBuffer,
  212. LPTSTR & lp,
  213. CString & strReplacement,
  214. HRESULT & hr
  215. ) const;
  216. //
  217. // Check for override message
  218. //
  219. BOOL HasOverride(
  220. UINT * pnMessage = NULL
  221. ) const;
  222. //
  223. // Assignment Operators
  224. //
  225. public:
  226. const CError & operator =(HRESULT hr);
  227. const CError & operator =(const CError & err);
  228. //
  229. // Comparison Operators
  230. //
  231. public:
  232. const BOOL operator ==(HRESULT hr);
  233. const BOOL operator ==(CError & err);
  234. const BOOL operator !=(HRESULT hr);
  235. const BOOL operator !=(CError & err);
  236. //
  237. // Conversion Operators
  238. //
  239. public:
  240. operator const HRESULT() const { return m_hrCode; }
  241. operator const DWORD() const;
  242. operator const BOOL() const;
  243. operator LPOLESTR();
  244. operator LPCTSTR();
  245. #if defined(_DEBUG) || DBG
  246. public:
  247. //
  248. // CDumpContext stream operator
  249. //
  250. inline friend CDumpContext & AFXAPI operator <<(
  251. CDumpContext & dc,
  252. const CError & value
  253. )
  254. {
  255. return dc << (DWORD)value.m_hrCode;
  256. }
  257. #endif // _DEBUG
  258. protected:
  259. static HRESULT CvtToInternalFormat(HRESULT hrCode);
  260. //
  261. // Check for FACILITY dll
  262. //
  263. static LPCTSTR FindFacility(
  264. DWORD dwFacility
  265. );
  266. protected:
  267. friend BOOL InitErrorFunctionality();
  268. friend void TerminateErrorFunctionality();
  269. static BOOL AllocateStatics();
  270. static void DeAllocateStatics();
  271. static BOOL AreStaticsAllocated() { return s_fAllocated; }
  272. protected:
  273. static const TCHAR s_chEscape; // Escape character
  274. static const TCHAR s_chEscText; // Escape code for text
  275. static const TCHAR s_chEscNumber; // Escape code for error code
  276. static LPCTSTR s_cszLMDLL; // Lanman Message DLL
  277. static LPCTSTR s_cszWSDLL; // Winsock Message DLL
  278. static LPCTSTR s_cszFacility[]; // Facility Table
  279. static HRESULT s_cdwMinLMErr; // Lanman Error Range
  280. static HRESULT s_cdwMaxLMErr; // Lanman Error Range
  281. static HRESULT s_cdwMinWSErr; // Winsock Error Range
  282. static HRESULT s_cdwMaxWSErr; // Winsock Error Range
  283. static DWORD s_cdwFacilities; // Number of facility items
  284. //
  285. // Allocated objects
  286. //
  287. static CString * s_pstrDefError; // Default Error String
  288. static CString * s_pstrDefSuccs; // Default Success String
  289. static CMapDWORDtoCString * s_pmapFacilities;
  290. static BOOL s_fAllocated;
  291. protected:
  292. const CError & Construct(HRESULT hr);
  293. const CError & Construct(const CError & err);
  294. CMapHRESULTtoUINT mapOverrides;
  295. private:
  296. HRESULT m_hrCode;
  297. CString m_str;
  298. };
  299. //
  300. // Inline Expansions
  301. //
  302. // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  303. inline HRESULT GetLastHRESULT()
  304. {
  305. return CError::HResult(::GetLastError());
  306. }
  307. inline /* static */ BOOL CError::Succeeded(HRESULT hrCode)
  308. {
  309. //
  310. // Works with either HRESULT or WIN32 error code
  311. //
  312. return IS_HRESULT(hrCode)
  313. ? SUCCEEDED(hrCode)
  314. : hrCode == ERROR_SUCCESS;
  315. }
  316. inline /* static */ BOOL CError::Failed(HRESULT hrCode)
  317. {
  318. //
  319. // Works with either HRESULT or WIN32 error code
  320. //
  321. return IS_HRESULT(hrCode)
  322. ? FAILED(hrCode)
  323. : hrCode != ERROR_SUCCESS;
  324. }
  325. inline /* static */ void CError::UnregisterFacility(
  326. DWORD dwFacility
  327. )
  328. {
  329. RegisterFacility(dwFacility, NULL);
  330. }
  331. inline CError::CError()
  332. {
  333. Construct(S_OK);
  334. }
  335. inline CError::CError(HRESULT hrCode)
  336. {
  337. Construct(hrCode);
  338. }
  339. inline CError::CError(DWORD dwCode)
  340. {
  341. Construct((HRESULT)dwCode);
  342. }
  343. inline DWORD CError::Win32Error() const
  344. {
  345. return CError::Win32Error(m_hrCode);
  346. }
  347. inline void CError::Reset()
  348. {
  349. m_hrCode = S_OK;
  350. }
  351. inline void CError::GetLastWinError()
  352. {
  353. Construct(::GetLastError());
  354. }
  355. inline void CError::SetLastWinError() const
  356. {
  357. ::SetLastError(Win32Error(m_hrCode));
  358. }
  359. inline void CError::RemoveOverride(
  360. HRESULT hrCode
  361. )
  362. {
  363. (void)CError::AddOverride(hrCode, REMOVE_OVERRIDE);
  364. }
  365. inline const CError & CError::operator =(HRESULT hr)
  366. {
  367. return Construct(hr);
  368. }
  369. inline const CError & CError::operator =(const CError & err)
  370. {
  371. return Construct(err);
  372. }
  373. inline const BOOL CError::operator ==(HRESULT hr)
  374. {
  375. return m_hrCode == hr;
  376. }
  377. inline const BOOL CError::operator ==(CError & err)
  378. {
  379. return m_hrCode == err.m_hrCode;
  380. }
  381. inline const BOOL CError::operator !=(HRESULT hr)
  382. {
  383. return m_hrCode != hr;
  384. }
  385. inline const BOOL CError::operator !=(CError & err)
  386. {
  387. return m_hrCode != err.m_hrCode;
  388. }
  389. inline CError::operator const DWORD() const
  390. {
  391. return Win32Error();
  392. }
  393. inline CError::operator const BOOL() const
  394. {
  395. return Succeeded();
  396. }
  397. inline CError::operator LPOLESTR()
  398. {
  399. TextFromHRESULT(m_str);
  400. return m_str.GetBuffer(0);
  401. }
  402. inline CError::operator LPCTSTR()
  403. {
  404. TextFromHRESULT(m_str);
  405. return m_str;
  406. }
  407. //
  408. // AfxMessageBox helpers
  409. //
  410. // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  411. inline BOOL NoYesMessageBox(UINT nID)
  412. {
  413. return AfxMessageBox(nID, MB_YESNO|MB_ICONQUESTION|MB_DEFBUTTON2) == IDYES;
  414. }
  415. inline BOOL NoYesMessageBox(CString & str)
  416. {
  417. return AfxMessageBox(str, MB_YESNO|MB_ICONQUESTION|MB_DEFBUTTON2) == IDYES;
  418. }
  419. inline BOOL YesNoMessageBox(UINT nID)
  420. {
  421. return AfxMessageBox(nID, MB_YESNO|MB_ICONQUESTION|MB_DEFBUTTON1) == IDYES;
  422. }
  423. inline BOOL YesNoMessageBox(CString & str)
  424. {
  425. return AfxMessageBox(str, MB_YESNO|MB_ICONQUESTION|MB_DEFBUTTON1) == IDYES;
  426. }
  427. #endif // _MSG_H