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. {
  86. const CLSID * clsID;
  87. const WCHAR * strName;
  88. DWORD dwMerit;
  89. UINT nPins;
  90. const AMOVIESETUP_PIN * lpPin;
  91. }
  92. AMOVIESETUP_FILTER, * PAMOVIESETUP_FILTER, * FAR LPAMOVIESETUP_FILTER;
  93. /* The DLLENTRY module initialises the module handle on loading */
  94. extern HINSTANCE g_hInst;
  95. /* On DLL load remember which platform we are running on */
  96. extern DWORD g_amPlatform;
  97. extern OSVERSIONINFO g_osInfo; // Filled in by GetVersionEx
  98. /* Version of IUnknown that is renamed to allow a class to support both
  99. non delegating and delegating IUnknowns in the same COM object */
  100. #ifndef INONDELEGATINGUNKNOWN_DEFINED
  101. DECLARE_INTERFACE(INonDelegatingUnknown)
  102. {
  103. STDMETHOD(NonDelegatingQueryInterface) (THIS_ REFIID, LPVOID *) PURE;
  104. STDMETHOD_(ULONG, NonDelegatingAddRef)(THIS) PURE;
  105. STDMETHOD_(ULONG, NonDelegatingRelease)(THIS) PURE;
  106. };
  107. #define INONDELEGATINGUNKNOWN_DEFINED
  108. #endif
  109. typedef INonDelegatingUnknown *PNDUNKNOWN;
  110. /* This is the base object class that supports active object counting. As
  111. part of the debug facilities we trace every time a C++ object is created
  112. or destroyed. The name of the object has to be passed up through the class
  113. derivation list during construction as you cannot call virtual functions
  114. in the constructor. The downside of all this is that every single object
  115. constructor has to take an object name parameter that describes it */
  116. class CBaseObject
  117. {
  118. private:
  119. // Disable the copy constructor and assignment by default so you will get
  120. // compiler errors instead of unexpected behaviour if you pass objects
  121. // by value or assign objects.
  122. CBaseObject(const CBaseObject& objectSrc); // no implementation
  123. void operator=(const CBaseObject& objectSrc); // no implementation
  124. private:
  125. static LONG m_cObjects; /* Total number of objects active */
  126. protected:
  127. #ifdef DEBUG
  128. DWORD m_dwCookie; /* Cookie identifying this object */
  129. #endif
  130. public:
  131. /* These increment and decrement the number of active objects */
  132. CBaseObject(__in_opt LPCTSTR pName);
  133. #ifdef UNICODE
  134. CBaseObject(__in_opt LPCSTR pName);
  135. #endif
  136. ~CBaseObject();
  137. /* Call this to find if there are any CUnknown derived objects active */
  138. static LONG ObjectsActive() {
  139. return m_cObjects;
  140. };
  141. };
  142. /* An object that supports one or more COM interfaces will be based on
  143. this class. It supports counting of total objects for DLLCanUnloadNow
  144. support, and an implementation of the core non delegating IUnknown */
  145. class AM_NOVTABLE CUnknown : public INonDelegatingUnknown,
  146. public CBaseObject
  147. {
  148. private:
  149. const LPUNKNOWN m_pUnknown; /* Owner of this object */
  150. protected: /* So we can override NonDelegatingRelease() */
  151. volatile LONG m_cRef; /* Number of reference counts */
  152. public:
  153. CUnknown(__in_opt LPCTSTR pName, __in_opt LPUNKNOWN pUnk);
  154. virtual ~CUnknown() {};
  155. // This is redundant, just use the other constructor
  156. // as we never touch the HRESULT in this anyway
  157. CUnknown(__in_opt LPCTSTR Name, __in_opt LPUNKNOWN pUnk, __inout_opt HRESULT *phr);
  158. #ifdef UNICODE
  159. CUnknown(__in_opt LPCSTR pName, __in_opt LPUNKNOWN pUnk);
  160. CUnknown(__in_opt LPCSTR pName, __in_opt LPUNKNOWN pUnk,__inout_opt HRESULT *phr);
  161. #endif
  162. /* Return the owner of this object */
  163. LPUNKNOWN GetOwner() const {
  164. return m_pUnknown;
  165. };
  166. /* Called from the class factory to create a new instance, it is
  167. pure virtual so it must be overriden in your derived class */
  168. /* static CUnknown *CreateInstance(LPUNKNOWN, HRESULT *) */
  169. /* Non delegating unknown implementation */
  170. STDMETHODIMP NonDelegatingQueryInterface(REFIID, __deref_out void **);
  171. STDMETHODIMP_(ULONG) NonDelegatingAddRef();
  172. STDMETHODIMP_(ULONG) NonDelegatingRelease();
  173. };
  174. /* Return an interface pointer to a requesting client
  175. performing a thread safe AddRef as necessary */
  176. STDAPI GetInterface(LPUNKNOWN pUnk, __out void **ppv);
  177. /* A function that can create a new COM object */
  178. typedef CUnknown *(CALLBACK *LPFNNewCOMObject)(__in_opt LPUNKNOWN pUnkOuter, __inout_opt HRESULT *phr);
  179. /* A function (can be NULL) which is called from the DLL entrypoint
  180. routine for each factory template:
  181. bLoading - TRUE on DLL load, FALSE on DLL unload
  182. rclsid - the m_ClsID of the entry
  183. */
  184. typedef void (CALLBACK *LPFNInitRoutine)(BOOL bLoading, const CLSID *rclsid);
  185. /* Create one of these per object class in an array so that
  186. the default class factory code can create new instances */
  187. class CFactoryTemplate {
  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__ */