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.

344 lines
6.7 KiB

  1. // comobj.cpp : Implementation of CWamregApp and DLL registration.
  2. #include "common.h"
  3. #include "comobj.h"
  4. #include "iwamreg.h"
  5. #include "wamadm.h"
  6. #define DEFAULT_TRACE_FLAGS (DEBUG_ERROR)
  7. #include "auxfunc.h"
  8. #include "dbgutil.h"
  9. //==========================================================================
  10. // Global variables
  11. //
  12. //==========================================================================
  13. CWmRgSrvFactory* g_pWmRgSrvFactory = NULL;
  14. DWORD g_dwRefCount = 0;
  15. DWORD g_dwWamAdminRegister = 0;
  16. //==========================================================================
  17. // Static functions
  18. //
  19. //==========================================================================
  20. /////////////////////////////////////////////////////////////////////////////
  21. //
  22. /*===================================================================
  23. CWmRgSrv::CWmRgSrv
  24. Constructor for CWmRgSrv.
  25. Parameter: NONE
  26. Return: NONE
  27. Side affect: Init a Metabase pointer(via UNICODE DCOM interface), Create an Event.
  28. ===================================================================*/
  29. CWmRgSrv::CWmRgSrv()
  30. : m_cRef(1)
  31. {
  32. InterlockedIncrement((long *)&g_dwRefCount);
  33. }
  34. /*===================================================================
  35. CWmRgSrv::~CWmRgSrv
  36. Destructor for CWmRgSrv.
  37. Parameter: NONE
  38. Return: NONE
  39. Side affect: Release the Metabase pointer, and Destroy the internal event object.
  40. ===================================================================*/
  41. CWmRgSrv::~CWmRgSrv()
  42. {
  43. InterlockedDecrement((long *)&g_dwRefCount);
  44. DBG_ASSERT(m_cRef == 0);
  45. }
  46. /*===================================================================
  47. CWmRgSrv::QueryInterface
  48. UNDONE
  49. Parameter:
  50. NONE.
  51. Return: HRESULT
  52. Side affect: .
  53. ===================================================================*/
  54. STDMETHODIMP CWmRgSrv::QueryInterface(REFIID riid, void ** ppv)
  55. {
  56. if (riid == IID_IUnknown || riid == IID_IADMEXT)
  57. {
  58. *ppv = static_cast<IADMEXT*>(this);
  59. }
  60. else
  61. {
  62. *ppv = NULL;
  63. return E_NOINTERFACE;
  64. }
  65. reinterpret_cast<IUnknown*>(*ppv)->AddRef();
  66. return S_OK;
  67. }
  68. STDMETHODIMP_(ULONG) CWmRgSrv::AddRef( )
  69. {
  70. return InterlockedIncrement(&m_cRef);
  71. }
  72. STDMETHODIMP_(ULONG) CWmRgSrv::Release( )
  73. {
  74. if (InterlockedDecrement(&m_cRef) == 0)
  75. {
  76. delete this;
  77. return 0;
  78. }
  79. return m_cRef;
  80. }
  81. /*===================================================================
  82. CWmRgSrv::Initialize
  83. UNDONE
  84. Parameter:
  85. NONE.
  86. Return: HRESULT
  87. Side affect: .
  88. ===================================================================*/
  89. STDMETHODIMP CWmRgSrv::Initialize( )
  90. {
  91. HRESULT hrReturn = NOERROR;
  92. CWamAdminFactory *pWamAdminFactory = new CWamAdminFactory();
  93. if (pWamAdminFactory == NULL)
  94. {
  95. DBGPRINTF((DBG_CONTEXT, "WamRegSrv Init failed. error %08x\n",
  96. GetLastError()));
  97. hrReturn = E_OUTOFMEMORY;
  98. goto LExit;
  99. }
  100. hrReturn = g_RegistryConfig.LoadWamDllPath();
  101. if (SUCCEEDED(hrReturn))
  102. {
  103. hrReturn = WamRegMetabaseConfig::MetabaseInit();
  104. }
  105. if (SUCCEEDED(hrReturn))
  106. {
  107. hrReturn = CoRegisterClassObject(CLSID_WamAdmin,
  108. static_cast<IUnknown *>(pWamAdminFactory),
  109. CLSCTX_SERVER,
  110. REGCLS_MULTIPLEUSE,
  111. &g_dwWamAdminRegister);
  112. if (FAILED(hrReturn))
  113. {
  114. DBGPRINTF((DBG_CONTEXT, "WamRegSrv Init failed. error %08x\n",
  115. GetLastError()));
  116. }
  117. }
  118. if (FAILED(hrReturn))
  119. {
  120. if (g_dwWamAdminRegister)
  121. {
  122. //
  123. // PREfix has a problem with this code because we're not checking
  124. // the return value of CoRevokeClassObject. There's really
  125. // nothing different we could do in the event of failure, so
  126. // there's no point in checking.
  127. //
  128. /* INTRINSA suppress=all */
  129. CoRevokeClassObject(g_dwWamAdminRegister);
  130. g_dwWamAdminRegister = 0;
  131. }
  132. RELEASE(pWamAdminFactory);
  133. }
  134. if (FAILED(hrReturn))
  135. {
  136. WamRegMetabaseConfig::MetabaseUnInit();
  137. }
  138. LExit:
  139. return hrReturn;
  140. }
  141. /*===================================================================
  142. CWmRgSrv::Terminate
  143. UNDONE
  144. Parameter:
  145. NONE.
  146. Return: HRESULT
  147. Side affect: .
  148. ===================================================================*/
  149. HRESULT CWmRgSrv::EnumDcomCLSIDs
  150. (
  151. /* [size_is][out] */CLSID *pclsidDcom,
  152. /* [in] */ DWORD dwEnumIndex
  153. )
  154. {
  155. HRESULT hr = HRESULT_FROM_WIN32(ERROR_NO_MORE_ITEMS);
  156. if (dwEnumIndex == 0)
  157. {
  158. *pclsidDcom = CLSID_WamAdmin;
  159. hr = S_OK;
  160. }
  161. return hr;
  162. }
  163. /*===================================================================
  164. CWmRgSrv::Terminate
  165. UNDONE
  166. Parameter:
  167. NONE.
  168. Return: HRESULT
  169. Side affect: .
  170. ===================================================================*/
  171. STDMETHODIMP CWmRgSrv::Terminate( )
  172. {
  173. //
  174. // PREfix has a problem with this code because we're not checking
  175. // the return value of CoRevokeClassObject. There's really
  176. // nothing different we could do in the event of failure, so
  177. // there's no point in checking.
  178. //
  179. /* INTRINSA suppress=all */
  180. CoRevokeClassObject(g_dwWamAdminRegister);
  181. WamRegMetabaseConfig::MetabaseUnInit();
  182. return S_OK;
  183. }
  184. /*
  185. CWmRgSrvFactory: Class Factory IUnknown Implementation
  186. */
  187. CWmRgSrvFactory::CWmRgSrvFactory()
  188. : m_pWmRgServiceObj(NULL)
  189. {
  190. m_cRef = 0;
  191. InterlockedIncrement((long *)&g_dwRefCount);
  192. }
  193. CWmRgSrvFactory::~CWmRgSrvFactory()
  194. {
  195. InterlockedDecrement((long *)&g_dwRefCount);
  196. RELEASE(m_pWmRgServiceObj);
  197. }
  198. STDMETHODIMP CWmRgSrvFactory::QueryInterface(REFIID riid, void ** ppv)
  199. {
  200. HRESULT hrReturn = S_OK;
  201. if (riid==IID_IUnknown || riid == IID_IClassFactory)
  202. {
  203. if (m_pWmRgServiceObj == NULL)
  204. {
  205. *ppv = (IClassFactory *) this;
  206. AddRef();
  207. hrReturn = S_OK;
  208. }
  209. else
  210. {
  211. *ppv = (IClassFactory *) this;
  212. AddRef();
  213. hrReturn = S_OK;
  214. }
  215. }
  216. else
  217. {
  218. hrReturn = E_NOINTERFACE;
  219. }
  220. return hrReturn;
  221. }
  222. STDMETHODIMP_(ULONG) CWmRgSrvFactory::AddRef( )
  223. {
  224. DWORD dwRefCount;
  225. dwRefCount = InterlockedIncrement((long *)&m_cRef);
  226. return dwRefCount;
  227. }
  228. STDMETHODIMP_(ULONG) CWmRgSrvFactory::Release( )
  229. {
  230. DWORD dwRefCount;
  231. dwRefCount = InterlockedDecrement((long *)&m_cRef);
  232. return dwRefCount;
  233. }
  234. STDMETHODIMP CWmRgSrvFactory::CreateInstance(IUnknown * pUnknownOuter, REFIID riid, void ** ppv)
  235. {
  236. HRESULT hrReturn = NOERROR;
  237. if (pUnknownOuter != NULL)
  238. {
  239. hrReturn = CLASS_E_NOAGGREGATION;
  240. }
  241. if (m_pWmRgServiceObj == NULL)
  242. {
  243. m_pWmRgServiceObj = new CWmRgSrv();
  244. if (m_pWmRgServiceObj == NULL)
  245. {
  246. hrReturn = E_OUTOFMEMORY;
  247. }
  248. }
  249. if (m_pWmRgServiceObj)
  250. {
  251. if (FAILED(m_pWmRgServiceObj->QueryInterface(riid, ppv)))
  252. {
  253. hrReturn = E_NOINTERFACE;
  254. }
  255. }
  256. return hrReturn;
  257. }
  258. STDMETHODIMP CWmRgSrvFactory::LockServer(BOOL fLock)
  259. {
  260. if (fLock)
  261. {
  262. InterlockedIncrement((long *)&g_dwRefCount);
  263. }
  264. else
  265. {
  266. InterlockedDecrement((long *)&g_dwRefCount);
  267. }
  268. return S_OK;
  269. }