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.

301 lines
13 KiB

  1. //
  2. // DESKTOP.CPP
  3. //
  4. #include "precomp.h"
  5. // prototype declarations
  6. static BOOL ImportADTInfoHelper(LPCTSTR pcszInsFile, LPCTSTR pcszDeskWorkDir, LPCTSTR pcszDeskInf, BOOL fImportADT);
  7. static BOOL ImportDesktopComps(LPCTSTR pcszInsFile, LPCTSTR pcszDeskWorkDir, LPCTSTR pcszDeskInf, BOOL fImportADTComps);
  8. static BOOL ImportWallpaperInfo(LPCTSTR pcszInsFile, LPCTSTR pcszWallpaperWorkDir, BOOL fImportWallpaper);
  9. static BOOL RunningOnWin98();
  10. BOOL WINAPI ImportADTInfoA(LPCSTR pcszInsFile, LPCSTR pcszDeskWorkDir, LPCSTR pcszDeskInf, BOOL fImportADT)
  11. {
  12. USES_CONVERSION;
  13. return ImportADTInfoHelper(A2CT(pcszInsFile), A2CT(pcszDeskWorkDir), A2CT(pcszDeskInf), fImportADT);
  14. }
  15. BOOL WINAPI ImportADTInfoW(LPCWSTR pcwszInsFile, LPCWSTR pcwszDeskWorkDir, LPCWSTR pcwszDeskInf, BOOL fImportADT)
  16. {
  17. USES_CONVERSION;
  18. return ImportADTInfoHelper(W2CT((LPWSTR)pcwszInsFile), W2CT((LPWSTR)pcwszDeskWorkDir), W2CT((LPWSTR)pcwszDeskInf), fImportADT);
  19. }
  20. BOOL WINAPI ShowDeskCpl(VOID)
  21. {
  22. BOOL bRet = FALSE;
  23. HKEY hkPol;
  24. DWORD dwOldScrSav = 0, dwOldAppearance = 0, dwOldSettings = 0;
  25. // display desk.cpl (right-click->properties on the desktop) but hide the ScreenSaver, Appearance and Settings
  26. // tabs by setting their corresponding reg values to 1
  27. if (RegCreateKeyEx(HKEY_CURRENT_USER, REGSTR_PATH_POLICIES TEXT("\\") REGSTR_KEY_SYSTEM, 0, NULL,
  28. REG_OPTION_NON_VOLATILE, KEY_DEFAULT_ACCESS, NULL, &hkPol, NULL) == ERROR_SUCCESS)
  29. {
  30. DWORD dwData = 1;
  31. DWORD cbSize;
  32. // save the old values before setting them to 1
  33. cbSize = sizeof(dwOldScrSav);
  34. RegQueryValueEx(hkPol, REGSTR_VAL_DISPCPL_NOSCRSAVPAGE, NULL, NULL, (LPBYTE) &dwOldScrSav, &cbSize);
  35. RegSetValueEx(hkPol, REGSTR_VAL_DISPCPL_NOSCRSAVPAGE, 0, REG_DWORD, (CONST BYTE *) &dwData, sizeof(dwData));
  36. cbSize = sizeof(dwOldAppearance);
  37. RegQueryValueEx(hkPol, REGSTR_VAL_DISPCPL_NOAPPEARANCEPAGE, NULL, NULL, (LPBYTE) &dwOldAppearance, &cbSize);
  38. RegSetValueEx(hkPol, REGSTR_VAL_DISPCPL_NOAPPEARANCEPAGE, 0, REG_DWORD, (CONST BYTE *) &dwData, sizeof(dwData));
  39. cbSize = sizeof(dwOldSettings);
  40. RegQueryValueEx(hkPol, REGSTR_VAL_DISPCPL_NOSETTINGSPAGE, NULL, NULL, (LPBYTE) &dwOldSettings, &cbSize);
  41. // if we are running on Win98, because of a bug in desk.cpl, if all the restrictions for the tabs in Display properties
  42. // are set to 1, the Web tab doesn't show up. Workaround for this bug is to not set the SettingsPage to 1.
  43. if (!IsOS(OS_MEMPHIS))
  44. RegSetValueEx(hkPol, REGSTR_VAL_DISPCPL_NOSETTINGSPAGE, 0, REG_DWORD, (CONST BYTE *) &dwData, sizeof(dwData));
  45. RegCloseKey(hkPol);
  46. }
  47. bRet = RunAndWaitA("rundll32.exe shell32.dll,Control_RunDLL desk.cpl", NULL, SW_SHOW);
  48. // restore the original values
  49. if (RegCreateKeyEx(HKEY_CURRENT_USER, REGSTR_PATH_POLICIES TEXT("\\") REGSTR_KEY_SYSTEM, 0, NULL,
  50. REG_OPTION_NON_VOLATILE, KEY_DEFAULT_ACCESS, NULL, &hkPol, NULL) == ERROR_SUCCESS)
  51. {
  52. RegSetValueEx(hkPol, REGSTR_VAL_DISPCPL_NOSCRSAVPAGE, 0, REG_DWORD,
  53. (CONST BYTE *) &dwOldScrSav, sizeof(dwOldScrSav));
  54. RegSetValueEx(hkPol, REGSTR_VAL_DISPCPL_NOAPPEARANCEPAGE, 0, REG_DWORD,
  55. (CONST BYTE *) &dwOldAppearance, sizeof(dwOldAppearance));
  56. RegSetValueEx(hkPol, REGSTR_VAL_DISPCPL_NOSETTINGSPAGE, 0, REG_DWORD,
  57. (CONST BYTE *) &dwOldSettings, sizeof(dwOldSettings));
  58. RegCloseKey(hkPol);
  59. }
  60. return bRet;
  61. }
  62. static BOOL ImportADTInfoHelper(LPCTSTR pcszInsFile, LPCTSTR pcszDeskWorkDir, LPCTSTR pcszDeskInf, BOOL fImportADT)
  63. {
  64. BOOL bRet = TRUE;
  65. if (pcszInsFile == NULL || pcszDeskWorkDir == NULL || pcszDeskInf == NULL)
  66. return FALSE;
  67. bRet = ImportDesktopComps(pcszInsFile, pcszDeskWorkDir, pcszDeskInf, fImportADT) && bRet;
  68. bRet = ImportWallpaperInfo(pcszInsFile, pcszDeskWorkDir, fImportADT) && bRet;
  69. return bRet;
  70. }
  71. static BOOL ImportDesktopComps(LPCTSTR pcszInsFile, LPCTSTR pcszDeskWorkDir, LPCTSTR pcszDeskInf, BOOL fImportADTComps)
  72. {
  73. BOOL bRet = FALSE;
  74. HKEY hkDesk;
  75. // Before processing anything, first clear out the entries in the INS file and delete work dirs
  76. // clear out the entries in the INS file that correspond to importing Active Desktop components
  77. InsWriteBool(DESKTOP_OBJ_SECT, IMPORT_DESKTOP, FALSE, pcszInsFile);
  78. InsWriteString(EXTREGINF, DESKTOP, NULL, pcszInsFile);
  79. // blow away the pcszDeskWorkDir and pcszDeskInf
  80. PathRemovePath(pcszDeskWorkDir);
  81. PathRemovePath(pcszDeskInf);
  82. if (!fImportADTComps)
  83. return TRUE;
  84. InsWriteBool(DESKTOP_OBJ_SECT, IMPORT_DESKTOP, TRUE, pcszInsFile);
  85. // Import the Active Desktop components
  86. if (RegOpenKeyEx(HKEY_CURRENT_USER, KEY_DESKTOP_COMP, 0, KEY_DEFAULT_ACCESS, &hkDesk) == ERROR_SUCCESS)
  87. {
  88. TCHAR szFullInfName[MAX_PATH];
  89. HANDLE hInf;
  90. if (PathIsFileSpec(pcszDeskInf)) // create DESKTOP.INF under pcszDeskWorkDir
  91. PathCombine(szFullInfName, pcszDeskWorkDir, pcszDeskInf);
  92. else
  93. StrCpy(szFullInfName, pcszDeskInf);
  94. // create DESKTOP.INF file
  95. if ((hInf = CreateNewFile(szFullInfName)) != INVALID_HANDLE_VALUE)
  96. {
  97. DWORD dwType, dwOldGeneralFlags, dwGeneralFlags, cbSize;
  98. TCHAR szSubKey[MAX_PATH];
  99. DWORD dwIndex, cchSize;
  100. BOOL fUpdateIns = FALSE;
  101. dwOldGeneralFlags = 0;
  102. cbSize = sizeof(dwOldGeneralFlags);
  103. RegQueryValueEx(hkDesk, GEN_FLAGS, NULL, &dwType, (LPBYTE) &dwOldGeneralFlags, &cbSize);
  104. dwGeneralFlags = dwOldGeneralFlags | RD_DIRTY;
  105. RegSetValueEx(hkDesk, GEN_FLAGS, 0, dwType, (CONST BYTE *) &dwGeneralFlags, sizeof(dwGeneralFlags));
  106. // first, write the standard goo - [Version], [DefaultInstall], etc. - to DESKTOP.INF
  107. WriteStringToFile(hInf, (LPCVOID) DESK_INF_ADD, StrLen(DESK_INF_ADD));
  108. ExportRegKey2Inf(hkDesk, TEXT("HKCU"), KEY_DESKTOP_COMP, hInf);
  109. WriteStringToFile(hInf, (LPCVOID) TEXT("\r\n"), 2);
  110. // restore the original value for GEN_FLAGS
  111. RegSetValueEx(hkDesk, GEN_FLAGS, 0, dwType, (CONST BYTE *) &dwOldGeneralFlags, sizeof(dwOldGeneralFlags));
  112. // for each desktop component that's enumerated, spit out its information to DESKTOP.INF and
  113. // if it's a local file, copy it to pcszDeskWorkDir
  114. for (dwIndex = 0, cchSize = ARRAYSIZE(szSubKey);
  115. RegEnumKeyEx(hkDesk, dwIndex, szSubKey, &cchSize, NULL, NULL, NULL, NULL) == ERROR_SUCCESS;
  116. dwIndex++, cchSize = ARRAYSIZE(szSubKey))
  117. {
  118. HKEY hkSub;
  119. if (RegOpenKeyEx(hkDesk, szSubKey, 0, KEY_DEFAULT_ACCESS, &hkSub) == ERROR_SUCCESS)
  120. {
  121. TCHAR szDeskCompFile[MAX_URL];
  122. TCHAR szFullSubkey[MAX_PATH];
  123. BOOL fRestoreSource = FALSE;
  124. // get the name of the component from SOURCE
  125. *szDeskCompFile = TEXT('\0');
  126. cbSize = sizeof(szDeskCompFile);
  127. RegQueryValueEx(hkSub, SOURCE, NULL, NULL, (LPBYTE) szDeskCompFile, &cbSize);
  128. if (PathIsLocalPath(szDeskCompFile) && PathFileExists(szDeskCompFile) && !PathIsDirectory(szDeskCompFile))
  129. {
  130. // During branding time, all the local files get copied into the
  131. // "<windows>\web" dir; so temporarily set the SOURCE in the registry
  132. // to point to this location before it's exported to DESKTOP.INF
  133. if (CopyFileToDir(szDeskCompFile, pcszDeskWorkDir))
  134. {
  135. TCHAR szNewPath[MAX_URL];
  136. // if the file is a .htm file, then copy all the IMG SRC's specified in it to pcszDeskWorkDir
  137. if (PathIsExtension(szDeskCompFile, TEXT(".htm")) || PathIsExtension(szDeskCompFile, TEXT(".html")))
  138. CopyHtmlImgs(szDeskCompFile, pcszDeskWorkDir, NULL, NULL);
  139. wnsprintf(szNewPath, ARRAYSIZE(szNewPath), TEXT("%%25%%\\Web\\%s"), PathFindFileName(szDeskCompFile));
  140. fRestoreSource = TRUE;
  141. RegSetValueEx(hkSub, SOURCE, 0, REG_SZ, (CONST BYTE *)szNewPath, (DWORD)StrCbFromSz(szNewPath));
  142. RegSetValueEx(hkSub, SUBSCRIBEDURL, 0, REG_SZ, (CONST BYTE *)szNewPath, (DWORD)StrCbFromSz(szNewPath));
  143. }
  144. }
  145. // dump the registry info for this component to DESKTOP.INF
  146. wnsprintf(szFullSubkey, ARRAYSIZE(szFullSubkey), TEXT("%s\\%s"), KEY_DESKTOP_COMP, szSubKey);
  147. ExportRegKey2Inf(hkSub, TEXT("HKCU"), szFullSubkey, hInf);
  148. WriteStringToFile(hInf, (LPCVOID) TEXT("\r\n"), 2);
  149. fUpdateIns = TRUE;
  150. if (fRestoreSource)
  151. {
  152. RegSetValueEx(hkSub, SOURCE, 0, REG_SZ, (CONST BYTE *)szDeskCompFile, (DWORD)StrCbFromSz(szDeskCompFile));
  153. RegSetValueEx(hkSub, SUBSCRIBEDURL, 0, REG_SZ, (CONST BYTE *)szDeskCompFile, (DWORD)StrCbFromSz(szDeskCompFile));
  154. }
  155. RegCloseKey(hkSub);
  156. }
  157. }
  158. CloseFile(hInf);
  159. if (fUpdateIns)
  160. {
  161. TCHAR szBuf[MAX_PATH];
  162. // update the INS file
  163. wnsprintf(szBuf, ARRAYSIZE(szBuf), TEXT("*,%s,DefaultInstall"), PathFindFileName(pcszDeskInf));
  164. WritePrivateProfileString(EXTREGINF, DESKTOP, szBuf, pcszInsFile);
  165. bRet = TRUE;
  166. }
  167. else
  168. {
  169. PathRemovePath(pcszDeskWorkDir);
  170. PathRemovePath(szFullInfName);
  171. }
  172. }
  173. RegCloseKey(hkDesk);
  174. }
  175. return bRet;
  176. }
  177. static BOOL ImportWallpaperInfo(LPCTSTR pcszInsFile, LPCTSTR pcszWallpaperWorkDir, BOOL fImportWallpaper)
  178. {
  179. BOOL bRet = FALSE;
  180. HKEY hkDesk;
  181. // Before processing anything, first clear out the entries in the INS file
  182. // delete the WALLPAPER section in the INS file
  183. WritePrivateProfileString(WALLPAPER, NULL, NULL, pcszInsFile);
  184. if (!fImportWallpaper)
  185. return TRUE;
  186. // Import the wallpaper information
  187. if (RegOpenKeyEx(HKEY_CURRENT_USER, KEY_DESKTOP_GEN, 0, KEY_DEFAULT_ACCESS, &hkDesk) == ERROR_SUCCESS)
  188. {
  189. DWORD cbSize;
  190. TCHAR szWallpaperFile[MAX_PATH];
  191. *szWallpaperFile = TEXT('\0');
  192. cbSize = sizeof(szWallpaperFile);
  193. if (RegQueryValueEx(hkDesk, WALLPAPER, NULL, NULL, (LPBYTE) szWallpaperFile, &cbSize) != ERROR_SUCCESS ||
  194. *szWallpaperFile == TEXT('\0'))
  195. {
  196. // try reading the information from BACKUPWALLPAPER
  197. cbSize = sizeof(szWallpaperFile);
  198. RegQueryValueEx(hkDesk, BACKUPWALLPAPER, NULL, NULL, (LPBYTE) szWallpaperFile, &cbSize);
  199. }
  200. // During branding time, all the wallpaper files specified in the INS file will
  201. // get copied into whatever dir that's specified in the registry or if not found,
  202. // into "<windows>\web\wallpaper"
  203. // BUGBUG: even if szWallpaperFile points to a UNC or network path, we will still copy the files
  204. // to pcszWallpaperWorkDir and during branding time, would get copied into the client's machine
  205. if (CopyFileToDirEx(szWallpaperFile, pcszWallpaperWorkDir, WALLPAPER, pcszInsFile))
  206. {
  207. TCHAR szBuf[16];
  208. DWORD dwPos = 0;
  209. // if the file is a local .htm file, then copy all the IMG SRC's specified in it to pcszWallpaperWorkDir
  210. if (PathIsExtension(szWallpaperFile, TEXT(".htm")) || PathIsExtension(szWallpaperFile, TEXT(".htm")))
  211. CopyHtmlImgs(szWallpaperFile, pcszWallpaperWorkDir, WALLPAPER, pcszInsFile);
  212. cbSize = sizeof(dwPos);
  213. RegQueryValueEx(hkDesk, COMPONENTPOS, NULL, NULL, (LPBYTE) &dwPos, &cbSize);
  214. wnsprintf(szBuf, ARRAYSIZE(szBuf), TEXT("%lu"), dwPos);
  215. // update the INS file
  216. WritePrivateProfileString(DESKTOP_OBJ_SECT, OPTION, TEXT("1"), pcszInsFile);
  217. WritePrivateProfileString(WALLPAPER, COMPONENTPOS, szBuf, pcszInsFile);
  218. bRet = TRUE;
  219. }
  220. RegCloseKey(hkDesk);
  221. }
  222. return bRet;
  223. }