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.

764 lines
16 KiB

  1. /*++
  2. 1998 Seagate Software, Inc. All rights reserved.
  3. Module Name:
  4. Wsbassrt.h
  5. Abstract:
  6. This header file defines the part of the platform code that is
  7. responsible for the low level error handling used by all other
  8. modules.
  9. Author:
  10. Rohde Wakefield [rohde] 23-Oct-1996
  11. Revision History:
  12. Brian Dodd [brian] 6-Dec-1996
  13. added WsbAssertStatus, WsbAssertNoError, WsbAssertHandle
  14. added WsbAffirmStatus, WsbAffirmNoError, WsbAffirmHandle
  15. Michael Lotz [lotz] 3-Mar-1997
  16. added WsbAffirmNtStatus
  17. Cat Brant [cbrant] 10-Feb-1998
  18. added WsbAssertNtStatus
  19. --*/
  20. #include "stdio.h"
  21. #include "crtdbg.h"
  22. #include "wsbtrace.h"
  23. #ifndef _WSBASSRT_
  24. #define _WSBASSRT_
  25. //
  26. // The following macros should be used when dealing with
  27. // many HRESULT return values in C++ exception handling.
  28. /*++
  29. Macro Name:
  30. WsbAssert
  31. Macro Description:
  32. Should be used for checking conditions that if seen
  33. would be considered coding errors (i.e. the conditions should
  34. never occur).
  35. Arguments:
  36. cond - A boolean expression for the condition to check.
  37. hr - The result parameter to throw if the condition is false.
  38. --*/
  39. #define WsbAssert(cond, hr) \
  40. if (!(cond)) { \
  41. WsbLogEvent(WSB_MESSAGE_PROGRAM_ASSERT, 0, NULL, WsbHrAsString(hr), NULL); \
  42. _ASSERTE(cond); \
  43. WsbThrow(hr); \
  44. }
  45. /*++
  46. Macro Name:
  47. WsbThrow
  48. Macro Description:
  49. Throw the argument.
  50. Arguments:
  51. hr - Parameter to throw.
  52. --*/
  53. #ifdef WSB_TRACE_IS
  54. #define WsbThrow(hr) \
  55. { \
  56. WsbTrace(OLESTR("WsbThrow <%hs>, <%d>, hr = <%ls>.\n"), __FILE__, __LINE__, WsbHrAsString(hr)); \
  57. throw((HRESULT)hr); \
  58. }
  59. #else
  60. #define WsbThrow(hr) throw((HRESULT)hr)
  61. #endif
  62. /*++
  63. Macro Name:
  64. WsbAffirm
  65. Macro Description:
  66. Should be used for checking conditions that are
  67. considered errors to the function (and the function should not
  68. continue), but are the result of errors that are allowable (although
  69. potentially rare). This function has failed, but the caller needs to
  70. determine whether this is a fatal problem, a problem that needs to
  71. be logged an worked around, or whether it can handle the problem.
  72. Arguments:
  73. cond - A boolean expression for the condition to check.
  74. hr - The result parameter to throw if the condition is false.
  75. --*/
  76. #define WsbAffirm(cond, hr) if (!(cond)) WsbThrow(hr)
  77. /*++
  78. Macro Name:
  79. WsbAffirmHr
  80. Macro Description:
  81. Similar to WsbAffirm(), but is used to wrap functions
  82. that return an HRESULT (normally COM methods).
  83. A sample use is:
  84. HRESULT hr = S_OK;
  85. try {
  86. WsbAssert(0 != pUnk);
  87. WsbAffirmHr(CoCreateInstance(...));
  88. } WsbCatch(hr)
  89. return (hr);
  90. Arguments:
  91. hr - Result from a function call.
  92. --*/
  93. #define WsbAffirmHr(hr) \
  94. { \
  95. HRESULT lHr; \
  96. lHr = (hr); \
  97. WsbAffirm(SUCCEEDED(lHr), lHr); \
  98. }
  99. #define WsbAffirmHrOk(hr) \
  100. { \
  101. HRESULT lHr; \
  102. lHr = (hr); \
  103. WsbAffirm(S_OK == lHr, lHr); \
  104. }
  105. /*++
  106. Macro Name:
  107. WsbAssertHr
  108. Macro Description:
  109. Similar to WsbAssert(), but is used to wrap functions
  110. that return an HRESULT (normally COM methods).
  111. Arguments:
  112. hr - Result from a function call.
  113. --*/
  114. #define WsbAssertHr(hr) \
  115. { \
  116. HRESULT lHr; \
  117. lHr = (hr); \
  118. WsbAssert(SUCCEEDED(lHr), lHr); \
  119. }
  120. /*++
  121. Macro Name:
  122. WsbAssertHrOk
  123. Macro Description:
  124. Checks that a function result is S_OK.
  125. Arguments:
  126. hr - Result from a function call.
  127. --*/
  128. #define WsbAssertHrOk(hr) \
  129. { \
  130. HRESULT lHr; \
  131. lHr = (hr); \
  132. WsbAssert(S_OK == lHr, lHr); \
  133. }
  134. /*++
  135. Macro Name:
  136. WsbAssertStatus
  137. Macro Description:
  138. Similar to WsbAssert(), but is used to wrap Win32 functions
  139. that return a BOOL status.
  140. This macro checks the status, and if FALSE, gets the
  141. last error and converts it to HRESULT, then asserts
  142. the result.
  143. Arguments:
  144. status - a BOOL result from a function call.
  145. See Also:
  146. WsbAffirmStatus
  147. --*/
  148. #define WsbAssertStatus(status) \
  149. { \
  150. BOOL bStatus; \
  151. bStatus = (status); \
  152. if (!bStatus) { \
  153. DWORD dwErr = GetLastError(); \
  154. HRESULT lHr = HRESULT_FROM_WIN32(dwErr); \
  155. WsbAssert(SUCCEEDED(lHr), lHr); \
  156. } \
  157. }
  158. /*++
  159. Macro Name:
  160. WsbAssertWin32
  161. Macro Description:
  162. Similar to WsbAssert(), but is used to wrap Win32 functions
  163. that return a Win32 status.
  164. This macro checks the status, and if not ERROR_SUCCESS,
  165. converts it to HRESULT, then asserts the result.
  166. Arguments:
  167. status - a Win32 result from a function call.
  168. See Also:
  169. WsbAffirmStatus
  170. --*/
  171. #define WsbAssertWin32( status ) \
  172. { \
  173. LONG lStatus; \
  174. lStatus = (status); \
  175. if ( lStatus != ERROR_SUCCESS ) { \
  176. HRESULT lHr = HRESULT_FROM_WIN32( lStatus ); \
  177. WsbAssert(SUCCEEDED(lHr), lHr); \
  178. } \
  179. }
  180. /*++
  181. Macro Name:
  182. WsbAssertNoError
  183. Macro Description:
  184. Similar to WsbAssert(), but is used to wrap Win32 functions
  185. that return a DWORD error code. These functions return NO_ERROR
  186. if the function completed successfully.
  187. This macro checks the return value and if an error condition
  188. is detected, the error is converted to an HRESULT, then asserts
  189. the result.
  190. Arguments:
  191. err - a DWORD result from a function call.
  192. See Also:
  193. WsbAffirmNoError
  194. --*/
  195. #define WsbAssertNoError(retval) \
  196. { \
  197. DWORD dwErr; \
  198. dwErr = (retval); \
  199. if (dwErr != NO_ERROR) { \
  200. HRESULT lHr = HRESULT_FROM_WIN32(dwErr); \
  201. WsbAssert(SUCCEEDED(lHr), lHr); \
  202. } \
  203. }
  204. /*++
  205. Macro Name:
  206. WsbAssertHandle
  207. Macro Description:
  208. Similar to WsbAssert(), but is used to wrap Win32 functions
  209. that return a HANDLE.
  210. This macro checks the handle and if it is invalid, gets the
  211. last error, converts it to an HRESULT, then asserts the result.
  212. Arguments:
  213. hndl - a HANDLE result from a function call.
  214. See Also:
  215. WsbAffirmHandle
  216. --*/
  217. #define WsbAssertHandle(hndl) \
  218. { \
  219. HANDLE hHndl; \
  220. hHndl = (hndl); \
  221. if ((hHndl == INVALID_HANDLE_VALUE) || (hHndl == NULL)) { \
  222. DWORD dwErr = GetLastError(); \
  223. HRESULT lHr = HRESULT_FROM_WIN32(dwErr); \
  224. WsbAssert(SUCCEEDED(lHr), lHr); \
  225. } \
  226. }
  227. /*++
  228. Macro Name:
  229. WsbAssertPointer
  230. Macro Description:
  231. Similar to WsbAssert(), but is used specifically to check for
  232. a valid pointer.
  233. This macro asserts that the pointer is non-zero, and throws
  234. E_POINTER if it is not.
  235. Arguments:
  236. ptr - the pointer to test.
  237. See Also:
  238. WsbAffirmPointer
  239. --*/
  240. #define WsbAssertPointer( ptr ) \
  241. { \
  242. WsbAssert( ptr != 0, E_POINTER);\
  243. }
  244. /*++
  245. Macro Name:
  246. WsbAssertAlloc
  247. Macro Description:
  248. Similar to WsbAssert(), but is used specifically to check for
  249. a valid memory allocation.
  250. This macro asserts that the pointer is non-zero, and throws
  251. E_OUTOFMEMORY if it is not.
  252. Arguments:
  253. ptr - the pointer to test.
  254. See Also:
  255. WsbAffirmAlloc
  256. --*/
  257. #define WsbAssertAlloc( ptr ) \
  258. { \
  259. WsbAssert( (ptr) != 0, E_OUTOFMEMORY );\
  260. }
  261. /*++
  262. Macro Name:
  263. WsbAffirmStatus
  264. Macro Description:
  265. Similar to WsbAffirm(), but is used to wrap Win32 functions
  266. that return a BOOL status.
  267. This macro checks the status, and if FALSE, gets the
  268. last error and converts it to HRESULT, then affirms
  269. the result.
  270. Arguments:
  271. status - a BOOL result from a function call.
  272. See Also:
  273. WsbAssertStatus
  274. --*/
  275. #define WsbAffirmStatus(status) \
  276. { \
  277. BOOL bStatus; \
  278. bStatus = (status); \
  279. if (!bStatus) { \
  280. DWORD dwErr = GetLastError(); \
  281. HRESULT lHr = HRESULT_FROM_WIN32(dwErr); \
  282. WsbAffirm(SUCCEEDED(lHr), lHr); \
  283. } \
  284. }
  285. /*++
  286. Macro Name:
  287. WsbAffirmWin32
  288. Macro Description:
  289. Similar to WsbAssert(), but is used to wrap Win32 functions
  290. that return a Win32 status.
  291. This macro checks the status, and if not ERROR_SUCCESS,
  292. converts it to HRESULT, then asserts the result.
  293. Arguments:
  294. status - a Win32 result from a function call.
  295. See Also:
  296. WsbAffirmStatus
  297. --*/
  298. #define WsbAffirmWin32( status ) \
  299. { \
  300. LONG lStatus; \
  301. lStatus = (status); \
  302. if ( lStatus != ERROR_SUCCESS ) { \
  303. HRESULT lHr = HRESULT_FROM_WIN32( lStatus ); \
  304. WsbAffirm(SUCCEEDED(lHr), lHr); \
  305. } \
  306. }
  307. /*++
  308. Macro Name:
  309. WsbAffirmNtStatus
  310. Macro Description:
  311. Similar to WsbAffirm(), but is used to wrap NT System functions
  312. that return a NTSTATUS result.
  313. This macro checks the status, and if not successful, gets the
  314. last error and converts it to HRESULT, then affirms
  315. the result.
  316. Arguments:
  317. status - a NTSTATUS result from a function call.
  318. See Also:
  319. WsbAffirmStatus
  320. --*/
  321. #define WsbAffirmNtStatus(status) \
  322. { \
  323. NTSTATUS _ntStatus; \
  324. _ntStatus = (NTSTATUS)( status ); \
  325. if ( !NT_SUCCESS( _ntStatus ) ) { \
  326. HRESULT _lHr = HRESULT_FROM_NT( _ntStatus ); \
  327. WsbAffirm(SUCCEEDED(_lHr), _lHr); \
  328. } \
  329. }
  330. /*++
  331. Macro Name:
  332. WsbAssertNtStatus
  333. Macro Description:
  334. Similar to WsbAssert(), but is used to wrap NT System functions
  335. that return a NTSTATUS result.
  336. This macro checks the status, and if not successful, gets the
  337. last error and converts it to HRESULT, then affirms
  338. the result.
  339. Arguments:
  340. status - a NTSTATUS result from a function call.
  341. See Also:
  342. WsbAssertStatus
  343. --*/
  344. #define WsbAssertNtStatus(status) \
  345. { \
  346. NTSTATUS _ntStatus; \
  347. _ntStatus = (NTSTATUS)( status ); \
  348. if ( !NT_SUCCESS( _ntStatus ) ) { \
  349. HRESULT _lHr = HRESULT_FROM_NT( _ntStatus ); \
  350. WsbAssert(SUCCEEDED(_lHr), _lHr); \
  351. } \
  352. }
  353. /*++
  354. Macro Name:
  355. WsbAffirmLsaStatus
  356. Macro Description:
  357. Similar to WsbAffirm(), but is used to wrap NT System functions
  358. that return a NTSTATUS result.
  359. This macro checks the status, and if not successful, gets the
  360. last error and converts it to HRESULT, then affirms
  361. the result.
  362. Arguments:
  363. status - a NTSTATUS result from a function call.
  364. See Also:
  365. WsbAffirmStatus
  366. --*/
  367. #define WsbAffirmLsaStatus(status) \
  368. { \
  369. NTSTATUS _ntStatus; \
  370. _ntStatus = (NTSTATUS)( status ); \
  371. if ( !NT_SUCCESS( _ntStatus ) ) { \
  372. HRESULT _lHr = HRESULT_FROM_WIN32( LsaNtStatusToWinError(_ntStatus) ); \
  373. WsbAffirm(SUCCEEDED(_lHr), _lHr); \
  374. } \
  375. }
  376. /*++
  377. Macro Name:
  378. WsbAffirmNoError
  379. Macro Description:
  380. Similar to WsbAffirm(), but is used to wrap Win32 functions
  381. that return a DWORD error code. These functions return NO_ERROR
  382. if the function completed successfully.
  383. This macro checks the return value and if an error condition
  384. is detected, the error is converted to an HRESULT, then affirms
  385. the result.
  386. Arguments:
  387. err - a DWORD result from a function call.
  388. See Also:
  389. WsbAssertNoError
  390. --*/
  391. #define WsbAffirmNoError(retval) \
  392. { \
  393. DWORD dwErr; \
  394. dwErr = (retval); \
  395. if (dwErr != NO_ERROR) { \
  396. HRESULT lHr = HRESULT_FROM_WIN32(dwErr); \
  397. WsbAffirm(SUCCEEDED(lHr), lHr); \
  398. } \
  399. }
  400. /*++
  401. Macro Name:
  402. WsbAffirmHandle
  403. Macro Description:
  404. Similar to WsbAffirm(), but is used to wrap Win32 functions
  405. that return a HANDLE.
  406. This macro checks the handle and if it is invalid, gets the
  407. last error, converts it to an HRESULT, then affirms the result.
  408. Arguments:
  409. hndl - a HANDLE result from a function call.
  410. See Also:
  411. WsbAssertHandle
  412. --*/
  413. #define WsbAffirmHandle(hndl) \
  414. { \
  415. HANDLE hHndl; \
  416. hHndl = (hndl); \
  417. if ((hHndl == INVALID_HANDLE_VALUE) || (hHndl == NULL)) { \
  418. DWORD dwErr = GetLastError(); \
  419. HRESULT lHr = HRESULT_FROM_WIN32(dwErr); \
  420. WsbAffirm(SUCCEEDED(lHr), lHr); \
  421. } \
  422. }
  423. /*++
  424. Macro Name:
  425. WsbAffirmPointer
  426. Macro Description:
  427. Similar to WsbAffirm(), but is used specifically to check for
  428. a valid pointer.
  429. This macro affrims that the pointer is non-zero, and returns
  430. E_POINTER if it is not.
  431. Arguments:
  432. ptr - the pointer to test.
  433. See Also:
  434. WsbAssertPointer
  435. --*/
  436. #define WsbAffirmPointer( ptr ) \
  437. { \
  438. WsbAffirm( ptr != 0, E_POINTER);\
  439. }
  440. /*++
  441. Macro Name:
  442. WsbAffirmAlloc
  443. Macro Description:
  444. Similar to WsbAffirm(), but is used specifically to check for
  445. a valid memory allocation.
  446. This macro affrims that the pointer is non-zero, and returns
  447. E_OUTOFMEMORY if it is not.
  448. Arguments:
  449. ptr - the pointer to test.
  450. See Also:
  451. WsbAssertAlloc
  452. --*/
  453. #define WsbAffirmAlloc( ptr ) \
  454. { \
  455. WsbAffirm( (ptr) != 0, E_OUTOFMEMORY );\
  456. }
  457. /*++
  458. Macro Name:
  459. WsbCatchAndDo
  460. Macro Description:
  461. Catch an exception and execute some code.
  462. Arguments:
  463. hr - The result value that was thrown.
  464. code - Code to execute.
  465. --*/
  466. #define WsbCatchAndDo(hr, code) \
  467. catch (HRESULT catchHr) { \
  468. hr = catchHr; \
  469. { code } \
  470. }
  471. /*++
  472. Macro Name:
  473. WsbCatch
  474. Macro Description:
  475. Catch an exception and save the error code value.
  476. Arguments:
  477. hr - The result value that was thrown.
  478. --*/
  479. #define WsbCatch(hr) \
  480. catch(HRESULT catchHr) { \
  481. hr = catchHr; \
  482. }
  483. #endif // _WSBASSRT_