Factory.cpp
上传用户:biuytresa
上传日期:2007-12-07
资源大小:721k
文件大小:2k
源码类别:

DNA

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "factory.h"
  3. #include "CompB.h"
  4. extern ULONG    g_LockNumber;
  5. extern ULONG    g_CompBNumber;
  6. CBFactory::CBFactory()
  7. {
  8. m_Ref = 0;
  9. }
  10. CBFactory::~CBFactory()
  11. {
  12. }
  13. HRESULT  CBFactory::QueryInterface(const IID& iid, void **ppv)
  14. {
  15. if ( iid == IID_IUnknown )
  16. {
  17. *ppv = (IUnknown *) this ;
  18. ((IUnknown *)(*ppv))->AddRef();
  19. } else if ( iid == IID_IClassFactory)
  20. {
  21. *ppv = (IClassFactory *) this ;
  22. ((IClassFactory *)(*ppv))->AddRef() ;
  23. else
  24. {
  25. *ppv = NULL;
  26. return E_NOINTERFACE ;
  27. }
  28. return S_OK;
  29. }
  30. ULONG   CBFactory::AddRef()
  31. {
  32. m_Ref ++;
  33. return  (ULONG) m_Ref;
  34. }
  35. ULONG   CBFactory::Release()
  36. {
  37. m_Ref --;
  38. if (m_Ref == 0 ) {
  39. delete this;
  40. return 0;
  41. }
  42. return  (ULONG) m_Ref;
  43. }
  44. HRESULT CBFactory::CreateInstance(IUnknown *pUnknownOuter, 
  45.    const IID& iid, void **ppv)
  46. {
  47. CB  *pObj;   
  48. HRESULT hr;
  49.    
  50. *ppv=NULL;
  51. hr=E_OUTOFMEMORY;
  52. if (NULL != pUnknownOuter)
  53. return CLASS_E_NOAGGREGATION;
  54.    
  55. //Create the object passing function to notify on destruction.
  56. pObj=new CB ();
  57. if (NULL==pObj)
  58. return hr;   
  59.    
  60. pObj->AddRef();  // The Reference count of pObj is 1
  61. hr = pObj->Init();
  62. if (FAILED(hr) ) {
  63. g_CompBNumber --; // Reference count g_CompBNumber be added in constructor
  64. delete pObj;
  65. return E_FAIL;
  66. }
  67.    
  68. //Obtain the first interface pointer (which does an AddRef)
  69. hr=pObj->QueryInterface(iid, ppv);
  70. pObj->Release(); // The Reference count of pObj is 1
  71. return hr;
  72. }
  73. HRESULT CBFactory::LockServer(BOOL bLock)
  74. {
  75.    if (bLock)
  76.       g_LockNumber ++;
  77.    else
  78.       g_LockNumber --;
  79.    return NOERROR;
  80. }