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.

231 lines
3.6 KiB

  1. //
  2. // Copyright 2001 - Microsoft Corporation
  3. //
  4. // Created By:
  5. // Geoff Pease (GPease) 23-JAN-2001
  6. //
  7. // Maintained By:
  8. // Geoff Pease (GPease) 23-JAN-2001
  9. //
  10. //////////////////////////////////////////////////////////////////////////////
  11. #include "pch.h"
  12. #pragma hdrstop
  13. DEFINE_THISCLASS("CFactory")
  14. #define THISCLASS CFactory
  15. // ************************************************************************
  16. //
  17. // Constructor / Destructor
  18. //
  19. // ************************************************************************
  20. //
  21. // Constructor
  22. //
  23. CFactory::CFactory( void )
  24. {
  25. TraceFunc( "" );
  26. Assert( 0 == m_cRef );
  27. Assert( NULL == m_pfnCreateInstance );
  28. InterlockedIncrement( &g_cObjects );
  29. TraceFuncExit();
  30. }
  31. //
  32. //
  33. //
  34. HRESULT
  35. CFactory::Init(
  36. LPCREATEINST lpfnCreateIn
  37. )
  38. {
  39. TraceFunc( "" );
  40. // IUnknown stuff
  41. Assert( 0 == m_cRef );
  42. AddRef( );
  43. // IClassFactory
  44. m_pfnCreateInstance = lpfnCreateIn;
  45. HRETURN( S_OK );
  46. }
  47. //
  48. // Destructor
  49. //
  50. CFactory::~CFactory( void )
  51. {
  52. TraceFunc( "" );
  53. InterlockedDecrement( &g_cObjects );
  54. TraceFuncExit();
  55. }
  56. // ************************************************************************
  57. //
  58. // IUnknown
  59. //
  60. // ************************************************************************
  61. //
  62. //
  63. //
  64. STDMETHODIMP
  65. CFactory::QueryInterface(
  66. REFIID riid,
  67. LPVOID *ppv
  68. )
  69. {
  70. TraceQIFunc( riid, ppv );
  71. HRESULT hr = E_NOINTERFACE;
  72. if ( IsEqualIID( riid, IID_IUnknown ) )
  73. {
  74. //
  75. // Can't track IUnknown as they must be equal the same address
  76. // for every QI.
  77. //
  78. *ppv = static_cast<IClassFactory*>( this );
  79. hr = S_OK;
  80. }
  81. else if ( IsEqualIID( riid, IID_IClassFactory ) )
  82. {
  83. *ppv = TraceInterface( __THISCLASS__, IClassFactory, this, 0 );
  84. hr = S_OK;
  85. }
  86. if ( SUCCEEDED( hr ) )
  87. {
  88. ((IUnknown*) *ppv)->AddRef( );
  89. }
  90. QIRETURN( hr, riid );
  91. }
  92. //
  93. //
  94. //
  95. STDMETHODIMP_(ULONG)
  96. CFactory::AddRef( void )
  97. {
  98. TraceFunc( "[IUnknown]" );
  99. InterlockedIncrement( &m_cRef );
  100. RETURN( m_cRef );
  101. }
  102. //
  103. //
  104. //
  105. STDMETHODIMP_(ULONG)
  106. CFactory::Release( void )
  107. {
  108. TraceFunc( "[IUnknown]" );
  109. InterlockedDecrement( &m_cRef );
  110. if ( m_cRef )
  111. RETURN( m_cRef );
  112. TraceDo( delete this );
  113. RETURN(0);
  114. }
  115. // ************************************************************************
  116. //
  117. // IClassFactory
  118. //
  119. // ************************************************************************
  120. //
  121. //
  122. //
  123. STDMETHODIMP
  124. CFactory::CreateInstance(
  125. IUnknown *pUnkOuter,
  126. REFIID riid,
  127. void **ppv
  128. )
  129. {
  130. TraceFunc( "[IClassFactory]" );
  131. if ( !ppv )
  132. RRETURN(E_POINTER);
  133. *ppv = NULL;
  134. HRESULT hr = E_NOINTERFACE;
  135. IUnknown * pUnk = NULL;
  136. if ( NULL != pUnkOuter )
  137. {
  138. hr = THR(CLASS_E_NOAGGREGATION);
  139. goto Cleanup;
  140. }
  141. Assert( m_pfnCreateInstance != NULL );
  142. hr = THR( m_pfnCreateInstance( &pUnk ) );
  143. if ( FAILED( hr ) )
  144. goto Cleanup;
  145. // Can't safe type.
  146. TraceMsgDo( hr = pUnk->QueryInterface( riid, ppv ), "0x%08x" );
  147. Cleanup:
  148. if ( pUnk != NULL )
  149. {
  150. ULONG cRef;
  151. //
  152. // Release the created instance, not the punk
  153. //
  154. TraceMsgDo( cRef = ((IUnknown*) pUnk)->Release( ), "%u" );
  155. }
  156. HRETURN( hr );
  157. }
  158. //
  159. //
  160. //
  161. STDMETHODIMP
  162. CFactory::LockServer(
  163. BOOL fLock
  164. )
  165. {
  166. TraceFunc( "[IClassFactory]" );
  167. if ( fLock )
  168. {
  169. InterlockedIncrement( &g_cLock );
  170. }
  171. else
  172. {
  173. InterlockedDecrement( &g_cLock );
  174. }
  175. HRETURN( S_OK );
  176. }