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.

205 lines
3.5 KiB

  1. // Enter and Leave macros for all externally called functions. Debug version does not
  2. // have try/catch block to make gp faults easier to track down.
  3. //
  4. // Examples:
  5. //
  6. // // This is the standard way to wrap APIs.
  7. // HRESULT API1(...)
  8. // {
  9. // ENTER_API
  10. // {
  11. //
  12. // // API code here.
  13. //
  14. // }
  15. // LEAVE_API
  16. // }
  17. //
  18. // // This is how you would do it if you need more control.
  19. // HRESULT API2(...)
  20. // {
  21. // STD_MANAGE_STATE
  22. //
  23. // try
  24. // {
  25. //
  26. // // Your code here.
  27. //
  28. // }
  29. // catch (CSpecialException *pexcept)
  30. // {
  31. // // This is how you would catch an exception that required
  32. // // special processing (beyond just returning an hresult).
  33. // }
  34. //
  35. // // This is the default way to return E_OutOfMemory for CMemoryException
  36. // DefCatchMemory
  37. //
  38. // // This is required to catch all other exceptions and return E_FAIL
  39. // DefCatchAll
  40. // }
  41. #if defined(AFX_MANAGE_STATE)
  42. // Note that we must manage MFC .dll state around each of our public
  43. // entry points via this call to AFX_MANAGE_STATE(AfxGetStaticModuleState())
  44. #define STD_MANAGE_STATE AFX_MANAGE_STATE(AfxGetStaticModuleState());
  45. #else
  46. #define STD_MANAGE_STATE
  47. #endif
  48. #if 0
  49. #define DefCatchMemory \
  50. catch (CMemoryException *pe) \
  51. { \
  52. pe->Delete(); \
  53. return E_OUTOFMEMORY; \
  54. }
  55. #define DefCatchOleException \
  56. catch (COleException *pe) \
  57. { \
  58. HRESULT hr = pe->m_sc; \
  59. pe->Delete(); \
  60. return hr; \
  61. }
  62. #define DefCatchOleDispatchException \
  63. catch (COleDispatchException *pe) \
  64. { \
  65. HRESULT hr = pe->m_scError; \
  66. pe->Delete(); \
  67. return hr; \
  68. }
  69. #endif
  70. #define DefCatchHRESULT \
  71. catch (HRESULT hr) \
  72. { \
  73. return hr; \
  74. }
  75. #define DefCatchComError \
  76. catch (_com_error ce) \
  77. { \
  78. return ce.Error(); \
  79. }
  80. #define DefCatchAll \
  81. catch (...) \
  82. { \
  83. return E_FAIL; \
  84. }
  85. #if 0
  86. #define StdCatchMost \
  87. DefCatchHRESULT \
  88. DefCatchComError \
  89. DefCatchMemory \
  90. DefCatchOleException \
  91. DefCatchOleDispatchException
  92. #else
  93. #define StdCatchMost \
  94. DefCatchHRESULT \
  95. DefCatchComError
  96. #endif
  97. // Don't do DefCatchAll in _DEBUG so unexpected exceptions will fault.
  98. #ifdef _DEBUG
  99. #define StdCatchAll StdCatchMost
  100. #else
  101. #define StdCatchAll \
  102. StdCatchMost \
  103. DefCatchAll
  104. #endif
  105. #define ENTER_API \
  106. STD_MANAGE_STATE \
  107. try
  108. #define LEAVE_API \
  109. StdCatchAll \
  110. return ERROR_SUCCESS;
  111. template <typename X>
  112. inline void ValidateInPtr(X *px)
  113. {
  114. if ((px == NULL) || IsBadReadPtr(px, sizeof(X)))
  115. _com_issue_error(E_POINTER);
  116. }
  117. template <typename X>
  118. inline void ValidateInPtr_NULL_OK(X *px)
  119. {
  120. if (px != NULL)
  121. ValidateInPtr<X>(px);
  122. }
  123. template <typename X>
  124. inline void _ValidateOut(X *px)
  125. {
  126. ValidateInPtr<X>(px);
  127. if (IsBadWritePtr(px, sizeof(X)))
  128. _com_issue_error(E_POINTER);
  129. }
  130. template <typename X>
  131. inline void ValidateOut(X *px)
  132. {
  133. _ValidateOut<X>(px);
  134. }
  135. template <typename X>
  136. inline void ValidateOut(X *px, X x)
  137. {
  138. _ValidateOut<X>(px);
  139. *px = x;
  140. }
  141. template <typename X>
  142. inline void ValidateOutPtr(X **ppx)
  143. {
  144. ValidateOut<X *>(ppx);
  145. *ppx = NULL;
  146. }
  147. template <typename X>
  148. inline void ValidateOutPtr(X **ppx, X * px)
  149. {
  150. ValidateOut<X *>(ppx);
  151. *ppx = px;
  152. }
  153. inline void ValidateOut(VARIANT *pvar)
  154. {
  155. _ValidateOut<VARIANT>(pvar);
  156. VariantClear(pvar);
  157. }
  158. inline void ValidateIn(BSTR bstr)
  159. {
  160. if ((bstr != NULL) && IsBadStringPtrW(bstr, -1))
  161. _com_issue_error(E_POINTER);
  162. }
  163. inline void ValidateOut(BSTR *pbstr)
  164. {
  165. _ValidateOut<BSTR>(pbstr);
  166. #if 0
  167. if (*pbstr != NULL)
  168. {
  169. ValidateIn(*pbstr);
  170. SysFreeString(*pbstr);
  171. *pbstr = NULL;
  172. }
  173. #endif
  174. }