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.

230 lines
3.9 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. Assert( 0 != g_cObjects );
  54. InterlockedDecrement( &g_cObjects );
  55. TraceFuncExit();
  56. }
  57. // ************************************************************************
  58. //
  59. // IUnknown
  60. //
  61. // ************************************************************************
  62. //
  63. //
  64. //
  65. STDMETHODIMP
  66. CFactory::QueryInterface(
  67. REFIID riid,
  68. LPVOID *ppv
  69. )
  70. {
  71. TraceQIFunc( riid, ppv );
  72. HRESULT hr = E_NOINTERFACE;
  73. if ( IsEqualIID( riid, IID_IUnknown ) )
  74. {
  75. //
  76. // Can't track IUnknown as they must be equal the same address
  77. // for every QI.
  78. //
  79. *ppv = static_cast<IClassFactory*>( this );
  80. hr = S_OK;
  81. }
  82. else if ( IsEqualIID( riid, IID_IClassFactory ) )
  83. {
  84. *ppv = TraceInterface( __THISCLASS__, IClassFactory, this, 0 );
  85. hr = S_OK;
  86. }
  87. if ( SUCCEEDED( hr ) )
  88. {
  89. ((IUnknown*) *ppv)->AddRef( );
  90. }
  91. QIRETURN( hr, riid );
  92. }
  93. //
  94. //
  95. //
  96. STDMETHODIMP_(ULONG)
  97. CFactory::AddRef( void )
  98. {
  99. TraceFunc( "[IUnknown]" );
  100. ULONG cRef = InterlockedIncrement( &m_cRef );
  101. RETURN( cRef );
  102. }
  103. //
  104. //
  105. //
  106. STDMETHODIMP_(ULONG)
  107. CFactory::Release( void )
  108. {
  109. TraceFunc( "[IUnknown]" );
  110. Assert( 0 != m_cRef );
  111. ULONG cRef = InterlockedDecrement( &m_cRef );
  112. if ( 0 == cRef )
  113. {
  114. TraceDo( delete this );
  115. }
  116. RETURN(cRef);
  117. }
  118. // ************************************************************************
  119. //
  120. // IClassFactory
  121. //
  122. // ************************************************************************
  123. //
  124. //
  125. //
  126. STDMETHODIMP
  127. CFactory::CreateInstance(
  128. IUnknown *pUnkOuter,
  129. REFIID riid,
  130. void **ppv
  131. )
  132. {
  133. TraceFunc( "[IClassFactory]" );
  134. if ( !ppv )
  135. RRETURN(E_POINTER);
  136. *ppv = NULL;
  137. HRESULT hr = E_NOINTERFACE;
  138. IUnknown * pUnk = NULL;
  139. if ( NULL != pUnkOuter )
  140. {
  141. hr = THR(CLASS_E_NOAGGREGATION);
  142. goto Cleanup;
  143. }
  144. Assert( m_pfnCreateInstance != NULL );
  145. hr = THR( m_pfnCreateInstance( &pUnk ) );
  146. if ( FAILED( hr ) )
  147. goto Cleanup;
  148. // Can't safe type.
  149. TraceMsgDo( hr = pUnk->QueryInterface( riid, ppv ), "0x%08x" );
  150. Cleanup:
  151. if ( pUnk != NULL )
  152. {
  153. ULONG cRef;
  154. //
  155. // Release the created instance, not the punk
  156. //
  157. TraceMsgDo( cRef = ((IUnknown*) pUnk)->Release( ), "%u" );
  158. }
  159. HRETURN( hr );
  160. }
  161. //
  162. //
  163. //
  164. STDMETHODIMP
  165. CFactory::LockServer(
  166. BOOL fLock
  167. )
  168. {
  169. TraceFunc( "[IClassFactory]" );
  170. if ( fLock )
  171. {
  172. InterlockedIncrement( &g_cLock );
  173. }
  174. else
  175. {
  176. Assert( 0 != g_cLock );
  177. InterlockedDecrement( &g_cLock );
  178. }
  179. HRETURN( S_OK );
  180. }