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.

321 lines
8.1 KiB

  1. /*++
  2. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  3. Module Name:
  4. MAINDLL.CPP
  5. Abstract:
  6. Contains DLL Entrypoints
  7. History:
  8. --*/
  9. #include "precomp.h"
  10. #include <wbemcli.h>
  11. #include "pathparse.h"
  12. #include "wbemerror.h"
  13. #include <wmiutils.h>
  14. #include <wbemint.h>
  15. #include "genlex.h"
  16. #include "assocqp.h"
  17. #include "ql.h"
  18. #include "wmiquery.h"
  19. #include "helpers.h"
  20. HINSTANCE g_hInstance;
  21. long g_cLock;
  22. long g_cObj;
  23. //***************************************************************************
  24. //
  25. // BOOL WINAPI DllMain
  26. //
  27. // DESCRIPTION:
  28. //
  29. // Entry point for DLL. Good place for initialization.
  30. //
  31. // PARAMETERS:
  32. //
  33. // hInstance instance handle
  34. // ulReason why we are being called
  35. // pvReserved reserved
  36. //
  37. // RETURN VALUE:
  38. //
  39. // TRUE if OK.
  40. //
  41. //***************************************************************************
  42. BOOL WINAPI DllMain(
  43. IN HINSTANCE hInstance,
  44. IN ULONG ulReason,
  45. LPVOID pvReserved)
  46. {
  47. if (DLL_PROCESS_DETACH == ulReason)
  48. {
  49. CWmiQuery::Shutdown();
  50. }
  51. else if (DLL_PROCESS_ATTACH == ulReason)
  52. {
  53. g_hInstance = hInstance;
  54. DisableThreadLibraryCalls ( hInstance ) ;
  55. CWmiQuery::Startup();
  56. }
  57. return TRUE;
  58. }
  59. //***************************************************************************
  60. //
  61. // STDAPI DllGetClassObject
  62. //
  63. // DESCRIPTION:
  64. //
  65. // Called when Ole wants a class factory. Return one only if it is the sort
  66. // of class this DLL supports.
  67. //
  68. // PARAMETERS:
  69. //
  70. // rclsid CLSID of the object that is desired.
  71. // riid ID of the desired interface.
  72. // ppv Set to the class factory.
  73. //
  74. // RETURN VALUE:
  75. //
  76. // S_OK all is well
  77. // E_FAILED not something we support
  78. //
  79. //***************************************************************************
  80. STDAPI DllGetClassObject(
  81. IN REFCLSID rclsid,
  82. IN REFIID riid,
  83. OUT PPVOID ppv)
  84. {
  85. HRESULT hr = WBEM_E_FAILED;
  86. IClassFactory * pFactory = NULL;
  87. if (CLSID_WbemDefPath == rclsid)
  88. pFactory = new CGenFactory<CDefPathParser>();
  89. //postponed till Blackcomb if (CLSID_UmiDefURL == rclsid)
  90. //postponed till Blackcomb pFactory = new CGenFactory<CDefPathParser>();
  91. else if (CLSID_WbemStatusCodeText == rclsid)
  92. pFactory = new CGenFactory<CWbemError>();
  93. else if (CLSID_WbemQuery == rclsid)
  94. pFactory = new CGenFactory<CWmiQuery>();
  95. if(pFactory == NULL)
  96. return E_FAIL;
  97. hr=pFactory->QueryInterface(riid, ppv);
  98. if (FAILED(hr))
  99. delete pFactory;
  100. return hr;
  101. }
  102. //***************************************************************************
  103. //
  104. // STDAPI DllCanUnloadNow
  105. //
  106. // DESCRIPTION:
  107. //
  108. // Answers if the DLL can be freed, that is, if there are no
  109. // references to anything this DLL provides.
  110. //
  111. // RETURN VALUE:
  112. //
  113. // S_OK if it is OK to unload
  114. // S_FALSE if still in use
  115. //
  116. //***************************************************************************
  117. STDAPI DllCanUnloadNow(void)
  118. {
  119. SCODE sc;
  120. HRESULT hRes = CWmiQuery::CanUnload();
  121. if (hRes == S_FALSE)
  122. return S_FALSE;
  123. // It is OK to unload if there are no objects or locks on the
  124. // class factory.
  125. sc=(0L==g_cObj && 0L==g_cLock) ? S_OK : S_FALSE;
  126. return ResultFromScode(sc);
  127. }
  128. POLARITY void RegisterUtilsDLL(IN HMODULE hModule, IN GUID guid, IN TCHAR * pDesc, TCHAR * pModel,
  129. TCHAR * pProgID)
  130. {
  131. // char szID[128];
  132. TCHAR wcID[128];
  133. TCHAR szCLSID[128];
  134. TCHAR szModule[MAX_PATH];
  135. HKEY hKey1 = NULL, hKey2 = NULL;
  136. // Create the path.
  137. wchar_t strCLSID[128];
  138. if(0 ==StringFromGUID2(guid, strCLSID, 128))
  139. return;
  140. #ifdef UNICODE
  141. lstrcpy(wcID, strCLSID);
  142. #else
  143. wcstombs(wcID, strCLSID, 128);
  144. #endif
  145. // wcstombs(szID, wcID, 128);
  146. lstrcpy(szCLSID, __TEXT("SOFTWARE\\CLASSES\\CLSID\\"));
  147. lstrcat(szCLSID, wcID);
  148. // Create entries under CLSID
  149. if(ERROR_SUCCESS != RegCreateKey(HKEY_LOCAL_MACHINE, szCLSID, &hKey1))
  150. return;
  151. RegSetValueEx(hKey1, NULL, 0, REG_SZ, (BYTE *)pDesc, (lstrlen(pDesc)+1) * sizeof(TCHAR));
  152. if(ERROR_SUCCESS != RegCreateKey(hKey1,__TEXT("InprocServer32"),&hKey2))
  153. return;
  154. if(0 == GetModuleFileName(hModule, szModule, MAX_PATH))
  155. return;
  156. RegSetValueEx(hKey2, NULL, 0, REG_SZ, (BYTE *)szModule,
  157. (lstrlen(szModule)+1) * sizeof(TCHAR));
  158. RegSetValueEx(hKey2, __TEXT("ThreadingModel"), 0, REG_SZ,
  159. (BYTE *)pModel, (lstrlen(pModel)+1) * sizeof(TCHAR));
  160. RegCloseKey(hKey1);
  161. RegCloseKey(hKey2);
  162. // If there is a progid, then add it too
  163. if(pProgID)
  164. {
  165. wsprintf(wcID, __TEXT("SOFTWARE\\CLASSES\\%s"), pProgID);
  166. if(ERROR_SUCCESS == RegCreateKey(HKEY_LOCAL_MACHINE, wcID, &hKey1))
  167. {
  168. RegSetValueEx(hKey1, NULL, 0, REG_SZ, (BYTE *)pDesc , (lstrlen(pDesc)+1) * sizeof(TCHAR));
  169. if(ERROR_SUCCESS == RegCreateKey(hKey1,__TEXT("CLSID"),&hKey2))
  170. {
  171. // wcstombs(szID, wcID, 128);
  172. RegSetValueEx(hKey2, NULL, 0, REG_SZ, (BYTE *)wcID,
  173. (lstrlen(wcID)+1) * sizeof(TCHAR));
  174. RegCloseKey(hKey2);
  175. hKey2 = NULL;
  176. }
  177. RegCloseKey(hKey1);
  178. }
  179. }
  180. return;
  181. }
  182. POLARITY void UnRegisterUtilsDLL(GUID guid, TCHAR * pProgID)
  183. {
  184. // char szID[128];
  185. TCHAR wcID[128];
  186. TCHAR szCLSID[128];
  187. HKEY hKey;
  188. // Create the path using the CLSID
  189. wchar_t strCLSID[128];
  190. if(0 ==StringFromGUID2(guid, strCLSID, 128))
  191. return;
  192. #ifdef UNICODE
  193. lstrcpy(wcID, strCLSID);
  194. #else
  195. wcstombs(wcID, strCLSID, 128);
  196. #endif
  197. // wcstombs(szID, wcID, 128);
  198. lstrcpy(szCLSID, __TEXT("SOFTWARE\\CLASSES\\CLSID\\"));
  199. lstrcat(szCLSID, wcID);
  200. // First delete the InProcServer subkey.
  201. DWORD dwRet = RegOpenKey(HKEY_LOCAL_MACHINE, szCLSID, &hKey);
  202. if(dwRet == NO_ERROR)
  203. {
  204. RegDeleteKey(hKey, __TEXT("InProcServer32"));
  205. RegCloseKey(hKey);
  206. }
  207. dwRet = RegOpenKey(HKEY_LOCAL_MACHINE, __TEXT("SOFTWARE\\CLASSES\\CLSID"), &hKey);
  208. if(dwRet == NO_ERROR)
  209. {
  210. RegDeleteKey(hKey,wcID);
  211. RegCloseKey(hKey);
  212. }
  213. if(pProgID)
  214. {
  215. HKEY hKey;
  216. DWORD dwRet = RegOpenKey(HKEY_LOCAL_MACHINE, pProgID, &hKey);
  217. if(dwRet == NO_ERROR)
  218. {
  219. RegDeleteKey(hKey, __TEXT("CLSID"));
  220. RegCloseKey(hKey);
  221. }
  222. RegDeleteKey(HKEY_LOCAL_MACHINE, pProgID);
  223. }
  224. }
  225. //***************************************************************************
  226. //
  227. // DllRegisterServer
  228. //
  229. // Purpose: Called during setup or by regsvr32.
  230. //
  231. // Return: NOERROR if registration successful, error otherwise.
  232. //***************************************************************************
  233. #define LocatorPROGID "WBEMComLocator"
  234. STDAPI DllRegisterServer(void)
  235. {
  236. RegisterUtilsDLL(g_hInstance, CLSID_WbemDefPath, __TEXT("WbemDefaultPathParser"), __TEXT("Both"), NULL);
  237. //postponed till Blackcomb RegisterDLL(g_hInstance, CLSID_UmiDefURL, __TEXT("UMIDefaultPathParser"), __TEXT("Both"), NULL);
  238. RegisterUtilsDLL(g_hInstance, CLSID_WbemStatusCodeText, __TEXT("WbemStatusCode"), __TEXT("Both"), NULL);
  239. RegisterUtilsDLL(g_hInstance, CLSID_WbemQuery, __TEXT("WbemQuery"), __TEXT("Both"), NULL);
  240. return NOERROR;
  241. }
  242. //***************************************************************************
  243. //
  244. // DllUnregisterServer
  245. //
  246. // Purpose: Called when it is time to remove the registry entries.
  247. //
  248. // Return: NOERROR if registration successful, error otherwise.
  249. //***************************************************************************
  250. STDAPI DllUnregisterServer(void)
  251. {
  252. UnRegisterUtilsDLL(CLSID_WbemDefPath,NULL);
  253. //postponed till Blackcomb UnRegisterDLL(CLSID_UmiDefURL,NULL);
  254. UnRegisterUtilsDLL(CLSID_WbemStatusCodeText,NULL);
  255. UnRegisterUtilsDLL(CLSID_WbemQuery,NULL);
  256. return NOERROR;
  257. }