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.

255 lines
6.4 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2002 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // DispatchHandler.h
  7. //
  8. // Description:
  9. // This file contains a template to use as a base class for an
  10. // implementation of an IDispatch-based interface.
  11. //
  12. // Documentation:
  13. //
  14. // Implementation Files:
  15. // None.
  16. //
  17. // Maintained By:
  18. // John Franco (jfranco) 17-APR-2002
  19. //
  20. //////////////////////////////////////////////////////////////////////////////
  21. // Make sure that this file is included only once per compile path.
  22. #pragma once
  23. //////////////////////////////////////////////////////////////////////////////
  24. // Include Files
  25. //////////////////////////////////////////////////////////////////////////////
  26. //////////////////////////////////////////////////////////////////////////////
  27. // Constant Declarations
  28. //////////////////////////////////////////////////////////////////////////////
  29. //////////////////////////////////////////////////////////////////////////////
  30. // Template Declarations
  31. //////////////////////////////////////////////////////////////////////////////
  32. //////////////////////////////////////////////////////////////////////////////
  33. //++
  34. //
  35. // template class TDispatchHandler
  36. //
  37. // Description:
  38. // TDispatchHandler provides a type library-based implementation of
  39. // IDispatch for use by classes that implement a dual interface.
  40. // Instructions for use:
  41. // - inherit from TDispatchHandler< t_Interface > instead of
  42. // directly from t_Interface
  43. // - call HrInit with the type library's GUID when initializing
  44. // each instance
  45. // - return static_cast< TDispatchHandler< t_Interface >* >( this )
  46. // in response to a QueryInterface request for IID_IDispatch.
  47. //
  48. // Template Arguments:
  49. // t_Interface
  50. // The dual interface implemented by the derived class.
  51. // Requirements for t_Interface:
  52. // - has dual attribute in IDL
  53. // - based on IDispatch
  54. // - included in type library
  55. //
  56. //--
  57. //////////////////////////////////////////////////////////////////////////////
  58. template < class t_Interface >
  59. class TDispatchHandler
  60. : public t_Interface
  61. {
  62. private:
  63. ITypeInfo * m_ptypeinfo;
  64. // Private copy constructor to prevent copying.
  65. TDispatchHandler( const TDispatchHandler & );
  66. // Private assignment operator to prevent copying.
  67. TDispatchHandler & operator=( const TDispatchHandler & );
  68. public:
  69. TDispatchHandler( void );
  70. virtual ~TDispatchHandler( void );
  71. virtual HRESULT HrInit( REFGUID rlibid );
  72. STDMETHOD( GetIDsOfNames )(
  73. REFIID riid
  74. , OLECHAR ** rgszNames
  75. , unsigned int cNames
  76. , LCID lcid
  77. , DISPID * rgDispId
  78. );
  79. STDMETHOD( GetTypeInfo )( unsigned int iTInfo, LCID lcid, ITypeInfo ** ppTInfo );
  80. STDMETHOD( GetTypeInfoCount )( unsigned int * pctinfo );
  81. STDMETHOD( Invoke )(
  82. DISPID dispIdMember
  83. , REFIID riid
  84. , LCID lcid
  85. , WORD wFlags
  86. , DISPPARAMS * pDispParams
  87. , VARIANT * pVarResult
  88. , EXCEPINFO * pExcepInfo
  89. , unsigned int * puArgErr
  90. );
  91. }; //*** template class TDispatchHandler
  92. template < class t_Interface >
  93. TDispatchHandler< t_Interface >::TDispatchHandler( void )
  94. : m_ptypeinfo( NULL )
  95. {
  96. } //*** TDispatchHandler< t_Interface >::TDispatchHandler
  97. template < class t_Interface >
  98. TDispatchHandler< t_Interface >::~TDispatchHandler( void )
  99. {
  100. if ( m_ptypeinfo != NULL )
  101. {
  102. m_ptypeinfo->Release();
  103. m_ptypeinfo = NULL;
  104. }
  105. } //*** TDispatchHandler< t_Interface >::~TDispatchHandler
  106. template < class t_Interface >
  107. HRESULT
  108. TDispatchHandler< t_Interface >::HrInit( REFGUID rlibidIn )
  109. {
  110. HRESULT hr = S_OK;
  111. ITypeLib* pitypelib = NULL;
  112. hr = LoadRegTypeLib(
  113. rlibidIn
  114. , 1 // major version number--must match that in IDL
  115. , 0 // minor version number--must match that in IDL
  116. , LOCALE_NEUTRAL
  117. , &pitypelib
  118. );
  119. if ( FAILED( hr ) )
  120. {
  121. goto Cleanup;
  122. }
  123. hr = pitypelib->GetTypeInfoOfGuid( __uuidof( t_Interface ), &m_ptypeinfo );
  124. if ( FAILED( hr ) )
  125. {
  126. goto Cleanup;
  127. }
  128. Cleanup:
  129. if ( pitypelib != NULL )
  130. {
  131. pitypelib->Release();
  132. }
  133. return hr;
  134. } //*** TDispatchHandler< t_Interface >::HrInit
  135. template < class t_Interface >
  136. STDMETHODIMP
  137. TDispatchHandler< t_Interface >::GetIDsOfNames(
  138. REFIID riid
  139. , OLECHAR ** rgszNames
  140. , unsigned int cNames
  141. , LCID lcid
  142. , DISPID * rgDispId
  143. )
  144. {
  145. return m_ptypeinfo->GetIDsOfNames( rgszNames, cNames, rgDispId );
  146. } //*** TDispatchHandler< t_Interface >::GetIDsOfNames
  147. template < class t_Interface >
  148. STDMETHODIMP
  149. TDispatchHandler< t_Interface >::GetTypeInfo(
  150. unsigned int iTInfo
  151. , LCID lcid
  152. , ITypeInfo ** ppTInfo
  153. )
  154. {
  155. HRESULT hr = S_OK;
  156. if ( ppTInfo == NULL )
  157. {
  158. hr = E_POINTER;
  159. goto Cleanup;
  160. }
  161. *ppTInfo = NULL;
  162. if ( iTInfo > 0 )
  163. {
  164. hr = TYPE_E_ELEMENTNOTFOUND;
  165. goto Cleanup;
  166. }
  167. m_ptypeinfo->AddRef();
  168. *ppTInfo = m_ptypeinfo;
  169. Cleanup:
  170. return hr;
  171. } //*** TDispatchHandler< t_Interface >::GetTypeInfo
  172. template < class t_Interface >
  173. STDMETHODIMP
  174. TDispatchHandler< t_Interface >::GetTypeInfoCount(
  175. unsigned int * pctinfo
  176. )
  177. {
  178. HRESULT hr = S_OK;
  179. if ( pctinfo == NULL )
  180. {
  181. hr = THR( E_POINTER );
  182. goto Cleanup;
  183. }
  184. *pctinfo = 1;
  185. Cleanup:
  186. return hr;
  187. } //*** TDispatchHandler< t_Interface >::GetTypeInfoCount
  188. template < class t_Interface >
  189. STDMETHODIMP
  190. TDispatchHandler< t_Interface >::Invoke(
  191. DISPID dispIdMember
  192. , REFIID riid
  193. , LCID lcid
  194. , WORD wFlags
  195. , DISPPARAMS * pDispParams
  196. , VARIANT * pVarResult
  197. , EXCEPINFO * pExcepInfo
  198. , unsigned int * puArgErr
  199. )
  200. {
  201. return m_ptypeinfo->Invoke( this, dispIdMember, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr );
  202. } //*** TDispatchHandler< t_Interface >::Invoke