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.

185 lines
5.9 KiB

  1. //==========================================================================;
  2. //
  3. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  4. // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  5. // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  6. // PURPOSE.
  7. //
  8. // Copyright (c) 1992 - 1998 Microsoft Corporation. All Rights Reserved.
  9. //
  10. //--------------------------------------------------------------------------;
  11. // Base class hierachy for creating COM objects, December 1994
  12. #include "dmocom.h"
  13. #pragma warning( disable : 4514 ) // Disable warnings re unused inline functions
  14. /* Constructor */
  15. // We know we use "this" in the initialization list, we also know we don't modify *phr.
  16. #pragma warning( disable : 4355 4100 )
  17. CComBase::CComBase(IUnknown* pUnk)
  18. :
  19. /* Start the object with a reference count of zero - when the */
  20. /* object is queried for it's first interface this may be */
  21. /* incremented depending on whether or not this object is */
  22. /* currently being aggregated upon */
  23. m_cRef(0)
  24. /* Set our pointer to our IUnknown interface. */
  25. /* If we have an outer, use its, otherwise use ours. */
  26. /* This pointer effectivly points to the owner of */
  27. /* this object and can be accessed by the GetOwner() method. */
  28. , m_pUnknown( pUnk != 0 ? pUnk : reinterpret_cast<IUnknown*>( static_cast<INDUnknown*>(this) ) )
  29. /* Why the double cast? Well, the inner cast is a type-safe cast */
  30. /* to pointer to a type from which we inherit. The second is */
  31. /* type-unsafe but works because INDUnknown "behaves */
  32. /* like" IUnknown. (Only the names on the methods change.) */
  33. {
  34. // Everything we need to do has been done in the initializer list
  35. }
  36. // This does the same as above except it has a useless HRESULT argument
  37. // use the previous constructor, this is just left for compatibility...
  38. CComBase::CComBase(IUnknown* pUnk,HRESULT *phr) :
  39. m_cRef(0),
  40. m_pUnknown( pUnk != 0 ? pUnk : reinterpret_cast<IUnknown*>( static_cast<INDUnknown*>(this) ) )
  41. {
  42. }
  43. #pragma warning( default : 4355 4100 )
  44. /* QueryInterface */
  45. STDMETHODIMP CComBase::NDQueryInterface(REFIID riid, void ** ppv)
  46. {
  47. CheckPointer(ppv,E_POINTER);
  48. ValidateReadWritePtr(ppv,sizeof(PVOID));
  49. /* We know only about IUnknown */
  50. if (riid == IID_IUnknown) {
  51. GetInterface((IUnknown*) (INDUnknown*) this, ppv);
  52. return NOERROR;
  53. } else {
  54. *ppv = NULL;
  55. return E_NOINTERFACE;
  56. }
  57. }
  58. /* We have to ensure that we DON'T use a max macro, since these will typically */
  59. /* lead to one of the parameters being evaluated twice. Since we are worried */
  60. /* about concurrency, we can't afford to access the m_cRef twice since we can't */
  61. /* afford to run the risk that its value having changed between accesses. */
  62. #ifdef max
  63. #undef max
  64. #endif
  65. template<class T> inline static T max( const T & a, const T & b )
  66. {
  67. return a > b ? a : b;
  68. }
  69. /* AddRef */
  70. STDMETHODIMP_(ULONG) CComBase::NDAddRef()
  71. {
  72. LONG lRef = InterlockedIncrement( &m_cRef );
  73. ASSERT(lRef > 0);
  74. /*
  75. DbgLog((LOG_MEMORY,3,TEXT(" Obj %d ref++ = %d"),
  76. m_dwCookie, m_cRef));
  77. */
  78. return max(ULONG(m_cRef), 1ul);
  79. }
  80. /* Release */
  81. STDMETHODIMP_(ULONG) CComBase::NDRelease()
  82. {
  83. /* If the reference count drops to zero delete ourselves */
  84. LONG lRef = InterlockedDecrement( &m_cRef );
  85. ASSERT(lRef >= 0);
  86. /*
  87. DbgLog((LOG_MEMORY,3,TEXT(" Object %d ref-- = %d"),
  88. m_dwCookie, m_cRef));
  89. */
  90. if (lRef == 0) {
  91. // COM rules say we must protect against re-entrancy.
  92. // If we are an aggregator and we hold our own interfaces
  93. // on the aggregatee, the QI for these interfaces will
  94. // addref ourselves. So after doing the QI we must release
  95. // a ref count on ourselves. Then, before releasing the
  96. // private interface, we must addref ourselves. When we do
  97. // this from the destructor here it will result in the ref
  98. // count going to 1 and then back to 0 causing us to
  99. // re-enter the destructor. Hence we add an extra refcount here
  100. // once we know we will delete the object.
  101. // for an example aggregator see filgraph\distrib.cpp.
  102. m_cRef++;
  103. delete this;
  104. return ULONG(0);
  105. } else {
  106. return max(ULONG(m_cRef), 1ul);
  107. }
  108. }
  109. /* Return an interface pointer to a requesting client
  110. performing a thread safe AddRef as necessary */
  111. STDAPI GetInterface(IUnknown* pUnk, void **ppv)
  112. {
  113. CheckPointer(ppv, E_POINTER);
  114. *ppv = pUnk;
  115. pUnk->AddRef();
  116. return NOERROR;
  117. }
  118. /* Compares two interfaces and returns TRUE if they are on the same object */
  119. BOOL WINAPI IsEqualObject(IUnknown *pFirst, IUnknown *pSecond)
  120. {
  121. /* Different objects can't have the same interface pointer for
  122. any interface
  123. */
  124. if (pFirst == pSecond) {
  125. return TRUE;
  126. }
  127. /* OK - do it the hard way - check if they have the same
  128. IUnknown pointers - a single object can only have one of these
  129. */
  130. IUnknown* pUnknown1; // Retrieve the IUnknown interface
  131. IUnknown* pUnknown2; // Retrieve the other IUnknown interface
  132. HRESULT hr; // General OLE return code
  133. ASSERT(pFirst);
  134. ASSERT(pSecond);
  135. /* See if the IUnknown pointers match */
  136. hr = pFirst->QueryInterface(IID_IUnknown,(void **) &pUnknown1);
  137. ASSERT(SUCCEEDED(hr));
  138. ASSERT(pUnknown1);
  139. hr = pSecond->QueryInterface(IID_IUnknown,(void **) &pUnknown2);
  140. ASSERT(SUCCEEDED(hr));
  141. ASSERT(pUnknown2);
  142. /* Release the extra interfaces we hold */
  143. pUnknown1->Release();
  144. pUnknown2->Release();
  145. return (pUnknown1 == pUnknown2);
  146. }