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.

319 lines
7.1 KiB

  1. //***************************************************************************
  2. //
  3. // MAINDLL.CPP
  4. //
  5. // Module: SCE WMI provider code
  6. //
  7. // Purpose: Contains DLL entry points. Also has code that controls
  8. // when the DLL can be unloaded by tracking the number of
  9. // objects and locks as well as routines that support
  10. // self registration.
  11. //
  12. // Copyright (c) 1999-2001 Microsoft Corporation
  13. //
  14. //***************************************************************************
  15. #include <objbase.h>
  16. #include <initguid.h>
  17. #include "sceprov.h"
  18. #include "scecore_i.c"
  19. #include "sceparser.h"
  20. #include "persistmgr.h"
  21. #include "resource.h"
  22. #ifdef _MERGE_PROXYSTUB
  23. extern "C" HINSTANCE hProxyDll;
  24. #endif
  25. //
  26. // this is the ATL wrapper for our module
  27. //
  28. CComModule _Module;
  29. //
  30. // this is the ATL object map. If you need to create another COM object that
  31. // is externally createable, then you need to add an entry here. You don't need
  32. // to mess with class factory stuff.
  33. //
  34. BEGIN_OBJECT_MAP(ObjectMap)
  35. OBJECT_ENTRY(CLSID_SceProv, CSceWmiProv)
  36. OBJECT_ENTRY(CLSID_ScePathParser, CScePathParser)
  37. OBJECT_ENTRY(CLSID_SceQueryParser, CSceQueryParser)
  38. OBJECT_ENTRY(CLSID_ScePersistMgr, CScePersistMgr)
  39. END_OBJECT_MAP()
  40. LPCWSTR lpszSceProvMof = L"Wbem\\SceProv.mof";
  41. /*
  42. Routine Description:
  43. Name:
  44. DllMain
  45. Functionality:
  46. Entry point for DLL.
  47. Arguments:
  48. See DllMain on MSDN.
  49. Return Value:
  50. TRUE if OK.
  51. Notes:
  52. DllMain will be called for attach and detach. When other dlls are loaded, this function
  53. will also get called. So, this is not necessary a good place for you to initialize some
  54. globals unless you know precisely what you are doing. Please read MSDN for details before
  55. you try to modify this function.
  56. As a general design approach, we use gloal class instances to guarantee its creation and
  57. destruction.
  58. */
  59. extern "C"
  60. BOOL WINAPI DllMain (
  61. IN HINSTANCE hInstance,
  62. IN ULONG ulReason,
  63. LPVOID pvReserved
  64. )
  65. {
  66. #ifdef _MERGE_PROXYSTUB
  67. if (!PrxDllMain(hInstance, dwReason, lpReserved))
  68. return FALSE;
  69. #endif
  70. if (ulReason == DLL_PROCESS_ATTACH)
  71. {
  72. _Module.Init(ObjectMap, hInstance);
  73. DisableThreadLibraryCalls(hInstance);
  74. }
  75. else if (ulReason == DLL_PROCESS_DETACH)
  76. {
  77. _Module.Term();
  78. }
  79. return TRUE;
  80. }
  81. /*
  82. Routine Description:
  83. Name:
  84. DllGetClassObject
  85. Functionality:
  86. Retrieves the class object from a DLL object handler or object application.
  87. DllGetClassObject is called from within the CoGetClassObject function when the
  88. class context is a DLL.
  89. As a benefit of using ATL, we just need to delegate this to our _Module object.
  90. Arguments:
  91. rclsid - Class ID (guid) for the class object being requested.
  92. riid - Interface GUID that is being requested.
  93. ppv - the interface pointer being returned if successful
  94. Return Value:
  95. Whatever GetClassObject returns for this class ID and its requested interface id.
  96. Notes:
  97. */
  98. STDAPI DllGetClassObject (
  99. IN REFCLSID rclsid,
  100. IN REFIID riid,
  101. OUT PPVOID ppv
  102. )
  103. {
  104. #ifdef _MERGE_PROXYSTUB
  105. if (PrxDllGetClassObject(rclsid, riid, ppv) == S_OK)
  106. return S_OK;
  107. #endif
  108. return _Module.GetClassObject(rclsid, riid, ppv);
  109. }
  110. /*
  111. Routine Description:
  112. Name:
  113. DllCanUnloadNow
  114. Functionality:
  115. Called periodically by COM in order to determine if the DLL can be freed.
  116. As a benefit of using ATL, we just need to delegate this to our _Module object.
  117. Arguments:
  118. None
  119. Return Value:
  120. S_OK if the dll can be unloaded. Otherwise, it returns S_FALSE;
  121. Notes:
  122. */
  123. STDAPI DllCanUnloadNow (void)
  124. {
  125. #ifdef _MERGE_PROXYSTUB
  126. if (PrxDllCanUnloadNow() != S_OK)
  127. return S_FALSE;
  128. #endif
  129. return (_Module.GetLockCount() == 0) ? S_OK : S_FALSE;
  130. }
  131. /*
  132. Routine Description:
  133. Name:
  134. DllRegisterServer
  135. Functionality:
  136. (1) Called during dll registration.
  137. As a benefit of using ATL, we just need to delegate this to our _Module object.
  138. (2) Since we are a provider, we will also compile our MOF file(s).
  139. Arguments:
  140. None
  141. Return Value:
  142. Success: success code (use SUCCEEDED(hr) to test).
  143. Failure: it returns various errors;
  144. Notes:
  145. */
  146. STDAPI DllRegisterServer(void)
  147. {
  148. #ifdef _MERGE_PROXYSTUB
  149. HRESULT hRes = PrxDllRegisterServer();
  150. if (FAILED(hRes))
  151. return hRes;
  152. #endif
  153. HRESULT hr = _Module.RegisterServer(TRUE);
  154. //
  155. // now compile the MOF file. This is only our current approach. It is not
  156. // required to compile the MOF file during DLL registration. Users can compile
  157. // MOF file(s) indenpendently from dll registration
  158. //
  159. if (SUCCEEDED(hr))
  160. {
  161. const int WBEM_MOF_FILE_LEN = 30;
  162. WCHAR szBuffer[MAX_PATH];
  163. WCHAR szMofFile[MAX_PATH + WBEM_MOF_FILE_LEN];
  164. szBuffer[0] = L'\0';
  165. szMofFile[0] = L'\0';
  166. if ( GetSystemDirectory( szBuffer, MAX_PATH ) ) {
  167. LPWSTR sz = szBuffer + wcslen(szBuffer);
  168. if ( sz != szBuffer && *(sz-1) != L'\\') {
  169. *sz++ = L'\\';
  170. *sz = L'\0';
  171. }
  172. hr = WBEM_NO_ERROR;
  173. //
  174. // this protects buffer overrun
  175. //
  176. if (wcslen(lpszSceProvMof) < WBEM_MOF_FILE_LEN)
  177. {
  178. wcscpy(szMofFile, szBuffer);
  179. wcscat( szMofFile, lpszSceProvMof);
  180. //
  181. // we need COM to be ready
  182. //
  183. hr = ::CoInitialize (NULL);
  184. if (SUCCEEDED(hr))
  185. {
  186. //
  187. // Get the MOF compiler interface
  188. //
  189. CComPtr<IMofCompiler> srpMof;
  190. hr = ::CoCreateInstance (CLSID_MofCompiler, NULL, CLSCTX_INPROC_SERVER, IID_IMofCompiler, (void **)&srpMof);
  191. if (SUCCEEDED(hr))
  192. {
  193. WBEM_COMPILE_STATUS_INFO stat;
  194. hr = srpMof->CompileFile( szMofFile,
  195. NULL,NULL,NULL,NULL,
  196. 0,0,0, &stat);
  197. }
  198. ::CoUninitialize();
  199. }
  200. }
  201. }
  202. }
  203. return hr;
  204. }
  205. /*
  206. Routine Description:
  207. Name:
  208. DllRegisterServer
  209. Functionality:
  210. (1) Called when it is time to remove the registry entries.
  211. As a benefit of using ATL, we just need to delegate this to our _Module object.
  212. Arguments:
  213. None
  214. Return Value:
  215. Success: S_OK (same as NOERROR).
  216. Failure: it returns various errors;
  217. Notes:
  218. There is no MOF unregistration. Otherwise, we should probably do a MOF unregistration
  219. */
  220. STDAPI DllUnregisterServer(void)
  221. {
  222. #ifdef _MERGE_PROXYSTUB
  223. PrxDllUnregisterServer();
  224. #endif
  225. _Module.UnregisterServer();
  226. return S_OK;
  227. }