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.

384 lines
8.9 KiB

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1996-1998 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. *pnHelpContext = 0;
  112. if ( nMaxError != 0 && lpszError != NULL )
  113. *lpszError = '\0';
  114. return FALSE;
  115. }
  116. virtual int ReportError( UINT nType = MB_OK, UINT nError = 0 );
  117. #if DBG || defined( _DEBUG )
  118. void PASCAL operator delete( void * pbData )
  119. {
  120. // check for proper exception object deletion
  121. CException * pException = (CException *) pbData;
  122. // use: pException->Delete(), do not use: delete pException
  123. ASSERT( pException->m_bReadyForDelete );
  124. ASSERT( pException->m_bAutoDelete > 0 );
  125. // avoid crash when assert above is ignored
  126. if ( pException->m_bReadyForDelete && pException->m_bAutoDelete > 0 )
  127. ::operator delete( pbData );
  128. }
  129. #endif // DBG || defined( _DEBUG )
  130. }; // class CException
  131. #endif // __AFX_H__
  132. /////////////////////////////////////////////////////////////////////////////
  133. // class CExceptionWithOper
  134. /////////////////////////////////////////////////////////////////////////////
  135. typedef int (WINAPI *PFNMSGBOX)( DWORD dwParam, LPCTSTR lpszText, UINT nType, UINT nIDHelp );
  136. class CExceptionWithOper : public CException
  137. {
  138. #ifdef __AFX_H__
  139. // abstract class for dynamic type checking
  140. DECLARE_DYNAMIC( CExceptionWithOper )
  141. #endif // __AFX_H__
  142. public:
  143. // Constructors
  144. CExceptionWithOper(
  145. IN UINT idsOperation,
  146. IN LPCTSTR pszOperArg1 = NULL,
  147. IN LPCTSTR pszOperArg2 = NULL
  148. )
  149. {
  150. SetOperation(idsOperation, pszOperArg1, pszOperArg2);
  151. } // CExceptionWithOper()
  152. CExceptionWithOper(
  153. IN UINT idsOperation,
  154. IN LPCTSTR pszOperArg1,
  155. IN LPCTSTR pszOperArg2,
  156. IN BOOL bAutoDelete
  157. )
  158. : CException( bAutoDelete )
  159. {
  160. SetOperation( idsOperation, pszOperArg1, pszOperArg2 );
  161. } // CExceptionWithOper(bAutoDelete)
  162. // Operations
  163. public:
  164. virtual BOOL GetErrorMessage(
  165. LPTSTR lpszError,
  166. UINT nMaxError,
  167. PUINT pnHelpContext = NULL
  168. )
  169. {
  170. // Format the operation string.
  171. FormatWithOperation( lpszError, nMaxError, NULL );
  172. return TRUE;
  173. } // GetErrorMessage()
  174. virtual int ReportError(
  175. UINT nType = MB_OK,
  176. UINT nError = 0
  177. );
  178. virtual int ReportError(
  179. HWND hwndParent,
  180. UINT nType = MB_OK,
  181. UINT nError = 0
  182. );
  183. virtual int ReportError(
  184. PFNMSGBOX pfnMsgBox,
  185. DWORD dwParam,
  186. UINT nType = MB_OK,
  187. UINT nError = 0
  188. );
  189. void SetOperation(
  190. IN UINT idsOperation,
  191. IN LPCTSTR pszOperArg1 = NULL,
  192. IN LPCTSTR pszOperArg2 = NULL
  193. );
  194. void SetOperationIfEmpty(
  195. IN UINT idsOperation,
  196. IN LPCTSTR pszOperArg1 = NULL,
  197. IN LPCTSTR pszOperArg2 = NULL
  198. )
  199. {
  200. if ( m_idsOperation == 0 )
  201. {
  202. SetOperation( idsOperation, pszOperArg1, pszOperArg2 );
  203. } // if: exception is empty
  204. } //*** SetOperationIfEmpty()
  205. void FormatWithOperation(
  206. OUT LPTSTR lpszError,
  207. IN UINT nMaxError,
  208. IN LPCTSTR pszMsg
  209. );
  210. // Implementation
  211. protected:
  212. UINT m_idsOperation;
  213. TCHAR m_szOperArg1[EXCEPT_MAX_OPER_ARG_LENGTH];
  214. TCHAR m_szOperArg2[EXCEPT_MAX_OPER_ARG_LENGTH];
  215. public:
  216. UINT IdsOperation( void ) const { return m_idsOperation; }
  217. LPTSTR PszOperArg1( void ) { return m_szOperArg1; }
  218. LPTSTR PszOperArg2( void ) { return m_szOperArg2; }
  219. }; //*** class CExceptionWithOper
  220. /////////////////////////////////////////////////////////////////////////////
  221. // class CNTException
  222. /////////////////////////////////////////////////////////////////////////////
  223. class CNTException : public CExceptionWithOper
  224. {
  225. #ifdef __AFX_H__
  226. // abstract class for dynamic type checking
  227. DECLARE_DYNAMIC( CNTException )
  228. #endif // __AFX_H__
  229. public:
  230. // Constructors
  231. CNTException(
  232. IN SC sc,
  233. IN UINT idsOperation = NULL,
  234. IN LPCTSTR pszOperArg1 = NULL,
  235. IN LPCTSTR pszOperArg2 = NULL
  236. )
  237. : CExceptionWithOper( idsOperation, pszOperArg1, pszOperArg2 )
  238. , m_sc( sc )
  239. {
  240. } // CNTException()
  241. CNTException(
  242. IN SC sc,
  243. IN UINT idsOperation,
  244. IN LPCTSTR pszOperArg1,
  245. IN LPCTSTR pszOperArg2,
  246. IN BOOL bAutoDelete
  247. )
  248. : CExceptionWithOper( idsOperation, pszOperArg1, pszOperArg2, bAutoDelete )
  249. , m_sc( sc )
  250. {
  251. } // CNTException( bAutoDelete )
  252. // Operations
  253. public:
  254. virtual BOOL GetErrorMessage(
  255. LPTSTR lpszError,
  256. UINT nMaxError,
  257. PUINT pnHelpContext = NULL
  258. )
  259. {
  260. return FormatErrorMessage( lpszError, nMaxError, pnHelpContext, TRUE /*bIncludeID*/ );
  261. } //*** GetErrorMessage()
  262. BOOL FormatErrorMessage(
  263. LPTSTR lpszError,
  264. UINT nMaxError,
  265. PUINT pnHelpContext = NULL,
  266. BOOL bIncludeID = FALSE
  267. );
  268. void SetOperation(
  269. IN SC sc,
  270. IN UINT idsOperation,
  271. IN LPCTSTR pszOperArg1 = NULL,
  272. IN LPCTSTR pszOperArg2 = NULL
  273. )
  274. {
  275. m_sc = sc;
  276. CExceptionWithOper::SetOperation( idsOperation, pszOperArg1, pszOperArg2 );
  277. } //*** SetOperation()
  278. void SetOperationIfEmpty(
  279. IN SC sc,
  280. IN UINT idsOperation,
  281. IN LPCTSTR pszOperArg1 = NULL,
  282. IN LPCTSTR pszOperArg2 = NULL
  283. )
  284. {
  285. if ( (m_sc == ERROR_SUCCESS) && (m_idsOperation == 0) )
  286. {
  287. m_sc = sc;
  288. CExceptionWithOper::SetOperation( idsOperation, pszOperArg1, pszOperArg2 );
  289. } // if: exception is empty
  290. } //*** SetOperationIfEmpty()
  291. // Implementation
  292. protected:
  293. SC m_sc;
  294. public:
  295. SC Sc( void ) { return m_sc; }
  296. }; //*** class CNTException
  297. /////////////////////////////////////////////////////////////////////////////
  298. // Global Functions
  299. /////////////////////////////////////////////////////////////////////////////
  300. void ThrowStaticException(
  301. IN UINT idsOperation = NULL,
  302. IN LPCTSTR pszOperArg1 = NULL,
  303. IN LPCTSTR pszOperArg2 = NULL
  304. );
  305. void ThrowStaticException(
  306. IN SC sc,
  307. IN UINT idsOperation = NULL,
  308. IN LPCTSTR pszOperArg1 = NULL,
  309. IN LPCTSTR pszOperArg2 = NULL
  310. );
  311. BOOL FormatErrorMessage(
  312. DWORD sc,
  313. LPTSTR lpszError,
  314. UINT nMaxError
  315. );
  316. /////////////////////////////////////////////////////////////////////////////
  317. #endif // _EXCOPER_H_