combase.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. //------------------------------------------------------------------------------
  2. // File: ComBase.h
  3. //
  4. // Desc: DirectShow base classes - defines a class hierarchy for creating
  5. // COM objects.
  6. //
  7. // Copyright (c) 1992-2001 Microsoft Corporation. All rights reserved.
  8. //------------------------------------------------------------------------------
  9. /*
  10. a. Derive your COM object from CUnknown
  11. b. Make a static CreateInstance function that takes an LPUNKNOWN, an HRESULT *
  12. and a TCHAR *. The LPUNKNOWN defines the object to delegate IUnknown calls
  13. to. The HRESULT * allows error codes to be passed around constructors and
  14. the TCHAR * is a descriptive name that can be printed on the debugger.
  15. It is important that constructors only change the HRESULT * if they have
  16. to set an ERROR code, if it was successful then leave it alone or you may
  17. overwrite an error code from an object previously created.
  18. When you call a constructor the descriptive name should be in static store
  19. as we do not copy the string. To stop large amounts of memory being used
  20. in retail builds by all these static strings use the NAME macro,
  21. CMyFilter = new CImplFilter(NAME("My filter"),pUnknown,phr);
  22. if (FAILED(hr)) {
  23. return hr;
  24. }
  25. In retail builds NAME(_x_) compiles to NULL, the base CBaseObject class
  26. knows not to do anything with objects that don't have a name.
  27. c. Have a constructor for your object that passes the LPUNKNOWN, HRESULT * and
  28. TCHAR * to the CUnknown constructor. You can set the HRESULT if you have an
  29. error, or just simply pass it through to the constructor.
  30. The object creation will fail in the class factory if the HRESULT indicates
  31. an error (ie FAILED(HRESULT) == TRUE)
  32. d. Create a FactoryTemplate with your object's class id and CreateInstance
  33. function.
  34. Then (for each interface) either
  35. Multiple inheritance
  36. 1. Also derive it from ISomeInterface
  37. 2. Include DECLARE_IUNKNOWN in your class definition to declare
  38. implementations of QueryInterface, AddRef and Release that
  39. call the outer unknown
  40. 3. Override NonDelegatingQueryInterface to expose ISomeInterface by
  41. code something like
  42. if (riid == IID_ISomeInterface) {
  43. return GetInterface((ISomeInterface *) this, ppv);
  44. } else {
  45. return CUnknown::NonDelegatingQueryInterface(riid, ppv);
  46. }
  47. 4. Declare and implement the member functions of ISomeInterface.
  48. or: Nested interfaces
  49. 1. Declare a class derived from CUnknown
  50. 2. Include DECLARE_IUNKNOWN in your class definition
  51. 3. Override NonDelegatingQueryInterface to expose ISomeInterface by
  52. code something like
  53. if (riid == IID_ISomeInterface) {
  54. return GetInterface((ISomeInterface *) this, ppv);
  55. } else {
  56. return CUnknown::NonDelegatingQueryInterface(riid, ppv);
  57. }
  58. 4. Implement the member functions of ISomeInterface. Use GetOwner() to
  59. access the COM object class.
  60. And in your COM object class:
  61. 5. Make the nested class a friend of the COM object class, and declare
  62. an instance of the nested class as a member of the COM object class.
  63. NOTE that because you must always pass the outer unknown and an hResult
  64. to the CUnknown constructor you cannot use a default constructor, in
  65. other words you will have to make the member variable a pointer to the
  66. class and make a NEW call in your constructor to actually create it.
  67. 6. override the NonDelegatingQueryInterface with code like this:
  68. if (riid == IID_ISomeInterface) {
  69. return m_pImplFilter->
  70. NonDelegatingQueryInterface(IID_ISomeInterface, ppv);
  71. } else {
  72. return CUnknown::NonDelegatingQueryInterface(riid, ppv);
  73. }
  74. You can have mixed classes which support some interfaces via multiple
  75. inheritance and some via nested classes
  76. */
  77. #ifndef __COMBASE__
  78. #define __COMBASE__
  79. // Filter Setup data structures no defined in axextend.idl
  80. typedef REGPINTYPES
  81. AMOVIESETUP_MEDIATYPE, * PAMOVIESETUP_MEDIATYPE, * FAR LPAMOVIESETUP_MEDIATYPE;
  82. typedef REGFILTERPINS
  83. AMOVIESETUP_PIN, * PAMOVIESETUP_PIN, * FAR LPAMOVIESETUP_PIN;
  84. typedef struct _AMOVIESETUP_FILTER {
  85. const CLSID * clsID;
  86. const WCHAR * strName;
  87. DWORD dwMerit;
  88. UINT nPins;
  89. const AMOVIESETUP_PIN * lpPin;
  90. }
  91. AMOVIESETUP_FILTER, * PAMOVIESETUP_FILTER, * FAR LPAMOVIESETUP_FILTER;
  92. /* The DLLENTRY module initialises the module handle on loading */
  93. extern HINSTANCE g_hInst;
  94. /* On DLL load remember which platform we are running on */
  95. extern DWORD g_amPlatform;
  96. extern OSVERSIONINFO g_osInfo; // Filled in by GetVersionEx
  97. /* Version of IUnknown that is renamed to allow a class to support both
  98. non delegating and delegating IUnknowns in the same COM object */
  99. #ifndef INONDELEGATINGUNKNOWN_DEFINED
  100. DECLARE_INTERFACE(INonDelegatingUnknown)
  101. {
  102. STDMETHOD(NonDelegatingQueryInterface) (THIS_ REFIID, LPVOID *) PURE;
  103. STDMETHOD_(ULONG, NonDelegatingAddRef)(THIS) PURE;
  104. STDMETHOD_(ULONG, NonDelegatingRelease)(THIS) PURE;
  105. };
  106. #define INONDELEGATINGUNKNOWN_DEFINED
  107. #endif
  108. typedef INonDelegatingUnknown *PNDUNKNOWN;
  109. /* This is the base object class that supports active object counting. As
  110. part of the debug facilities we trace every time a C++ object is created
  111. or destroyed. The name of the object has to be passed up through the class
  112. derivation list during construction as you cannot call virtual functions
  113. in the constructor. The downside of all this is that every single object
  114. constructor has to take an object name parameter that describes it */
  115. class CBaseObject
  116. {
  117. private:
  118. // Disable the copy constructor and assignment by default so you will get
  119. // compiler errors instead of unexpected behaviour if you pass objects
  120. // by value or assign objects.
  121. CBaseObject(const CBaseObject& objectSrc); // no implementation
  122. void operator=(const CBaseObject& objectSrc); // no implementation
  123. private:
  124. static LONG m_cObjects; /* Total number of objects active */
  125. protected:
  126. #ifdef DEBUG
  127. DWORD m_dwCookie; /* Cookie identifying this object */
  128. #endif
  129. public:
  130. /* These increment and decrement the number of active objects */
  131. CBaseObject(__in_opt LPCTSTR pName);
  132. #ifdef UNICODE
  133. CBaseObject(__in_opt LPCSTR pName);
  134. #endif
  135. ~CBaseObject();
  136. /* Call this to find if there are any CUnknown derived objects active */
  137. static LONG ObjectsActive() {
  138. return m_cObjects;
  139. };
  140. };
  141. /* An object that supports one or more COM interfaces will be based on
  142. this class. It supports counting of total objects for DLLCanUnloadNow
  143. support, and an implementation of the core non delegating IUnknown */
  144. class AM_NOVTABLE CUnknown : public INonDelegatingUnknown,
  145. public CBaseObject
  146. {
  147. private:
  148. const LPUNKNOWN m_pUnknown; /* Owner of this object */
  149. protected: /* So we can override NonDelegatingRelease() */
  150. volatile LONG m_cRef; /* Number of reference counts */
  151. public:
  152. CUnknown(__in_opt LPCTSTR pName, __in_opt LPUNKNOWN pUnk);
  153. virtual ~CUnknown() {};
  154. // This is redundant, just use the other constructor
  155. // as we never touch the HRESULT in this anyway
  156. CUnknown(__in_opt LPCTSTR Name, __in_opt LPUNKNOWN pUnk, __inout_opt HRESULT *phr);
  157. #ifdef UNICODE
  158. CUnknown(__in_opt LPCSTR pName, __in_opt LPUNKNOWN pUnk);
  159. CUnknown(__in_opt LPCSTR pName, __in_opt LPUNKNOWN pUnk,__inout_opt HRESULT *phr);
  160. #endif
  161. /* Return the owner of this object */
  162. LPUNKNOWN GetOwner() const {
  163. return m_pUnknown;
  164. };
  165. /* Called from the class factory to create a new instance, it is
  166. pure virtual so it must be overriden in your derived class */
  167. /* static CUnknown *CreateInstance(LPUNKNOWN, HRESULT *) */
  168. /* Non delegating unknown implementation */
  169. STDMETHODIMP NonDelegatingQueryInterface(REFIID, __deref_out void **);
  170. STDMETHODIMP_(ULONG) NonDelegatingAddRef();
  171. STDMETHODIMP_(ULONG) NonDelegatingRelease();
  172. };
  173. /* Return an interface pointer to a requesting client
  174. performing a thread safe AddRef as necessary */
  175. STDAPI GetInterface(LPUNKNOWN pUnk, __out void **ppv);
  176. /* A function that can create a new COM object */
  177. typedef CUnknown *(CALLBACK *LPFNNewCOMObject)(__in_opt LPUNKNOWN pUnkOuter, __inout_opt HRESULT *phr);
  178. /* A function (can be NULL) which is called from the DLL entrypoint
  179. routine for each factory template:
  180. bLoading - TRUE on DLL load, FALSE on DLL unload
  181. rclsid - the m_ClsID of the entry
  182. */
  183. typedef void (CALLBACK *LPFNInitRoutine)(BOOL bLoading, const CLSID *rclsid);
  184. /* Create one of these per object class in an array so that
  185. the default class factory code can create new instances */
  186. class CFactoryTemplate
  187. {
  188. public:
  189. const WCHAR * m_Name;
  190. const CLSID * m_ClsID;
  191. LPFNNewCOMObject m_lpfnNew;
  192. LPFNInitRoutine m_lpfnInit;
  193. const AMOVIESETUP_FILTER * m_pAMovieSetup_Filter;
  194. BOOL IsClassID(REFCLSID rclsid) const {
  195. return (IsEqualCLSID(*m_ClsID,rclsid));
  196. };
  197. CUnknown *CreateInstance(__inout_opt LPUNKNOWN pUnk, __inout_opt HRESULT *phr) const {
  198. CheckPointer(phr,NULL);
  199. return m_lpfnNew(pUnk, phr);
  200. };
  201. };
  202. /* You must override the (pure virtual) NonDelegatingQueryInterface to return
  203. interface pointers (using GetInterface) to the interfaces your derived
  204. class supports (the default implementation only supports IUnknown) */
  205. #define DECLARE_IUNKNOWN \
  206. STDMETHODIMP QueryInterface(REFIID riid, __deref_out void **ppv) { \
  207. return GetOwner()->QueryInterface(riid,ppv); \
  208. }; \
  209. STDMETHODIMP_(ULONG) AddRef() { \
  210. return GetOwner()->AddRef(); \
  211. }; \
  212. STDMETHODIMP_(ULONG) Release() { \
  213. return GetOwner()->Release(); \
  214. };
  215. HINSTANCE LoadOLEAut32();
  216. #endif /* __COMBASE__ */