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.

434 lines
13 KiB

  1. // dllreg.c -- autmatic registration and unregistration
  2. //
  3. #include "priv.h"
  4. #include "util.h"
  5. #include "htregmng.h"
  6. #include <advpub.h>
  7. #include <comcat.h>
  8. #include <winineti.h>
  9. #include "resource.h"
  10. #include "DllRegHelper.h"
  11. #include <mluisupp.h>
  12. #ifdef UNIX
  13. #include "unixstuff.h"
  14. #endif
  15. //=--------------------------------------------------------------------------=
  16. // miscellaneous [useful] numerical constants
  17. //=--------------------------------------------------------------------------=
  18. // the length of a guid once printed out with -'s, leading and trailing bracket,
  19. // plus 1 for NULL
  20. //
  21. #define GUID_STR_LEN 40
  22. //
  23. // helper macros
  24. //
  25. //#define RegCreate(hk, psz, phk) if (ERROR_SUCCESS != RegCreateKeyEx((hk), psz, 0, TEXT(""), REG_OPTION_NON_VOLATILE, KEY_READ|KEY_WRITE, NULL, (phk), &dwDummy)) goto CleanUp
  26. //#define RegSetStr(hk, psz) if (ERROR_SUCCESS != RegSetValueEx((hk), NULL, 0, REG_SZ, (BYTE*)(psz), lstrlen(psz)+1)) goto CleanUp
  27. //#define RegSetStrValue(hk, pszStr, psz) if(ERROR_SUCCESS != RegSetValueEx((hk), (const char *)(pszStr), 0, REG_SZ, (BYTE*)(psz), lstrlen(psz)+1)) goto CleanUp
  28. //#define RegCloseK(hk) RegCloseKey(hk); hk = NULL
  29. #define RegOpenK(hk, psz, phk) if (ERROR_SUCCESS != RegOpenKeyEx(hk, psz, 0, KEY_READ|KEY_WRITE, phk)) return FALSE
  30. //=--------------------------------------------------------------------------=
  31. // UnregisterTypeLibrary
  32. //=--------------------------------------------------------------------------=
  33. // blows away the type library keys for a given libid.
  34. //
  35. // Parameters:
  36. // REFCLSID - [in] libid to blow away.
  37. //
  38. // Output:
  39. // BOOL - TRUE OK, FALSE bad.
  40. //
  41. // Notes:
  42. // - WARNING: this function just blows away the entire type library section,
  43. // including all localized versions of the type library. mildly anti-
  44. // social, but not killer.
  45. //
  46. BOOL UnregisterTypeLibrary
  47. (
  48. const CLSID* piidLibrary
  49. )
  50. {
  51. TCHAR szScratch[GUID_STR_LEN];
  52. HKEY hk;
  53. BOOL f;
  54. // convert the libid into a string.
  55. //
  56. SHStringFromGUID(*piidLibrary, szScratch, ARRAYSIZE(szScratch));
  57. RegOpenK(HKEY_CLASSES_ROOT, TEXT("TypeLib"), &hk);
  58. f = SHDeleteKey(hk, szScratch);
  59. RegCloseKey(hk);
  60. return f;
  61. }
  62. HRESULT SHRegisterTypeLib(void)
  63. {
  64. HRESULT hr = S_OK;
  65. ITypeLib *pTypeLib;
  66. DWORD dwPathLen;
  67. TCHAR szTmp[MAX_PATH];
  68. // Load and register our type library.
  69. //
  70. dwPathLen = GetModuleFileName(HINST_THISDLL, szTmp, ARRAYSIZE(szTmp));
  71. #ifdef UNIX
  72. dwPathLen = ConvertModuleNameToUnix( szTmp );
  73. #endif
  74. hr = LoadTypeLib(szTmp, &pTypeLib);
  75. if (SUCCEEDED(hr))
  76. {
  77. // call the unregister type library as we had some old junk that
  78. // was registered by a previous version of OleAut32, which is now causing
  79. // the current version to not work on NT...
  80. UnregisterTypeLibrary(&LIBID_SHDocVw);
  81. hr = RegisterTypeLib(pTypeLib, szTmp, NULL);
  82. if (FAILED(hr))
  83. {
  84. TraceMsg(DM_WARNING, "sccls: RegisterTypeLib failed (%x)", hr);
  85. }
  86. pTypeLib->Release();
  87. }
  88. else
  89. {
  90. TraceMsg(DM_WARNING, "sccls: LoadTypeLib failed (%x)", hr);
  91. }
  92. return hr;
  93. }
  94. //
  95. // The actual functions called
  96. //
  97. /*----------------------------------------------------------
  98. Purpose: Calls the ADVPACK entry-point which executes an inf
  99. file section.
  100. */
  101. HRESULT
  102. CallRegInstall(
  103. LPSTR pszSection,
  104. BOOL bUninstall)
  105. {
  106. HRESULT hr = E_FAIL;
  107. HINSTANCE hinstAdvPack = LoadLibrary(TEXT("ADVPACK.DLL"));
  108. if (hinstAdvPack)
  109. {
  110. REGINSTALL pfnri = (REGINSTALL)GetProcAddress(hinstAdvPack, "RegInstall");
  111. if (pfnri)
  112. {
  113. char szIEPath[MAX_PATH];
  114. STRENTRY seReg[] = {
  115. { "MSIEXPLORE", szIEPath },
  116. // These two NT-specific entries must be at the end
  117. { "25", "%SystemRoot%" },
  118. { "11", "%SystemRoot%\\system32" },
  119. };
  120. STRTABLE stReg = { ARRAYSIZE(seReg) - 2, seReg };
  121. // Get the location of iexplore from the registry
  122. if ( !EVAL(GetIEPath(szIEPath, SIZECHARS(szIEPath))) )
  123. {
  124. // Failed, just say "iexplore"
  125. #ifndef UNIX
  126. StrCpyNA(szIEPath, "iexplore.exe", ARRAYSIZE(szIEPath));
  127. #else
  128. StrCpyNA(szIEPath, "iexplorer", ARRAYSIZE(szIEPath));
  129. #endif
  130. }
  131. if (g_fRunningOnNT)
  132. {
  133. // If on NT, we want custom action for %25% %11%
  134. // so that it uses %SystemRoot% in writing the
  135. // path to the registry.
  136. stReg.cEntries += 2;
  137. }
  138. hr = pfnri(g_hinst, pszSection, &stReg);
  139. if (bUninstall)
  140. {
  141. // ADVPACK will return E_UNEXPECTED if you try to uninstall
  142. // (which does a registry restore) on an INF section that was
  143. // never installed. We uninstall sections that may never have
  144. // been installed, so ignore this error
  145. hr = ((E_UNEXPECTED == hr) ? S_OK : hr);
  146. }
  147. }
  148. else
  149. TraceMsg(TF_ERROR, "DLLREG CallRegInstall() calling GetProcAddress(hinstAdvPack, \"RegInstall\") failed");
  150. FreeLibrary(hinstAdvPack);
  151. }
  152. else
  153. TraceMsg(TF_ERROR, "DLLREG CallRegInstall() Failed to load ADVPACK.DLL");
  154. return hr;
  155. }
  156. const CATID * const c_DeskBandClasses[] =
  157. {
  158. &CLSID_QuickLinks,
  159. &CLSID_AddressBand,
  160. NULL
  161. };
  162. const CATID * const c_OldDeskBandClasses[] =
  163. {
  164. &CLSID_QuickLinksOld,
  165. NULL
  166. };
  167. const CATID * const c_InfoBandClasses[] =
  168. {
  169. &CLSID_FavBand,
  170. &CLSID_HistBand,
  171. &CLSID_ExplorerBand,
  172. NULL
  173. };
  174. void RegisterCategories(BOOL fRegister)
  175. {
  176. enum DRH_REG_MODE eRegister = fRegister ? CCR_REG : CCR_UNREG;
  177. DRH_RegisterOneCategory(&CATID_DeskBand, IDS_CATDESKBAND, c_DeskBandClasses, eRegister);
  178. DRH_RegisterOneCategory(&CATID_InfoBand, IDS_CATINFOBAND, c_InfoBandClasses, eRegister);
  179. if (fRegister)
  180. {
  181. // only nuke the implementor(s), not the category
  182. DRH_RegisterOneCategory(&CATID_DeskBand, IDS_CATDESKBAND, c_OldDeskBandClasses, CCR_UNREGIMP);
  183. }
  184. }
  185. STDAPI
  186. DllRegisterServer(void)
  187. {
  188. HRESULT hr = S_OK;
  189. HRESULT hrExternal = S_OK;
  190. TraceMsg(DM_TRACE, "DLLREG DllRegisterServer() Beginning");
  191. #ifdef DEBUG
  192. if (IsFlagSet(g_dwBreakFlags, BF_ONAPIENTER))
  193. {
  194. TraceMsg(TF_ALWAYS, "Stopping in DllRegisterServer");
  195. DEBUG_BREAK;
  196. }
  197. #endif
  198. // Delete any old registration entries, then add the new ones.
  199. // Keep ADVPACK.DLL loaded across multiple calls to RegInstall.
  200. // (The inf engine doesn't guarantee DelReg/AddReg order, that's
  201. // why we explicitly unreg and reg here.)
  202. //
  203. HINSTANCE hinstAdvPack = LoadLibrary(TEXT("ADVPACK.DLL"));
  204. hr = THR(CallRegInstall("InstallControls", FALSE));
  205. if (SUCCEEDED(hrExternal))
  206. hrExternal = hr;
  207. if (hinstAdvPack)
  208. FreeLibrary(hinstAdvPack);
  209. hr = THR(SHRegisterTypeLib());
  210. if (SUCCEEDED(hrExternal))
  211. hrExternal = hr;
  212. #ifdef UNIX
  213. hrExternal = UnixRegisterBrowserInActiveSetup();
  214. #endif /* UNIX */
  215. return hrExternal;
  216. }
  217. STDAPI DllUnregisterServer(void)
  218. {
  219. HRESULT hr;
  220. TraceMsg(DM_TRACE, "DLLREG DllUnregisterServer() Beginning");
  221. // UnInstall the registry values
  222. hr = THR(CallRegInstall("UnInstallControls", TRUE));
  223. return hr;
  224. }
  225. extern HRESULT UpgradeSettings(void);
  226. /*----------------------------------------------------------
  227. Purpose: Install/uninstall user settings
  228. Description: Note that this function has special error handling.
  229. The function will keep hrExternal with the worse error
  230. but will only stop executing util the internal error (hr)
  231. gets really bad. This is because we need the external
  232. error to catch incorrectly authored INFs but the internal
  233. error to be robust in attempting to install other INF sections
  234. even if one doesn't make it.
  235. */
  236. STDAPI DllInstall(BOOL bInstall, LPCWSTR pszCmdLine)
  237. {
  238. HRESULT hr = S_OK;
  239. HRESULT hrExternal = S_OK;
  240. HINSTANCE hinstAdvPack;
  241. if (0 == StrCmpIW(pszCmdLine, TEXTW("ForceAssoc")))
  242. {
  243. InstallIEAssociations(IEA_FORCEIE);
  244. return hr;
  245. }
  246. hinstAdvPack = LoadLibrary(TEXT("ADVPACK.DLL")); // Keep ADVPACK.DLL loaded across multiple calls to RegInstall.
  247. #ifdef DEBUG
  248. if (IsFlagSet(g_dwBreakFlags, BF_ONAPIENTER))
  249. {
  250. TraceMsg(TF_ALWAYS, "Stopping in DllInstall");
  251. DEBUG_BREAK;
  252. }
  253. #endif
  254. // Assume we're installing for integrated shell unless otherwise
  255. // noted.
  256. BOOL bIntegrated = ((WhichPlatform() == PLATFORM_INTEGRATED) ? TRUE : FALSE);
  257. TraceMsg(DM_TRACE, "DLLREG DllInstall(bInstall=%lx, pszCmdLine=\"%ls\") bIntegrated=%lx", (DWORD) bInstall, pszCmdLine, (DWORD) bIntegrated);
  258. CoInitialize(0);
  259. if (bInstall)
  260. {
  261. // Backup current associations because InstallPlatformRegItems() may overwrite.
  262. hr = THR(CallRegInstall("InstallAssociations", FALSE));
  263. if (SUCCEEDED(hrExternal))
  264. hrExternal = hr;
  265. hr = THR(CallRegInstall("InstallBrowser", FALSE));
  266. if (SUCCEEDED(hrExternal))
  267. hrExternal = hr;
  268. if (bIntegrated)
  269. {
  270. // UnInstall settings that cannot be installed with Shell Integration.
  271. // This will be a NO-OP if it wasn't installed.
  272. hr = THR(CallRegInstall("UnInstallOnlyBrowser", TRUE));
  273. if (SUCCEEDED(hrExternal))
  274. hrExternal = hr;
  275. // Install IE4 shell components too.
  276. hr = THR(CallRegInstall("InstallOnlyShell", FALSE));
  277. if (SUCCEEDED(hrExternal))
  278. hrExternal = hr;
  279. if (GetUIVersion() >= 5)
  280. {
  281. hr = THR(CallRegInstall("InstallWin2KShell", FALSE));
  282. if (SUCCEEDED(hrExternal))
  283. hrExternal = hr;
  284. }
  285. else
  286. {
  287. hr = THR(CallRegInstall("InstallPreWin2KShell", FALSE));
  288. if (SUCCEEDED(hrExternal))
  289. hrExternal = hr;
  290. }
  291. if (IsOS(OS_WHISTLERORGREATER))
  292. {
  293. hr = THR(CallRegInstall("InstallXP", FALSE));
  294. if (SUCCEEDED(hrExternal))
  295. hrExternal = hr;
  296. }
  297. }
  298. else
  299. {
  300. // UnInstall Shell Integration settings.
  301. // This will be a NO-OP if it wasn't installed.
  302. hr = THR(CallRegInstall("UnInstallOnlyShell", TRUE));
  303. if (SUCCEEDED(hrExternal))
  304. hrExternal = hr;
  305. // Install IE4 shell components too.
  306. hr = THR(CallRegInstall("InstallOnlyBrowser", FALSE));
  307. if (SUCCEEDED(hrExternal))
  308. hrExternal = hr;
  309. }
  310. UpgradeSettings();
  311. UninstallCurrentPlatformRegItems();
  312. InstallIEAssociations(IEA_NORMAL);
  313. RegisterCategories(TRUE);
  314. SHRegisterTypeLib();
  315. }
  316. else
  317. {
  318. // Uninstall browser-only or integrated-browser?
  319. UninstallPlatformRegItems(bIntegrated);
  320. // Restore previous association settings that UninstallPlatformRegItems() could
  321. // have Uninstalled.
  322. hr = THR(CallRegInstall("UnInstallAssociations", TRUE));
  323. if (SUCCEEDED(hrExternal))
  324. hrExternal = hr;
  325. // UnInstall settings that cannot be installed with Shell Integration.
  326. // This will be a NO-OP if it wasn't installed.
  327. hr = THR(CallRegInstall("UnInstallOnlyBrowser", TRUE));
  328. if (SUCCEEDED(hrExternal))
  329. hrExternal = hr;
  330. // UnInstall Shell Integration settings.
  331. // This will be a NO-OP if it wasn't installed.
  332. hr = THR(CallRegInstall("UnInstallShell", TRUE));
  333. if (SUCCEEDED(hrExternal))
  334. hrExternal = hr;
  335. hr = THR(CallRegInstall("UnInstallBrowser", TRUE));
  336. if (SUCCEEDED(hrExternal))
  337. hrExternal = hr;
  338. UnregisterTypeLibrary(&LIBID_SHDocVw);
  339. RegisterCategories(FALSE);
  340. }
  341. if (hinstAdvPack)
  342. FreeLibrary(hinstAdvPack);
  343. CoUninitialize();
  344. return hrExternal;
  345. }
  346. /*----------------------------------------------------------
  347. Purpose: Gets a registry value that is User Specifc.
  348. This will open HKEY_CURRENT_USER if it exists,
  349. otherwise it will open HKEY_LOCAL_MACHINE.
  350. Returns: DWORD containing success or error code.
  351. Cond: --
  352. */
  353. LONG OpenRegUSKey(LPCTSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
  354. {
  355. DWORD dwRet = RegOpenKeyEx(HKEY_CURRENT_USER, lpSubKey, ulOptions, samDesired, phkResult);
  356. if (ERROR_SUCCESS != dwRet)
  357. dwRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpSubKey, ulOptions, samDesired, phkResult);
  358. return dwRet;
  359. }