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.

239 lines
5.3 KiB

  1. // Copyright (c) 1995 - 1997 Microsoft Corporation. All Rights Reserved.
  2. //
  3. // comint.h
  4. //
  5. #ifndef __CCOMINT__
  6. #define __CCOMINT__
  7. #pragma warning( disable : 4290 ) // warning C4290: C++ Exception Specification ignored
  8. // CCOMInt
  9. //
  10. // A class to manage COM interfaces - AddRef's in its constructor
  11. // and releases in the destructor.
  12. // It is intended to provide the same automatic releasing that CAutoLock
  13. // provides for critical sections.
  14. //
  15. // Provides an overloaded dereferenceing operator for calling interface members.
  16. //
  17. // a typical use would be:
  18. // CCOMInt<IMediaControl> IMC(IID_IMediaContol, punk);
  19. //
  20. // IMC->Run();
  21. //
  22. // I don't think this class is useful as a _complete_ replacement of
  23. // interface pointers, for two reasons:
  24. // - using heap allocated objects (if you want a lifetime longer than the scope)
  25. // has some ugly syntax:
  26. // CCOMInt<IPersist> *pIPer = new CCOMInt<IPersist>(IID_IPersist, pSomeInt);
  27. //
  28. // (*pIPer)->GetClassID() // YUK!
  29. //
  30. // In this context I'm not sure how useful the CoCreate'ing constructor is...
  31. //
  32. // - constructing these objects from functions that give you addref'd interfcaces
  33. // is inconvienent
  34. //
  35. // IStream *pStrm;
  36. // hr = pStorage->CreateStream(, , , &pStrm);
  37. // if (FAILED(hr)) {
  38. // // do something appropriate
  39. // }
  40. // CCOMInt<IStream> IStrm(pStrm);
  41. // pStrm->Release();
  42. //
  43. // [...use IStrm...]
  44. //
  45. //
  46. // If you want to use this you need to add the following to you compiler
  47. // options:
  48. //
  49. // you must #define: _INC_EXCPT
  50. // and add the switch: -GX
  51. //
  52. // NB: this file uses the exception classes defined in hrExcept.h
  53. //
  54. // CCOMInt
  55. //
  56. template<class I>
  57. class CCOMInt {
  58. public:
  59. // -- Constructors --
  60. // CoCreate
  61. CCOMInt<I>( REFIID riid // get this interface
  62. , REFCLSID rclsid // get the interface
  63. // from this object
  64. , LPUNKNOWN pUnkOuter = NULL // controlling unknown
  65. , DWORD dwClsContext = CLSCTX_INPROC_SERVER // CoCreate options
  66. // default is suitable
  67. // for dll servers
  68. )
  69. throw (CHRESULTException) {
  70. #ifdef NOCOMLITE
  71. HRESULT hr = CoCreateInstance( rclsid
  72. , pUnkOuter
  73. , dwClsContext
  74. , riid
  75. , (void **) &m_pInt
  76. );
  77. #else
  78. HRESULT hr = QzCreateFilterObject( rclsid
  79. , pUnkOuter
  80. , dwClsContext
  81. , riid
  82. , (void **) &m_pInt
  83. );
  84. #endif
  85. if (FAILED(hr)) {
  86. throw CHRESULTException(hr);
  87. }
  88. }
  89. // QueryInterface
  90. CCOMInt<I>( REFIID riid // get this interface
  91. , IUnknown *punk // from this interface
  92. )
  93. throw (CHRESULTException) {
  94. HRESULT hr = punk->QueryInterface(riid, (void **) &m_pInt);
  95. if (FAILED(hr)) {
  96. throw CHRESULTException(hr);
  97. }
  98. }
  99. // copy
  100. CCOMInt<I>(const CCOMInt<I> &com)
  101. throw () {
  102. // #ifdef USE_MSVC20
  103. m_pInt = com;
  104. // #else
  105. // m_pInt = const_cast<I*>(com); // new ANSI C++ in VC40
  106. // #endif
  107. (*this)->AddRef();
  108. }
  109. // existing pointer.
  110. CCOMInt<I>(I *pInt)
  111. throw (CHRESULTException) {
  112. if (pInt == NULL) {
  113. throw CHRESULTException(E_NOINTERFACE);
  114. }
  115. m_pInt = pInt;
  116. (*this)->AddRef();
  117. }
  118. // assignment operator
  119. CCOMInt<I>& operator = (const CCOMInt<I> &com)
  120. throw () {
  121. if (this != &com) { // not i = i
  122. (*this)->Release();
  123. // #ifdef USE_MSVC20
  124. m_pInt = com;
  125. // #else
  126. // m_pInt = const_cast<I*>(com); // new ANSI C++ in VC40
  127. // #endif
  128. (*this)->AddRef();
  129. }
  130. return *this;
  131. }
  132. // destructor
  133. ~CCOMInt<I>()
  134. throw () {
  135. m_pInt->Release();
  136. }
  137. // -- comparison operators --
  138. BOOL operator == (IUnknown *punk)
  139. #ifdef USE_MSVC20
  140. throw(CHRESULTException) const
  141. #else
  142. const throw (CHRESULTException)
  143. #endif
  144. {
  145. CCOMInt<IUnknown> IUnk1(IID_IUnknown, punk);
  146. CCOMInt<IUnknown> IUnk2(IID_IUnknown, *this);
  147. return ( ((IUnknown *)IUnk1) == ((IUnknown *)IUnk2) );
  148. }
  149. BOOL operator != (IUnknown *punk)
  150. #ifdef USE_MSVC20
  151. throw(CHRESULTException) const
  152. #else
  153. const throw (CHRESULTException)
  154. #endif
  155. {
  156. return !(*this == punk);
  157. }
  158. // cast to interface pointer
  159. operator I *()
  160. #ifdef USE_MSVC20
  161. throw() const
  162. #else
  163. const throw ()
  164. #endif
  165. {
  166. return m_pInt;
  167. }
  168. // dereference
  169. I *operator->()
  170. #ifdef USE_MSVC20
  171. throw() const
  172. #else
  173. const throw ()
  174. #endif
  175. {
  176. return m_pInt;
  177. }
  178. I &operator*()
  179. #ifdef USE_MSVC20
  180. throw() const
  181. #else
  182. const throw ()
  183. #endif
  184. {
  185. return *m_pInt;
  186. }
  187. private:
  188. I *m_pInt;
  189. // array dereferencing seems to make no sense.
  190. I &operator[] (int i)
  191. #ifdef USE_MSVC20
  192. throw(CHRESULTException) const
  193. #else
  194. const throw (CHRESULTException)
  195. #endif
  196. {
  197. throw CHRESULTException();
  198. return *m_pInt;
  199. }
  200. };
  201. #endif // __CCOMINT__