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

DNA

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "factory.h"
  3. #include "dictionary.h"
  4. extern ULONG    g_LockNumber;
  5. extern ULONG    g_DictionaryNumber;
  6. extern DWORD dwMainThreadID;
  7. extern "C" const GUID CLSID_Dictionary; 
  8. CDictionaryFactory::CDictionaryFactory()
  9. {
  10. m_Ref = 0;
  11. }
  12. CDictionaryFactory::~CDictionaryFactory()
  13. {
  14. }
  15. HRESULT  CDictionaryFactory::QueryInterface(const IID& iid, void **ppv)
  16. {
  17. if ( iid == IID_IUnknown )
  18. {
  19. *ppv = (IUnknown *) this ;
  20. ((IUnknown *)(*ppv))->AddRef() ;
  21. } else if ( iid == IID_IClassFactory) 
  22. {
  23. *ppv = (IClassFactory *) this ;
  24. ((IClassFactory *)(*ppv))->AddRef() ;
  25. else
  26. {
  27. *ppv = NULL;
  28. return E_NOINTERFACE ;
  29. }
  30. return S_OK;
  31. }
  32. ULONG   CDictionaryFactory::AddRef()
  33. {
  34. m_Ref ++;
  35. return  (ULONG) m_Ref;
  36. }
  37. ULONG   CDictionaryFactory::Release()
  38. {
  39. m_Ref --;
  40. if (m_Ref == 0 ) {
  41. delete this;
  42. return 0;
  43. }
  44. return  (ULONG) m_Ref;
  45. }
  46. HRESULT CDictionaryFactory::CreateInstance(IUnknown *pUnknownOuter, 
  47.    const IID& iid, void **ppv)
  48. {
  49.    CDictionary * pObj;   
  50.    HRESULT hr;
  51.    
  52.    *ppv=NULL;
  53.    hr=E_OUTOFMEMORY;
  54.    if (NULL != pUnknownOuter)
  55.    return CLASS_E_NOAGGREGATION;
  56.    
  57.    //Create the object passing function to notify on destruction.
  58.    pObj=new CDictionary();
  59.    if (NULL==pObj)
  60.       return hr;   
  61.    
  62.    //Obtain the first interface pointer (which does an AddRef)
  63.    hr=pObj->QueryInterface(iid, ppv);
  64.    if (hr != S_OK) {
  65.    //Kill the object if initial creation or FInit failed.
  66.       g_DictionaryNumber --; // Reference count g_cDictionary be added in constructor
  67.   delete pObj;
  68.    }
  69.    
  70.    return hr;   
  71. }
  72. HRESULT CDictionaryFactory::LockServer(BOOL bLock)
  73. {
  74. if (bLock)
  75. g_LockNumber ++;
  76. else
  77. {
  78. g_LockNumber --;
  79. if (CanUnloadNow()) {
  80. ::PostThreadMessage(dwMainThreadID, WM_QUIT, 0, 0) ;
  81. }
  82. }
  83. return NOERROR;
  84. }
  85. BOOL CDictionaryFactory::CanUnloadNow()
  86. {
  87. if (g_LockNumber>0 || g_DictionaryNumber >0)
  88. return FALSE;
  89. else 
  90. return TRUE;
  91. }
  92. CDictionaryFactory *CDictionaryFactory::theFactory = NULL;
  93. DWORD CDictionaryFactory::dwRegister = 0;
  94. //
  95. // Register factory
  96. //
  97. BOOL CDictionaryFactory::RegisterFactory()
  98. {
  99. // Create the class factory for dictionary component.
  100. theFactory = new CDictionaryFactory();
  101. theFactory->AddRef();
  102. IUnknown *pUnkForFactory = (IUnknown  *)theFactory;
  103. // Register the class factory.
  104. HRESULT hr = ::CoRegisterClassObject(
  105.               CLSID_Dictionary,
  106.               pUnkForFactory,
  107.               CLSCTX_LOCAL_SERVER,
  108.               REGCLS_MULTIPLEUSE,
  109.               // REGCLS_MULTI_SEPARATE, //@Multi
  110.               &dwRegister) ;
  111. if (FAILED(hr))
  112. {
  113. theFactory->Release() ;
  114. return FALSE ;
  115. }
  116. return TRUE ;
  117. }
  118. //
  119. // Unregister factories
  120. //
  121. void CDictionaryFactory::UnregisterFactory()
  122. {
  123. if (dwRegister != 0) 
  124. {
  125. ::CoRevokeClassObject(dwRegister) ;
  126. }
  127. // Release the class factory.
  128. if (theFactory != NULL)
  129. theFactory->Release();
  130. }