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.

310 lines
10 KiB

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