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.

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