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.

218 lines
6.6 KiB

  1. //--------------------------------------------------------------------
  2. // ErrorHandling - header
  3. // Copyright (C) Microsoft Corporation, 2001
  4. //
  5. // Created by: Duncan Bryce (duncanb), 11-11-2001
  6. //
  7. // Macro definitions for CertSrv style error handling
  8. //
  9. #ifndef ERROR_HANDLING_H
  10. #define ERROR_HANDLING_H
  11. #include <memory>
  12. #include <exception>
  13. using namespace std;
  14. //----------------------------------------------------------------------
  15. // Easy wrapper for FormatMessage()
  16. //----------------------------------------------------------------------
  17. HRESULT GetSystemErrorString(HRESULT hrIn, WCHAR ** pwszError);
  18. //----------------------------------------------------------------------
  19. // Exception handling translation code.
  20. // Translates SEH to C++-style exceptions.
  21. //----------------------------------------------------------------------
  22. class SeException : public std::exception
  23. {
  24. public:
  25. SeException(unsigned int code) : m_code(code) { }
  26. unsigned int getSECode() { return m_code; }
  27. private:
  28. unsigned int m_code;
  29. };
  30. void __cdecl SeTransFunc(unsigned int u, EXCEPTION_POINTERS* pExp);
  31. //----------------------------------------------------------------------
  32. // C-style error-handling routines.
  33. //----------------------------------------------------------------------
  34. #ifdef DBG
  35. #define _MyAssert(expression) \
  36. {\
  37. if (!(expression)) { \
  38. DebugWPrintf1(L"*** Assert failed: '%s' is false.\n", L## #expression); \
  39. DebugBreak(); \
  40. }\
  41. }
  42. #else //DBG
  43. #define _MyAssert(expression)
  44. #endif //DBG
  45. #define _Verify(expression, hr, label) \
  46. {\
  47. if (!(expression)) { \
  48. DebugWPrintf1(L"Verify failed: '%s' is false.\n", L## #expression); \
  49. hr=E_UNEXPECTED; \
  50. goto label; \
  51. }\
  52. }
  53. #define _IgnoreError(hr, errorsource) \
  54. DebugWPrintf1(L##errorsource L" failed with 0x%08X, ignored.\n", hr);
  55. #define _IgnoreErrorStr(hr, errorsource, wstr) \
  56. DebugWPrintf2(L##errorsource L"(%s) failed with 0x%08X, ignored.\n", wstr, hr);
  57. #define _IgnoreLastError(errorsource) \
  58. DebugWPrintf1(L##errorsource L" failed with 0x%08X, ignored.\n", HRESULT_FROM_WIN32(GetLastError()));
  59. #define _IgnoreIfError(hr, errorsource) \
  60. {\
  61. if (FAILED(hr)) { \
  62. DebugWPrintf1(L##errorsource L" failed with 0x%08X, ignored.\n", hr); \
  63. }\
  64. }
  65. #define _JumpError(hr, label, errorsource) \
  66. DebugWPrintf1(L##errorsource L" failed with 0x%08X.\n", hr); \
  67. goto label;
  68. #define _JumpErrorStr(hr, label, errorsource, wstr) \
  69. DebugWPrintf2(L##errorsource L"(%s) failed with 0x%08X.\n", wstr, hr); \
  70. goto label;
  71. #define _JumpLastError(hr, label, errorsource) \
  72. hr=HRESULT_FROM_WIN32(GetLastError()); \
  73. DebugWPrintf1(L##errorsource L" failed with 0x%08X.\n", hr); \
  74. goto label;
  75. #define _JumpLastErrorStr(hr, label, errorsource, wstr) \
  76. hr=HRESULT_FROM_WIN32(GetLastError()); \
  77. DebugWPrintf2(L##errorsource L"(%s) failed with 0x%08X.\n", wstr, hr); \
  78. goto label;
  79. #define _JumpIfError(hr, label, errorsource) \
  80. {\
  81. if (FAILED(hr)) { \
  82. DebugWPrintf1(L##errorsource L" failed with 0x%08X.\n", hr); \
  83. goto label; \
  84. }\
  85. }
  86. #define _JumpIfErrorStr(hr, label, errorsource, wstr) \
  87. {\
  88. if (FAILED(hr)) { \
  89. DebugWPrintf2(L##errorsource L"(%s) failed with 0x%08X.\n", wstr, hr); \
  90. goto label; \
  91. }\
  92. }
  93. #define _JumpIfOutOfMemory(hr, label, pointer) \
  94. {\
  95. if (NULL==(pointer)) { \
  96. hr=E_OUTOFMEMORY; \
  97. DebugWPrintf0(L"Out of memory ('" L## #pointer L"').\n"); \
  98. goto label; \
  99. }\
  100. }
  101. // Save the old se translator so we can restore it when we're done
  102. #define _BeginTryWith(hr) \
  103. { \
  104. _se_translator_function fnSeTranslatorOld = _set_se_translator(SeTransFunc); \
  105. hr=S_OK; \
  106. try
  107. #define _TrapException(hr) \
  108. catch (SeException see) { \
  109. hr = HRESULT_FROM_WIN32(see.getSECode()); \
  110. } \
  111. catch (std::bad_alloc bae) { \
  112. hr = E_OUTOFMEMORY; \
  113. } \
  114. catch (...) { \
  115. hr = E_UNEXPECTED; \
  116. } \
  117. _set_se_translator(fnSeTranslatorOld); \
  118. }
  119. #define _TeardownError(hr, hr2, errorsource) \
  120. {\
  121. if (FAILED(hr2)) { \
  122. DebugWPrintf1(L##errorsource L" failed with 0x%08X during teardown.\n", hr2); \
  123. if (!FAILED(hr)) { \
  124. hr=hr2; \
  125. } \
  126. }\
  127. }
  128. #define _SafeStlCall(func, hr, error, errorsource) \
  129. {\
  130. _BeginTryWith(hr) {\
  131. (func); \
  132. } _TrapException(hr); \
  133. if (FAILED(hr)) { \
  134. _JumpError(hr, error, errorsource); \
  135. } \
  136. }
  137. #define _AcquireResourceSharedOrFail(lock, bAcquiredResource, hr, error) \
  138. { \
  139. BOOLEAN bSuccess = FALSE; \
  140. HRESULT hr2 = myRtlAcquireResourceShared((lock), TRUE, &bSuccess); \
  141. if (FAILED(hr2)) { \
  142. hr = hr2; \
  143. _JumpError(hr, error, "myRtlAcquireResourceShared"); \
  144. } else if (!bSuccess) { \
  145. hr = HRESULT_FROM_WIN32(ERROR_INTERNAL_ERROR); \
  146. _JumpError(hr, error, "myRtlAcquireResourceShared: couldn't acquire resource"); \
  147. } \
  148. bAcquiredResource = true; \
  149. }
  150. #define _AcquireResourceExclusiveOrFail(lock, bAcquiredResource, hr, error) \
  151. { \
  152. BOOLEAN bSuccess = FALSE; \
  153. HRESULT hr2 = myRtlAcquireResourceExclusive((lock), TRUE, &bSuccess); \
  154. if (FAILED(hr2)) { \
  155. hr = hr2; \
  156. _JumpError(hr, error, "myRtlAcquireResourceShared"); \
  157. } else if (!bSuccess) { \
  158. hr = HRESULT_FROM_WIN32(ERROR_INTERNAL_ERROR); \
  159. _JumpError(hr, error, "myRtlAcquireResourceShared: couldn't acquire resource"); \
  160. } \
  161. bAcquiredResource = true; \
  162. }
  163. #define _ReleaseResource(lock, bAcquiredResource) \
  164. { \
  165. if (bAcquiredResource) { \
  166. HRESULT hr2 = myRtlReleaseResource(lock); \
  167. _IgnoreIfError(hr2, "myRtlReleaseResource"); \
  168. if (SUCCEEDED(hr2)) { \
  169. bAcquiredResource = false; \
  170. } \
  171. } \
  172. }
  173. #define _EnterCriticalSectionOrFail(lock, bAcquiredResource, hr, error) \
  174. { \
  175. hr = myEnterCriticalSection(lock); \
  176. _JumpIfError(hr, error, "myEnterCriticalSection"); \
  177. bAcquiredResource = true; \
  178. }
  179. #define _LeaveCriticalSection(lock, bAcquiredResource, hr) \
  180. { \
  181. if (bAcquiredResource) { \
  182. HRESULT hr2 = myLeaveCriticalSection(lock); \
  183. _TeardownError(hr, hr2, "myLeaveCriticalSection"); \
  184. } \
  185. }
  186. #endif ERROR_HANDLING_H