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.

217 lines
5.0 KiB

  1. /*****************************************************************************
  2. *
  3. * migfact.cpp - IClassFactory interface
  4. *
  5. *****************************************************************************/
  6. // includes
  7. #include <iostream.h>
  8. #include <objbase.h>
  9. #include <shlwapi.h>
  10. #include <shlwapip.h>
  11. #include <shlobj.h>
  12. #include "cowsite.h"
  13. #include "Iface.h" // Interface declarations
  14. #include "Registry.h" // Registry helper functions
  15. #include "migutil.h"
  16. #include "migeng.h"
  17. #include "migtask.h"
  18. #include "migoobe.h"
  19. /*****************************************************************************/
  20. // macros
  21. #define SAFECAST(_obj, _type) (((_type)(_obj)==(_obj)?0:0), (_type)(_obj))
  22. /*****************************************************************************/
  23. // extern methods
  24. STDAPI DllAddRef();
  25. STDAPI DllRelease();
  26. /*****************************************************************************/
  27. // function prototypesb
  28. HRESULT CMigWizEngine_Create(IID riid, LPVOID* ppvObj);
  29. /*****************************************************************************
  30. *
  31. * CMigFactory
  32. *
  33. *
  34. *****************************************************************************/
  35. class CMigFactory : public IClassFactory
  36. {
  37. public:
  38. //////////////////////////////////////////////////////
  39. // Public Interfaces
  40. //////////////////////////////////////////////////////
  41. // *** IUnknown ***
  42. virtual STDMETHODIMP_(ULONG) AddRef(void);
  43. virtual STDMETHODIMP_(ULONG) Release(void);
  44. virtual STDMETHODIMP QueryInterface(REFIID riid, LPVOID * ppvObj);
  45. // *** IClassFactory ***
  46. virtual STDMETHODIMP CreateInstance(IUnknown *pUnkOuter, REFIID riid, void **ppvObject);
  47. virtual STDMETHODIMP LockServer(BOOL fLock);
  48. public:
  49. CMigFactory(REFCLSID rclsid);
  50. ~CMigFactory(void);
  51. // Friend Functions
  52. friend HRESULT CMigFactory_Create(REFCLSID rclsid, REFIID riid, LPVOID * ppvObj);
  53. protected:
  54. int m_cRef;
  55. CLSID m_rclsid;
  56. };
  57. /*****************************************************************************
  58. * IClassFactory::CreateInstance
  59. *****************************************************************************/
  60. HRESULT CMigFactory::CreateInstance(IUnknown * punkOuter, REFIID riid, LPVOID * ppvObj)
  61. {
  62. HRESULT hres = ResultFromScode(REGDB_E_CLASSNOTREG);
  63. if (!punkOuter)
  64. {
  65. if (IsEqualIID(m_rclsid, CLSID_MigWizEngine))
  66. hres = CMigWizEngine_Create(riid, ppvObj);
  67. else
  68. hres = E_FAIL; //ASSERT(0);
  69. }
  70. else
  71. { // Does anybody support aggregation any more?
  72. hres = ResultFromScode(CLASS_E_NOAGGREGATION);
  73. }
  74. if (FAILED(hres) && ppvObj)
  75. {
  76. *ppvObj = NULL; // Be Robust. NT #355186
  77. }
  78. return hres;
  79. }
  80. /*****************************************************************************
  81. *
  82. * IClassFactory::LockServer
  83. *
  84. * Locking the server is identical to
  85. * creating an object and not releasing it until you want to unlock
  86. * the server.
  87. *
  88. *****************************************************************************/
  89. HRESULT CMigFactory::LockServer(BOOL fLock)
  90. {
  91. if (fLock)
  92. DllAddRef();
  93. else
  94. DllRelease();
  95. return S_OK;
  96. }
  97. /*****************************************************************************
  98. *
  99. * CFtpFactory_Create
  100. *
  101. *****************************************************************************/
  102. HRESULT CMigFactory_Create(REFCLSID rclsid, REFIID riid, LPVOID * ppvObj)
  103. {
  104. HRESULT hres;
  105. if (IsEqualIID(riid, IID_IClassFactory))
  106. {
  107. *ppvObj = (LPVOID) new CMigFactory(rclsid);
  108. hres = (*ppvObj) ? S_OK : E_OUTOFMEMORY;
  109. }
  110. else
  111. hres = ResultFromScode(E_NOINTERFACE);
  112. return hres;
  113. }
  114. /****************************************************\
  115. Constructor
  116. \****************************************************/
  117. CMigFactory::CMigFactory(REFCLSID rclsid) : m_cRef(1)
  118. {
  119. m_rclsid = rclsid;
  120. DllAddRef();
  121. }
  122. /****************************************************\
  123. Destructor
  124. \****************************************************/
  125. CMigFactory::~CMigFactory()
  126. {
  127. DllRelease();
  128. }
  129. //===========================
  130. // *** IUnknown Interface ***
  131. //===========================
  132. ULONG CMigFactory::AddRef()
  133. {
  134. m_cRef++;
  135. return m_cRef;
  136. }
  137. ULONG CMigFactory::Release()
  138. {
  139. m_cRef--;
  140. if (m_cRef > 0)
  141. return m_cRef;
  142. delete this;
  143. return 0;
  144. }
  145. HRESULT CMigFactory::QueryInterface(REFIID riid, void **ppvObj)
  146. {
  147. if (IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, IID_IClassFactory))
  148. {
  149. *ppvObj = SAFECAST(this, IClassFactory *);
  150. }
  151. else
  152. {
  153. *ppvObj = NULL;
  154. return E_NOINTERFACE;
  155. }
  156. AddRef();
  157. return S_OK;
  158. }
  159. HRESULT CMigWizEngine_Create(IID riid, LPVOID* ppvObj)
  160. {
  161. HRESULT hres = E_OUTOFMEMORY;
  162. CMigWizEngine* pengine = new CMigWizEngine();
  163. *ppvObj = NULL;
  164. if (pengine)
  165. {
  166. hres = pengine->QueryInterface(riid, ppvObj);
  167. pengine->Release();
  168. }
  169. return hres;
  170. }