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.

392 lines
11 KiB

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1996-2002 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // ExcOper.h
  7. //
  8. // Implementation File:
  9. // ExcOper.cpp
  10. //
  11. // Description:
  12. // Definition of the exception classes.
  13. //
  14. // Author:
  15. // David Potter (davidp) May 20, 1996
  16. //
  17. // Revision History:
  18. //
  19. // Notes:
  20. //
  21. /////////////////////////////////////////////////////////////////////////////
  22. #ifndef _EXCOPER_H_
  23. #define _EXCOPER_H_
  24. /////////////////////////////////////////////////////////////////////////////
  25. // Include Files
  26. /////////////////////////////////////////////////////////////////////////////
  27. /////////////////////////////////////////////////////////////////////////////
  28. // Forward Class Declarations
  29. /////////////////////////////////////////////////////////////////////////////
  30. class CExceptionWithOper;
  31. class CNTException;
  32. /////////////////////////////////////////////////////////////////////////////
  33. // Type Definitions
  34. /////////////////////////////////////////////////////////////////////////////
  35. typedef DWORD SC;
  36. #define EXCEPT_MAX_OPER_ARG_LENGTH 260
  37. /////////////////////////////////////////////////////////////////////////////
  38. // Wire in MFC if this is an MFC image.
  39. /////////////////////////////////////////////////////////////////////////////
  40. #ifdef __AFX_H__
  41. #define IDP_NO_ERROR_AVAILABLE AFX_IDP_NO_ERROR_AVAILABLE
  42. inline int EXC_AppMessageBox( LPCTSTR lpszText, UINT nType = MB_OK, UINT nIDHelp = 0 )
  43. {
  44. return AfxMessageBox( lpszText, nType, nIDHelp );
  45. }
  46. inline int EXC_AppMessageBox( UINT nIDPrompt, UINT nType = MB_OK, UINT nIDHelp = (UINT)-1 )
  47. {
  48. return AfxMessageBox( nIDPrompt, nType, nIDHelp );
  49. }
  50. inline int EXC_AppMessageBox( HWND hwndParent, LPCTSTR lpszText, UINT nType = MB_OK, UINT nIDHelp = 0 )
  51. {
  52. return AfxMessageBox( lpszText, nType, nIDHelp );
  53. }
  54. inline int EXC_AppMessageBox( HWND hwndParent, UINT nIDPrompt, UINT nType = MB_OK, UINT nIDHelp = (UINT)-1 )
  55. {
  56. return AfxMessageBox( nIDPrompt, nType, nIDHelp );
  57. }
  58. inline HINSTANCE EXC_GetResourceInstance( void )
  59. {
  60. return AfxGetApp()->m_hInstance;
  61. }
  62. #endif // __AFX_H__
  63. /////////////////////////////////////////////////////////////////////////////
  64. // class CException
  65. /////////////////////////////////////////////////////////////////////////////
  66. #ifndef __AFX_H__
  67. class CException
  68. {
  69. public:
  70. BOOL m_bAutoDelete;
  71. #if DBG || defined( _DEBUG )
  72. protected:
  73. BOOL m_bReadyForDelete;
  74. public:
  75. #endif // DBG || defined( _DEBUG )
  76. CException( void )
  77. {
  78. m_bAutoDelete = TRUE;
  79. #if DBG || defined( _DEBUG )
  80. m_bReadyForDelete = FALSE;
  81. #endif // DBG || defined( _DEBUG )
  82. }
  83. CException( BOOL bAutoDelete )
  84. {
  85. m_bAutoDelete = bAutoDelete;
  86. #if DBG || defined( _DEBUG )
  87. m_bReadyForDelete = FALSE;
  88. #endif // DBG || defined( _DEBUG )
  89. }
  90. virtual ~CException( void )
  91. {
  92. }
  93. void Delete( void ) // use to delete exception in 'catch' block
  94. {
  95. // delete exception if it is auto-deleting
  96. if ( m_bAutoDelete > 0 )
  97. {
  98. #if DBG || defined( _DEBUG )
  99. m_bReadyForDelete = TRUE;
  100. #endif // DBG || defined( _DEBUG )
  101. delete this;
  102. }
  103. }
  104. virtual BOOL GetErrorMessage(
  105. LPTSTR lpszError,
  106. UINT nMaxError,
  107. PUINT pnHelpContext = NULL
  108. )
  109. {
  110. if ( pnHelpContext != NULL )
  111. {
  112. *pnHelpContext = 0;
  113. }
  114. if ( nMaxError != 0 && lpszError != NULL )
  115. {
  116. *lpszError = '\0';
  117. }
  118. return FALSE;
  119. }
  120. virtual int ReportError( UINT nType = MB_OK, UINT nError = 0 );
  121. #if DBG || defined( _DEBUG )
  122. void PASCAL operator delete( void * pbData )
  123. {
  124. // check for proper exception object deletion
  125. CException * pException = (CException *) pbData;
  126. // use: pException->Delete(), do not use: delete pException
  127. ASSERT( pException->m_bReadyForDelete );
  128. ASSERT( pException->m_bAutoDelete > 0 );
  129. // avoid crash when assert above is ignored
  130. if ( pException->m_bReadyForDelete && pException->m_bAutoDelete > 0 )
  131. {
  132. ::operator delete( pbData );
  133. }
  134. }
  135. #endif // DBG || defined( _DEBUG )
  136. }; // class CException
  137. #endif // __AFX_H__
  138. /////////////////////////////////////////////////////////////////////////////
  139. // class CExceptionWithOper
  140. /////////////////////////////////////////////////////////////////////////////
  141. typedef int (WINAPI *PFNMSGBOX)( DWORD dwParam, LPCTSTR lpszText, UINT nType, UINT nIDHelp );
  142. class CExceptionWithOper : public CException
  143. {
  144. #ifdef __AFX_H__
  145. // abstract class for dynamic type checking
  146. DECLARE_DYNAMIC( CExceptionWithOper )
  147. #endif // __AFX_H__
  148. public:
  149. // Constructors
  150. CExceptionWithOper(
  151. IN UINT idsOperation,
  152. IN LPCTSTR pszOperArg1 = NULL,
  153. IN LPCTSTR pszOperArg2 = NULL
  154. )
  155. {
  156. SetOperation(idsOperation, pszOperArg1, pszOperArg2);
  157. } // CExceptionWithOper()
  158. CExceptionWithOper(
  159. IN UINT idsOperation,
  160. IN LPCTSTR pszOperArg1,
  161. IN LPCTSTR pszOperArg2,
  162. IN BOOL bAutoDelete
  163. )
  164. : CException( bAutoDelete )
  165. {
  166. SetOperation( idsOperation, pszOperArg1, pszOperArg2 );
  167. } // CExceptionWithOper(bAutoDelete)
  168. // Operations
  169. public:
  170. virtual BOOL GetErrorMessage(
  171. LPTSTR lpszError,
  172. UINT nMaxError,
  173. PUINT pnHelpContext = NULL
  174. )
  175. {
  176. UNREFERENCED_PARAMETER( pnHelpContext );
  177. // Format the operation string.
  178. FormatWithOperation( lpszError, nMaxError, NULL );
  179. return TRUE;
  180. } // GetErrorMessage()
  181. virtual int ReportError(
  182. UINT nType = MB_OK,
  183. UINT nError = 0
  184. );
  185. virtual int ReportError(
  186. HWND hwndParent,
  187. UINT nType = MB_OK,
  188. UINT nError = 0
  189. );
  190. virtual int ReportError(
  191. PFNMSGBOX pfnMsgBox,
  192. DWORD dwParam,
  193. UINT nType = MB_OK,
  194. UINT nError = 0
  195. );
  196. void SetOperation(
  197. IN UINT idsOperation,
  198. IN LPCTSTR pszOperArg1 = NULL,
  199. IN LPCTSTR pszOperArg2 = NULL
  200. );
  201. void SetOperationIfEmpty(
  202. IN UINT idsOperation,
  203. IN LPCTSTR pszOperArg1 = NULL,
  204. IN LPCTSTR pszOperArg2 = NULL
  205. )
  206. {
  207. if ( m_idsOperation == 0 )
  208. {
  209. SetOperation( idsOperation, pszOperArg1, pszOperArg2 );
  210. } // if: exception is empty
  211. } //*** SetOperationIfEmpty()
  212. void FormatWithOperation(
  213. OUT LPTSTR lpszError,
  214. IN UINT nMaxError,
  215. IN LPCTSTR pszMsg
  216. );
  217. // Implementation
  218. protected:
  219. UINT m_idsOperation;
  220. TCHAR m_szOperArg1[EXCEPT_MAX_OPER_ARG_LENGTH];
  221. TCHAR m_szOperArg2[EXCEPT_MAX_OPER_ARG_LENGTH];
  222. public:
  223. UINT IdsOperation( void ) const { return m_idsOperation; }
  224. LPTSTR PszOperArg1( void ) { return m_szOperArg1; }
  225. LPTSTR PszOperArg2( void ) { return m_szOperArg2; }
  226. }; //*** class CExceptionWithOper
  227. /////////////////////////////////////////////////////////////////////////////
  228. // class CNTException
  229. /////////////////////////////////////////////////////////////////////////////
  230. class CNTException : public CExceptionWithOper
  231. {
  232. #ifdef __AFX_H__
  233. // abstract class for dynamic type checking
  234. DECLARE_DYNAMIC( CNTException )
  235. #endif // __AFX_H__
  236. public:
  237. // Constructors
  238. CNTException(
  239. IN SC sc,
  240. IN UINT idsOperation = NULL,
  241. IN LPCTSTR pszOperArg1 = NULL,
  242. IN LPCTSTR pszOperArg2 = NULL
  243. )
  244. : CExceptionWithOper( idsOperation, pszOperArg1, pszOperArg2 )
  245. , m_sc( sc )
  246. {
  247. } // CNTException()
  248. CNTException(
  249. IN SC sc,
  250. IN UINT idsOperation,
  251. IN LPCTSTR pszOperArg1,
  252. IN LPCTSTR pszOperArg2,
  253. IN BOOL bAutoDelete
  254. )
  255. : CExceptionWithOper( idsOperation, pszOperArg1, pszOperArg2, bAutoDelete )
  256. , m_sc( sc )
  257. {
  258. } // CNTException( bAutoDelete )
  259. // Operations
  260. public:
  261. virtual BOOL GetErrorMessage(
  262. LPTSTR lpszError,
  263. UINT nMaxError,
  264. PUINT pnHelpContext = NULL
  265. )
  266. {
  267. return FormatErrorMessage( lpszError, nMaxError, pnHelpContext, TRUE /*bIncludeID*/ );
  268. } //*** GetErrorMessage()
  269. BOOL FormatErrorMessage(
  270. LPTSTR lpszError,
  271. UINT nMaxError,
  272. PUINT pnHelpContext = NULL,
  273. BOOL bIncludeID = FALSE
  274. );
  275. void SetOperation(
  276. IN SC sc,
  277. IN UINT idsOperation,
  278. IN LPCTSTR pszOperArg1 = NULL,
  279. IN LPCTSTR pszOperArg2 = NULL
  280. )
  281. {
  282. m_sc = sc;
  283. CExceptionWithOper::SetOperation( idsOperation, pszOperArg1, pszOperArg2 );
  284. } //*** SetOperation()
  285. void SetOperationIfEmpty(
  286. IN SC sc,
  287. IN UINT idsOperation,
  288. IN LPCTSTR pszOperArg1 = NULL,
  289. IN LPCTSTR pszOperArg2 = NULL
  290. )
  291. {
  292. if ( (m_sc == ERROR_SUCCESS) && (m_idsOperation == 0) )
  293. {
  294. m_sc = sc;
  295. CExceptionWithOper::SetOperation( idsOperation, pszOperArg1, pszOperArg2 );
  296. } // if: exception is empty
  297. } //*** SetOperationIfEmpty()
  298. // Implementation
  299. protected:
  300. SC m_sc;
  301. public:
  302. SC Sc( void ) { return m_sc; }
  303. }; //*** class CNTException
  304. /////////////////////////////////////////////////////////////////////////////
  305. // Global Functions
  306. /////////////////////////////////////////////////////////////////////////////
  307. void ThrowStaticException(
  308. IN UINT idsOperation = NULL,
  309. IN LPCTSTR pszOperArg1 = NULL,
  310. IN LPCTSTR pszOperArg2 = NULL
  311. );
  312. void ThrowStaticException(
  313. IN SC sc,
  314. IN UINT idsOperation = NULL,
  315. IN LPCTSTR pszOperArg1 = NULL,
  316. IN LPCTSTR pszOperArg2 = NULL
  317. );
  318. BOOL FormatErrorMessage(
  319. DWORD sc,
  320. LPTSTR lpszError,
  321. UINT nMaxError
  322. );
  323. /////////////////////////////////////////////////////////////////////////////
  324. #endif // _EXCOPER_H_