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.

256 lines
5.6 KiB

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