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.

210 lines
10 KiB

  1. // DocWrapExcept
  2. //
  3. // Classes that wrap interface pointers. All methods are passed through, but any
  4. // SEH-type exceptions are caught, logged, and result in an error.
  5. //
  6. // Note that COM interfaces are not supposed to throw exceptions - this code exists
  7. // as a robustness/preventative measure.
  8. //
  9. // This file in #included by DocWrapImpl.cpp. It exists as a separate file just to
  10. // avoid clutter - this code is not currently reused anywhere else.
  11. // Code below assumes the following have already beein defined (in DocWrapImpl.cpp):
  12. // class BasicDocTraitsAnchor
  13. // class BasicDocTraitsACP
  14. #include <tStr.h>
  15. void HandleException( DWORD dwException, LPCTSTR pFName )
  16. {
  17. TraceError( TSTR() << TEXT("Exception 0x") << WriteHex( dwException ) << TEXT(" thrown from ") << pFName );
  18. }
  19. // If we want to use the IMETHOD(name) call tracking macro here, we need to disable warning 4509,
  20. // which warns against mixing object that have destructors (used by IMETHOD) and SEH.
  21. //
  22. // #pragma warning( disable : 4509 )
  23. // Forwarding macro:
  24. // Calls through the interface - assumes a member variable pThis of the correct type.
  25. // Exceptions are logged (in HandleException), and a failure code returned.
  26. #define EXCEPT_FORWARD( fname, c, params ) /**/ \
  27. HRESULT STDMETHODCALLTYPE fname AS_DECL( c, params ) \
  28. {\
  29. __try {\
  30. return m_pThis-> fname AS_CALL( c, params ) ;\
  31. }\
  32. __except( HandleException( GetExceptionCode(), TEXT( # fname ) ), EXCEPTION_EXECUTE_HANDLER ) {\
  33. return E_FAIL;\
  34. }\
  35. }
  36. // This class acts as a 'holder' for the class that actually does the forwarding.
  37. // Template parameter I is the original interface being wrapped, template parameter
  38. // W is the wrapping/forwarding interface.
  39. // This class just takes care of basic 'smart pointer' housekeeping - assignment,
  40. // operator->, etc.
  41. //
  42. // Assumes that the wrapper class will have an accessible member pThis of type I*.
  43. template < class I, class W >
  44. class SEHWrapPtr
  45. {
  46. W m_SEHWrap;
  47. public:
  48. SEHWrapPtr( I * pThis )
  49. {
  50. m_SEHWrap.m_pThis = pThis;
  51. }
  52. operator I * ( )
  53. {
  54. return m_SEHWrap.m_pThis;
  55. }
  56. W * operator -> ()
  57. {
  58. return & m_SEHWrap;
  59. }
  60. operator & ()
  61. {
  62. return & m_SEHWrap.m_pThis;
  63. }
  64. void operator = ( I * pI )
  65. {
  66. m_SEHWrap.m_pThis = pI;
  67. }
  68. };
  69. // SEH Wrapper for IUnknown. Other SEH wrappers that wrap COM interfaces derive from this.
  70. template < class I >
  71. struct SEHWrap_IUnknown
  72. {
  73. public:
  74. I * m_pThis;
  75. EXCEPT_FORWARD( AddRef, 0, ( ) )
  76. EXCEPT_FORWARD( Release, 0, ( ) )
  77. EXCEPT_FORWARD( QueryInterface, 2, ( REFIID, riid, void **, ppv ) )
  78. };
  79. // This wrapper wraps methods which are common to ITextStoreACP and ITextStoreAnchor.
  80. // Derived classes add in the ACP- and Anchor- specific methods.
  81. template < class DocTraits >
  82. struct SEHWrap_TextStoreBase : public SEHWrap_IUnknown < DocTraits::IDoc >
  83. {
  84. public:
  85. EXCEPT_FORWARD( AdviseSink, 3, ( REFIID, riid, IUnknown *, punk, DWORD, dwMask ) )
  86. EXCEPT_FORWARD( UnadviseSink, 1, ( IUnknown *, punk ) )
  87. EXCEPT_FORWARD( RequestLock, 2, ( DWORD, dwLockFlags, HRESULT *, phrSession ) )
  88. EXCEPT_FORWARD( GetStatus, 1, ( TS_STATUS *, pdcs ) )
  89. EXCEPT_FORWARD( QueryInsert, 5, ( DocTraits::PosType, InsertStart,
  90. DocTraits::PosType, InsertEnd,
  91. ULONG, cch,
  92. DocTraits::PosType *, ppaInsertStart,
  93. DocTraits::PosType *, ppaInsertEnd ) )
  94. EXCEPT_FORWARD( QueryInsertEmbedded, 3, ( const GUID *, pguidService,
  95. const FORMATETC *, pFormatEtc,
  96. BOOL *, pfInsertable ) )
  97. EXCEPT_FORWARD( GetScreenExt, 2, ( TsViewCookie, vcView,
  98. RECT *, prc ) )
  99. EXCEPT_FORWARD( GetWnd, 2, ( TsViewCookie, vcView,
  100. HWND *, phwnd ) )
  101. EXCEPT_FORWARD( GetFormattedText, 3, ( DocTraits::PosType, Start,
  102. DocTraits::PosType, End,
  103. IDataObject **, ppDataObject ) )
  104. EXCEPT_FORWARD( GetTextExt, 5, ( TsViewCookie, vcView,
  105. DocTraits::PosType, Start,
  106. DocTraits::PosType, End,
  107. RECT *, prc,
  108. BOOL *, pfClipped ) )
  109. EXCEPT_FORWARD( RequestSupportedAttrs, 3, ( DWORD, dwFlags,
  110. ULONG, cFilterAttrs,
  111. const TS_ATTRID *, paFilterAttrs ) )
  112. EXCEPT_FORWARD( RequestAttrsAtPosition, 4, ( DocTraits::PosType, Pos,
  113. ULONG, cFilterAttrs,
  114. const TS_ATTRID *, paFilterAttrs,
  115. DWORD, dwFlags ) )
  116. EXCEPT_FORWARD( RequestAttrsTransitioningAtPosition,
  117. 4, ( DocTraits::PosType, Pos,
  118. ULONG, cFilterAttrs,
  119. const TS_ATTRID *, paFilterAttrs,
  120. DWORD, dwFlags ) )
  121. EXCEPT_FORWARD( RetrieveRequestedAttrs, 3, ( ULONG, ulCount,
  122. TS_ATTRVAL *, paAttrVals,
  123. ULONG *, pcFetched ) )
  124. EXCEPT_FORWARD( GetActiveView, 1, ( TsViewCookie *, pvcView ) )
  125. /*
  126. EXCEPT_FORWARDEXT( ScrollToRect, 4, ( DocTraits::PosType, Start,
  127. DocTraits::PosType, End,
  128. RECT, rc,
  129. DWORD, dwPosition ) )
  130. */
  131. };
  132. struct SEHWrap_TextStoreACP : public SEHWrap_TextStoreBase< BasicDocTraitsACP >
  133. {
  134. EXCEPT_FORWARD( GetSelection, 4, ( ULONG, ulIndex, ULONG, ulCount, TS_SELECTION_ACP *, pSelection, ULONG *, pcFetched ) )
  135. EXCEPT_FORWARD( GetText, 9, ( LONG, acpStart, LONG, acpEnd, WCHAR *, pchPlain, ULONG, cchPlainReq, ULONG *, pcchPlainRet, TS_RUNINFO *, prgRunInfo, ULONG, cRunInfoReq, ULONG *, pcRunInfoRet, LONG *, pacpNext ) )
  136. EXCEPT_FORWARD( GetEmbedded, 4, ( LONG, Pos, REFGUID, rguidService, REFIID, riid, IUnknown **, ppunk ) )
  137. EXCEPT_FORWARD( GetEndACP, 1, ( LONG *, pacp ) )
  138. EXCEPT_FORWARD( GetACPFromPoint, 4, ( TsViewCookie, vcView, const POINT *, ptScreen, DWORD, dwFlags, LONG *, pacp ) )
  139. EXCEPT_FORWARD( FindNextAttrTransition, 8, ( LONG, acpStart, LONG, acpEnd, ULONG, cFilterAttrs, const TS_ATTRID *, paFilterAttrs, DWORD, dwFlags, LONG *, pacpNext, BOOL *, pfFound, LONG *, plFoundOffset ) )
  140. EXCEPT_FORWARD( SetSelection, 2, ( ULONG, ulCount, const TS_SELECTION_ACP *, pSelection ) )
  141. EXCEPT_FORWARD( SetText, 6, ( DWORD, dwFlags, LONG, acpStart, LONG, acpEnd, const WCHAR *, pchText, ULONG, cch, TS_TEXTCHANGE *, pChange ) )
  142. EXCEPT_FORWARD( InsertEmbedded, 5, ( DWORD, dwFlags, LONG, acpStart, LONG, acpEnd, IDataObject *, pDataObject, TS_TEXTCHANGE *, pChange ) )
  143. EXCEPT_FORWARD( InsertTextAtSelection, 6, ( DWORD, dwFlags, const WCHAR *, pchText, ULONG, cch, LONG *, pacpStart, LONG *, pacpEnd, TS_TEXTCHANGE *, pChange ) )
  144. EXCEPT_FORWARD( InsertEmbeddedAtSelection, 5, ( DWORD, dwFlags, IDataObject *, pDataObject, LONG *, pacpStart, LONG *, pacpEnd, TS_TEXTCHANGE *, pChange ) )
  145. };
  146. struct SEHWrap_TextStoreAnchor : public SEHWrap_TextStoreBase< BasicDocTraitsAnchor >
  147. {
  148. EXCEPT_FORWARD( GetSelection, 4, ( ULONG, ulIndex, ULONG, ulCount, TS_SELECTION_ANCHOR *, pSelection, ULONG *, pcFetched ) )
  149. EXCEPT_FORWARD( GetText, 7, ( DWORD, dwFlags, IAnchor *, paStart, IAnchor *, paEnd, WCHAR *, pchText, ULONG, cchReq, ULONG *, pcch, BOOL, fUpdateAnchor ) )
  150. EXCEPT_FORWARD( GetEmbedded, 5, ( DWORD, dwFlags, IAnchor *, Pos, REFGUID, rguidService, REFIID, riid, IUnknown **, ppunk ) )
  151. EXCEPT_FORWARD( GetStart, 1, ( IAnchor **, ppaStart ) )
  152. EXCEPT_FORWARD( GetEnd, 1, ( IAnchor **, ppaEnd ) )
  153. EXCEPT_FORWARD( GetAnchorFromPoint, 4, ( TsViewCookie, vcView, const POINT *, ptScreen, DWORD, dwFlags, IAnchor **, ppaSite ) )
  154. EXCEPT_FORWARD( FindNextAttrTransition, 7, ( IAnchor *, paStart, IAnchor *, paEnd, ULONG, cFilterAttrs, const TS_ATTRID *, paFilterAttrs, DWORD, dwFlags, BOOL *, pfFound, LONG *, plFoundOffset ) )
  155. EXCEPT_FORWARD( SetSelection, 2, ( ULONG, ulCount, const TS_SELECTION_ANCHOR *, pSelection ) )
  156. EXCEPT_FORWARD( SetText, 5, ( DWORD, dwFlags, IAnchor *, paStart, IAnchor *, paEnd, const WCHAR *, pchText, ULONG, cch ) )
  157. EXCEPT_FORWARD( InsertEmbedded, 4, ( DWORD, dwFlags, IAnchor *, paStart, IAnchor *, paEnd, IDataObject *, pDataObject ) )
  158. EXCEPT_FORWARD( InsertTextAtSelection, 5, ( DWORD, dwFlags, const WCHAR *, pchText, ULONG, cch, IAnchor **, ppaStart, IAnchor **, ppaEnd ) )
  159. EXCEPT_FORWARD( InsertEmbeddedAtSelection, 4, ( DWORD, dwFlags, IDataObject *, pDataObject, IAnchor **, ppaStart, IAnchor **, ppaEnd ) )
  160. };
  161. // Finally, put the wrapper classes together with the holder classes to create 'smart pointer'-like
  162. // SEH wrappers:
  163. typedef SEHWrapPtr< ITextStoreACP, SEHWrap_TextStoreACP > SEHWrapPtr_TextStoreACP;
  164. typedef SEHWrapPtr< ITextStoreAnchor, SEHWrap_TextStoreAnchor > SEHWrapPtr_TextStoreAnchor;