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.

198 lines
5.9 KiB

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