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.

308 lines
7.2 KiB

  1. //***************************************************************************
  2. //
  3. // (c) 1999 by Microsoft Corporation
  4. //
  5. // MAINDLL.CPP
  6. //
  7. // alanbos 23-Mar-99 Created.
  8. //
  9. // Contains DLL entry points.
  10. //
  11. //***************************************************************************
  12. #include "precomp.h"
  13. #include "initguid.h"
  14. // Standard registry key/value names
  15. #define WBEMS_RK_SCC "SOFTWARE\\CLASSES\\CLSID\\"
  16. #define WBEMS_RK_SC "SOFTWARE\\CLASSES\\"
  17. #define WBEMS_RK_THRDMODEL "ThreadingModel"
  18. #define WBEMS_RV_APARTMENT "Apartment"
  19. #define WBEMS_RK_INPROC32 "InProcServer32"
  20. #define WBEMS_RK_CLSID "CLSID"
  21. #define GUIDSIZE 128
  22. // Count number of objects and number of locks.
  23. long g_cObj = 0 ;
  24. ULONG g_cLock = 0 ;
  25. HMODULE ghModule = NULL;
  26. // CLSID for our implementation of IActiveScriptingSite
  27. // {838E2F5E-E20E-11d2-B355-00105A1F473A}
  28. DEFINE_GUID(CLSID_WmiActiveScriptingSite,
  29. 0x838e2f5e, 0xe20e, 0x11d2, 0xb3, 0x55, 0x0, 0x10, 0x5a, 0x1f, 0x47, 0x3a);
  30. // forward defines
  31. STDAPI RegisterCoClass (REFGUID clsid, LPCTSTR desc);
  32. void UnregisterCoClass (REFGUID clsid);
  33. //***************************************************************************
  34. //
  35. // BOOL WINAPI DllMain
  36. //
  37. // DESCRIPTION:
  38. //
  39. // Entry point for DLL. Good place for initialization.
  40. //
  41. // PARAMETERS:
  42. //
  43. // hInstance instance handle
  44. // ulReason why we are being called
  45. // pvReserved reserved
  46. //
  47. // RETURN VALUE:
  48. //
  49. // TRUE if OK.
  50. //
  51. //***************************************************************************
  52. BOOL WINAPI DllMain (
  53. IN HINSTANCE hInstance,
  54. IN ULONG ulReason,
  55. LPVOID pvReserved
  56. )
  57. {
  58. switch (ulReason)
  59. {
  60. case DLL_PROCESS_DETACH:
  61. {
  62. }
  63. return TRUE;
  64. case DLL_THREAD_DETACH:
  65. {
  66. }
  67. return TRUE;
  68. case DLL_PROCESS_ATTACH:
  69. {
  70. if(ghModule == NULL)
  71. ghModule = hInstance;
  72. }
  73. return TRUE;
  74. case DLL_THREAD_ATTACH:
  75. {
  76. }
  77. return TRUE;
  78. }
  79. return TRUE;
  80. }
  81. //***************************************************************************
  82. //
  83. // STDAPI DllGetClassObject
  84. //
  85. // DESCRIPTION:
  86. //
  87. // Called when Ole wants a class factory. Return one only if it is the sort
  88. // of class this DLL supports.
  89. //
  90. // PARAMETERS:
  91. //
  92. // rclsid CLSID of the object that is desired.
  93. // riid ID of the desired interface.
  94. // ppv Set to the class factory.
  95. //
  96. // RETURN VALUE:
  97. //
  98. // S_OK all is well
  99. // E_FAILED not something we support
  100. //
  101. //***************************************************************************
  102. STDAPI DllGetClassObject(
  103. IN REFCLSID rclsid,
  104. IN REFIID riid,
  105. OUT LPVOID *ppv
  106. )
  107. {
  108. HRESULT hr;
  109. CWmiScriptingHostFactory *pObj = NULL;
  110. if (CLSID_WmiActiveScriptingSite == rclsid)
  111. pObj=new CWmiScriptingHostFactory();
  112. if(NULL == pObj)
  113. return E_FAIL;
  114. hr=pObj->QueryInterface(riid, ppv);
  115. if (FAILED(hr))
  116. delete pObj;
  117. return hr ;
  118. }
  119. //***************************************************************************
  120. //
  121. // STDAPI DllCanUnloadNow
  122. //
  123. // DESCRIPTION:
  124. //
  125. // Answers if the DLL can be freed, that is, if there are no
  126. // references to anything this DLL provides.
  127. //
  128. // RETURN VALUE:
  129. //
  130. // S_OK if it is OK to unload
  131. // S_FALSE if still in use
  132. //
  133. //***************************************************************************
  134. STDAPI DllCanUnloadNow ()
  135. {
  136. return (0L==g_cObj && 0L==g_cLock) ? S_OK : S_FALSE;
  137. }
  138. //***************************************************************************
  139. //
  140. // STDAPI RegisterCoClass
  141. //
  142. // DESCRIPTION:
  143. //
  144. // Helpers for the tiresome business of registry setup
  145. //
  146. // RETURN VALUE:
  147. //
  148. // ERROR alas
  149. // NOERROR rejoice
  150. //
  151. //***************************************************************************
  152. STDAPI RegisterCoClass (REFGUID clsid, LPCTSTR desc)
  153. {
  154. OLECHAR wcID[GUIDSIZE];
  155. char nwcID[GUIDSIZE];
  156. char szModule[MAX_PATH];
  157. HKEY hKey1 = NULL, hKey2 = NULL;
  158. char *szCLSID = new char [strlen (WBEMS_RK_SCC) + GUIDSIZE + 1];
  159. // Create the path.
  160. if(0 ==StringFromGUID2(clsid, wcID, GUIDSIZE))
  161. return ERROR;
  162. wcstombs(nwcID, wcID, GUIDSIZE);
  163. lstrcpy (szCLSID, WBEMS_RK_SCC);
  164. lstrcat (szCLSID, nwcID);
  165. if(0 == GetModuleFileName(ghModule, szModule, MAX_PATH))
  166. {
  167. delete [] szCLSID;
  168. return ERROR;
  169. }
  170. // Create entries under CLSID
  171. if(ERROR_SUCCESS == RegCreateKey(HKEY_LOCAL_MACHINE, szCLSID, &hKey1))
  172. {
  173. // Description (on main key)
  174. RegSetValueEx(hKey1, NULL, 0, REG_SZ, (BYTE *)desc, (strlen(desc)+1));
  175. // Register as inproc server
  176. if (ERROR_SUCCESS == RegCreateKey(hKey1, WBEMS_RK_INPROC32 ,&hKey2))
  177. {
  178. RegSetValueEx(hKey2, NULL, 0, REG_SZ, (BYTE *)szModule,
  179. (strlen(szModule)+1));
  180. RegSetValueEx(hKey2, WBEMS_RK_THRDMODEL, 0, REG_SZ, (BYTE *)WBEMS_RV_APARTMENT,
  181. (strlen(WBEMS_RV_APARTMENT)+1));
  182. RegCloseKey(hKey2);
  183. }
  184. RegCloseKey(hKey1);
  185. }
  186. else
  187. {
  188. delete [] szCLSID;
  189. return ERROR;
  190. }
  191. delete [] szCLSID;
  192. return NOERROR;
  193. }
  194. //***************************************************************************
  195. //
  196. // DllRegisterServer
  197. //
  198. // Purpose: Called during setup or by regsvr32.
  199. //
  200. // Return: NOERROR if registration successful, error otherwise.
  201. //***************************************************************************
  202. STDAPI DllRegisterServer(void)
  203. {
  204. return RegisterCoClass(CLSID_WmiActiveScriptingSite, "WMI Active Scripting Host");
  205. }
  206. //***************************************************************************
  207. //
  208. // STDAPI UnregisterCoClass
  209. //
  210. // DESCRIPTION:
  211. //
  212. // Helpers for the tiresome business of registry cleanup
  213. //
  214. // RETURN VALUE:
  215. //
  216. // ERROR alas
  217. // NOERROR rejoice
  218. //
  219. //***************************************************************************
  220. void UnregisterCoClass (REFGUID clsid)
  221. {
  222. OLECHAR wcID[GUIDSIZE];
  223. char nwcID[GUIDSIZE];
  224. HKEY hKey = NULL;
  225. char *szCLSID = new char [strlen (WBEMS_RK_SCC) + GUIDSIZE + 1];
  226. // Create the path using the CLSID
  227. if(0 != StringFromGUID2(clsid, wcID, GUIDSIZE))
  228. {
  229. wcstombs(nwcID, wcID, GUIDSIZE);
  230. lstrcpy (szCLSID, WBEMS_RK_SCC);
  231. lstrcat (szCLSID, nwcID);
  232. // First delete the subkeys of the HKLM\Software\Classes\CLSID\{GUID} entry
  233. if(NO_ERROR == RegOpenKey(HKEY_LOCAL_MACHINE, szCLSID, &hKey))
  234. {
  235. RegDeleteKey(hKey, WBEMS_RK_INPROC32);
  236. RegCloseKey(hKey);
  237. }
  238. // Delete the HKLM\Software\Classes\CLSID\{GUID} key
  239. if(NO_ERROR == RegOpenKey(HKEY_LOCAL_MACHINE, WBEMS_RK_SCC, &hKey))
  240. {
  241. RegDeleteKey(hKey, nwcID);
  242. RegCloseKey(hKey);
  243. }
  244. }
  245. delete [] szCLSID;
  246. }
  247. //***************************************************************************
  248. //
  249. // DllUnregisterServer
  250. //
  251. // Purpose: Called when it is time to remove the registry entries.
  252. //
  253. // Return: NOERROR if registration successful, error otherwise.
  254. //***************************************************************************
  255. STDAPI DllUnregisterServer(void)
  256. {
  257. UnregisterCoClass(CLSID_WmiActiveScriptingSite);
  258. return NOERROR;
  259. }