Leaked source code of windows server 2003
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.

335 lines
7.3 KiB

  1. /*++
  2. Copyright (c) 1997-1999 Microsoft Corporation
  3. Module Name:
  4. server.cpp
  5. Abstract:
  6. This is the implementation of the class factory for the NTFRS WMI provider.
  7. This file contains the implementation of the CFactory class and other
  8. global initialization functions relating to the provider.
  9. Author:
  10. Sudarshan Chitre (sudarc) , Mathew George (t-mattg) - 3-Aug-2000
  11. Environment
  12. User mode winnt
  13. --*/
  14. #include <frswmipv.h>
  15. /////////////////////////////////////////////////////////////////////////////
  16. //
  17. // BEGIN CLSID SPECIFIC SECTION
  18. //
  19. //
  20. //DEFINE_GUID(CLSID_Provider,
  21. // 0x39143F73,0xFDB1,0x4CF5,0x8C,0xB7,0xC8,0x43,0x9E,0x3F,0x5C,0x20);
  22. const CLSID CLSID_Provider = {0x39143F73,0xFDB1,0x4CF5,0x8C,0xB7,0xC8,0x43,0x9E,0x3F,0x5C,0x20};
  23. //
  24. // END CLSID SPECIFIC SECTION
  25. //
  26. /////////////////////////////////////////////////////////////////////////////
  27. static DWORD dwRegId;
  28. static IClassFactory *pClassFactory = NULL;
  29. static ULONG g_cLock = 0;
  30. //
  31. // Routines to update registry when installing/uninstalling our server.
  32. //
  33. void RegisterServer()
  34. {
  35. return;
  36. }
  37. void UnregisterServer()
  38. {
  39. return;
  40. }
  41. //
  42. // Implementation of the IClassFactory interface.
  43. //
  44. CFactory::CFactory(const CLSID & ClsId)
  45. /*++
  46. Routine Description:
  47. Constructor for the class factory.
  48. Arguments:
  49. ClsId : [in] CLSID of the server object which it creates when the
  50. CreateInstance method is called.
  51. Return Value:
  52. None
  53. --*/
  54. {
  55. m_cRef = 0;
  56. g_cLock++;
  57. m_ClsId = ClsId;
  58. }
  59. CFactory::~CFactory()
  60. /*++
  61. Routine Description:
  62. Destructor for the class factory.
  63. Arguments:
  64. None.
  65. Return Value:
  66. None
  67. --*/
  68. {
  69. g_cLock--;
  70. }
  71. // Implementation of the IUnknown interface
  72. ULONG CFactory::AddRef()
  73. /*++
  74. Routine Description:
  75. Increments the reference count of the object.
  76. Arguments:
  77. None
  78. Return Value:
  79. Current reference count. (> 0)
  80. --*/
  81. {
  82. return ++m_cRef;
  83. }
  84. ULONG CFactory::Release()
  85. /*++
  86. Routine Description:
  87. Decrements the reference count of the object. Frees
  88. the object resource when the reference count becomes
  89. zero.
  90. Arguments:
  91. None
  92. Return Value:
  93. New reference count.
  94. --*/
  95. {
  96. if (0 != --m_cRef)
  97. return m_cRef;
  98. delete this;
  99. return 0;
  100. }
  101. STDMETHODIMP CFactory::QueryInterface(REFIID riid, LPVOID * ppv)
  102. /*++
  103. Routine Description:
  104. This method is called by COM to obtain pointers to
  105. the IUnknown or the IClassFactory interface.
  106. Arguments:
  107. riid : GUID of the required interface.
  108. ppv : Pointer where the "interface pointer" is returned.
  109. Return Value:
  110. Status of operation. Pointer to the requested interface
  111. is returned in *ppv.
  112. --*/
  113. {
  114. *ppv = 0;
  115. if (IID_IUnknown == riid || IID_IClassFactory == riid)
  116. {
  117. *ppv = this;
  118. AddRef();
  119. return NOERROR;
  120. }
  121. return E_NOINTERFACE;
  122. }
  123. STDMETHODIMP CFactory::CreateInstance(
  124. LPUNKNOWN pUnkOuter,
  125. REFIID riid,
  126. LPVOID * ppvObj)
  127. /*++
  128. Routine Description:
  129. Constructs an instance of the CProvider object and returns
  130. a pointer to the IUnknown interface.
  131. Arguments:
  132. pUnkOuter : [in] IUnknown of the aggregrator. We do not
  133. support aggregration, and so this parameter should be null.
  134. riid : [in] GUID of the object to be instantiated.
  135. ppv : Destination for the IUnknown interface pointer.
  136. Return Value:
  137. Status of operation. Pointer to the IUnknown interface of the
  138. requested object is returned in *ppv.
  139. S_OK Success
  140. CLASS_E_NOAGGREGATION pUnkOuter must be NULL
  141. E_NOINTERFACE No such interface supported.
  142. --*/
  143. {
  144. IUnknown* pObj;
  145. HRESULT hr;
  146. //
  147. // Defaults
  148. //
  149. *ppvObj=NULL;
  150. hr = E_OUTOFMEMORY;
  151. //
  152. // We aren't supporting aggregation.
  153. //
  154. if (pUnkOuter)
  155. return CLASS_E_NOAGGREGATION;
  156. if (m_ClsId == CLSID_Provider)
  157. {
  158. pObj = (IWbemProviderInit *) new CProvider;
  159. }
  160. if (!pObj)
  161. return hr;
  162. //
  163. // Initialize the object and verify that it can return the
  164. // interface in question.
  165. //
  166. hr = pObj->QueryInterface(riid, ppvObj);
  167. //
  168. // Kill the object if initial creation or Init failed.
  169. //
  170. if (FAILED(hr))
  171. delete pObj;
  172. return hr;
  173. }
  174. STDMETHODIMP CFactory::LockServer(BOOL fLock)
  175. /*++
  176. Routine Description:
  177. Increments.Decrements the reference count of the server, so that
  178. resource can be deallocated when all instances of all objects
  179. provided by ther server are destroyed.
  180. Arguments:
  181. fLock : [in] Boolean indicating whether the refcount is to
  182. be incremented or decremented.
  183. Return Value:
  184. Status of the operation.
  185. --*/
  186. {
  187. if (fLock)
  188. InterlockedIncrement((LONG *) &g_cLock);
  189. else
  190. InterlockedDecrement((LONG *) &g_cLock);
  191. return NOERROR;
  192. }
  193. DWORD FrsWmiInitialize()
  194. /*++
  195. Routine Description:
  196. Main entry point to the WMI subsystem of NTFRS. This function
  197. initializes the COM libraries, initializes security and
  198. registers our class factory with COM.
  199. NOTE : The thread which calls this function should not
  200. terminate until the FrsWmiShutdown() function is called.
  201. Arguments:
  202. None.
  203. Return Value:
  204. Status of the operation.
  205. --*/
  206. {
  207. HRESULT hRes;
  208. // Initialize the COM library.
  209. hRes = CoInitializeEx(NULL, COINIT_MULTITHREADED);
  210. if(FAILED(hRes))
  211. return hRes;
  212. // Initialize COM security.
  213. hRes = CoInitializeSecurity (
  214. NULL, //Points to security descriptor
  215. -1, //Count of entries in asAuthSvc
  216. NULL, //Array of names to register
  217. NULL, //Reserved for future use
  218. RPC_C_AUTHN_LEVEL_CONNECT, //The default authentication level for proxies
  219. RPC_C_IMP_LEVEL_IMPERSONATE, //The default impersonation level for proxies
  220. NULL, //Authentication information for
  221. //each authentication service
  222. EOAC_DYNAMIC_CLOAKING, //Additional client and/or
  223. // server-side capabilities
  224. 0 //Reserved for future use
  225. );
  226. if(FAILED(hRes))
  227. {
  228. CoUninitialize() ;
  229. return hRes;
  230. }
  231. // Get a pointer to our class factory.
  232. pClassFactory = new CFactory(CLSID_Provider);
  233. pClassFactory->AddRef();
  234. // Register our server with COM.
  235. CoRegisterClassObject(CLSID_Provider, pClassFactory,
  236. CLSCTX_LOCAL_SERVER, REGCLS_MULTI_SEPARATE, &dwRegId);
  237. return ERROR_SUCCESS;
  238. }
  239. DWORD FrsWmiShutdown()
  240. /*++
  241. Routine Description:
  242. Shuts down the WMI subsystem within FRS, releases & and deregisters the
  243. class factory, unloads the COM libraries and free any other allocated
  244. resource.
  245. Arguments:
  246. None.
  247. Return Value:
  248. Status of the operation.
  249. --*/
  250. {
  251. //
  252. // Shutdown the server
  253. //
  254. pClassFactory->Release();
  255. CoRevokeClassObject(dwRegId);
  256. CoUninitialize();
  257. return ERROR_SUCCESS;
  258. }