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.

499 lines
16 KiB

  1. //* Microsoft Windows **
  2. //* Copyright(c) Microsoft Corp., 1994 **
  3. //*********************************************************************
  4. //
  5. // UTIL.C - common utility functions
  6. //
  7. // HISTORY:
  8. //
  9. // 12/21/94 jeremys Created.
  10. //
  11. #include "inetcplp.h"
  12. #include <advpub.h> // For REGINSTALL
  13. #include <mluisupp.h>
  14. #include "brutil.h"
  15. #include <mlang.h>
  16. #include <inetreg.h>
  17. // function prototypes
  18. VOID _cdecl FormatErrorMessage(TCHAR * pszMsg,DWORD cbMsg,TCHAR * pszFmt,va_list ArgList);
  19. extern VOID GetRNAErrorText(UINT uErr,CHAR * pszErrText,DWORD cbErrText);
  20. extern VOID GetMAPIErrorText(UINT uErr,CHAR * pszErrText,DWORD cbErrText);
  21. /*******************************************************************
  22. NAME: MsgBox
  23. SYNOPSIS: Displays a message box with the specified string ID
  24. ********************************************************************/
  25. int MsgBox(HWND hWnd,UINT nMsgID,UINT uIcon,UINT uButtons)
  26. {
  27. TCHAR szMsgBuf[MAX_RES_LEN+1];
  28. TCHAR szSmallBuf[SMALL_BUF_LEN+1];
  29. MLLoadShellLangString(IDS_APPNAME,szSmallBuf,sizeof(szSmallBuf));
  30. MLLoadShellLangString(nMsgID,szMsgBuf,sizeof(szMsgBuf));
  31. MessageBeep(uIcon);
  32. return (MessageBox(hWnd,szMsgBuf,szSmallBuf,uIcon | uButtons));
  33. }
  34. /*******************************************************************
  35. NAME: MsgBoxSz
  36. SYNOPSIS: Displays a message box with the specified text
  37. ********************************************************************/
  38. int MsgBoxSz(HWND hWnd,LPTSTR szText,UINT uIcon,UINT uButtons)
  39. {
  40. TCHAR szSmallBuf[SMALL_BUF_LEN+1];
  41. MLLoadShellLangString(IDS_APPNAME,szSmallBuf,sizeof(szSmallBuf));
  42. MessageBeep(uIcon);
  43. return (MessageBox(hWnd,szText,szSmallBuf,uIcon | uButtons));
  44. }
  45. /*******************************************************************
  46. NAME: MsgBoxParam
  47. SYNOPSIS: Displays a message box with the specified string ID
  48. NOTES: extra parameters are string pointers inserted into nMsgID.
  49. ********************************************************************/
  50. int _cdecl MsgBoxParam(HWND hWnd,UINT nMsgID,UINT uIcon,UINT uButtons,...)
  51. {
  52. va_list nextArg;
  53. BUFFER Msg(3*MAX_RES_LEN+1); // nice n' big for room for inserts
  54. BUFFER MsgFmt(MAX_RES_LEN+1);
  55. if (!Msg || !MsgFmt) {
  56. return MsgBox(hWnd,IDS_ERROutOfMemory,MB_ICONSTOP,MB_OK);
  57. }
  58. MLLoadShellLangString(nMsgID,MsgFmt.QueryPtr(),MsgFmt.QuerySize());
  59. va_start(nextArg, uButtons);
  60. FormatErrorMessage(Msg.QueryPtr(),Msg.QuerySize(),
  61. MsgFmt.QueryPtr(),nextArg);
  62. va_end(nextArg);
  63. return MsgBoxSz(hWnd,Msg.QueryPtr(),uIcon,uButtons);
  64. }
  65. BOOL EnableDlgItem(HWND hDlg,UINT uID,BOOL fEnable)
  66. {
  67. return EnableWindow(GetDlgItem(hDlg,uID),fEnable);
  68. }
  69. /*******************************************************************
  70. NAME: LoadSz
  71. SYNOPSIS: Loads specified string resource into buffer
  72. EXIT: returns a pointer to the passed-in buffer
  73. NOTES: If this function fails (most likely due to low
  74. memory), the returned buffer will have a leading NULL
  75. so it is generally safe to use this without checking for
  76. failure.
  77. ********************************************************************/
  78. LPTSTR LoadSz(UINT idString,LPTSTR lpszBuf,UINT cbBuf)
  79. {
  80. ASSERT(lpszBuf);
  81. // Clear the buffer and load the string
  82. if ( lpszBuf )
  83. {
  84. *lpszBuf = '\0';
  85. MLLoadString( idString, lpszBuf, cbBuf );
  86. }
  87. return lpszBuf;
  88. }
  89. /*******************************************************************
  90. NAME: FormatErrorMessage
  91. SYNOPSIS: Builds an error message by calling FormatMessage
  92. NOTES: Worker function for DisplayErrorMessage
  93. ********************************************************************/
  94. VOID _cdecl FormatErrorMessage(TCHAR * pszMsg,DWORD cbMsg,TCHAR * pszFmt,va_list ArgList)
  95. {
  96. ASSERT(pszMsg);
  97. ASSERT(pszFmt);
  98. // build the message into the pszMsg buffer
  99. DWORD dwCount = FormatMessage(FORMAT_MESSAGE_FROM_STRING,
  100. pszFmt,0,0,pszMsg,cbMsg,&ArgList);
  101. ASSERT(dwCount > 0);
  102. }
  103. /*----------------------------------------------------------
  104. Purpose: Calls the ADVPACK entry-point which executes an inf
  105. file section.
  106. */
  107. HRESULT CallRegInstall(LPSTR szSection)
  108. {
  109. HRESULT hr = E_FAIL;
  110. STRENTRY seReg[] = {
  111. #ifdef WINNT
  112. { "CHANNELBARINIT", "no" }, // channel bar off by default on NT
  113. #else
  114. { "CHANNELBARINIT", "yes" } // channel bar on by default on Win95/98
  115. #endif
  116. };
  117. STRTABLE stReg = { ARRAYSIZE(seReg), seReg };
  118. RegInstall(ghInstance, szSection, &stReg);
  119. return hr;
  120. }
  121. //
  122. // Code page to Script mapping table
  123. // Can't load MLang during setup, so, we port this table from MLANG
  124. //
  125. typedef struct tagCPTOSCRIPT{
  126. UINT uiCodePage;
  127. SCRIPT_ID sid;
  128. } CPTOSCRIPT;
  129. const CPTOSCRIPT CpToScript [] =
  130. {
  131. {1252, sidAsciiLatin},
  132. {1250, sidAsciiLatin},
  133. {1254, sidAsciiLatin},
  134. {1257, sidAsciiLatin},
  135. {1258, sidAsciiLatin},
  136. {1251, sidCyrillic },
  137. {1253, sidGreek },
  138. {1255, sidHebrew },
  139. {1256, sidArabic },
  140. {874, sidThai },
  141. {932, sidKana },
  142. {936, sidHan },
  143. {949, sidHangul },
  144. {950, sidBopomofo },
  145. {50000, sidUserDefined},
  146. };
  147. /*******************************************************************
  148. NAME: MigrateIEFontSetting
  149. SYNOPSIS: Port IE4 font setting data to IE5 script settings
  150. NOTES:
  151. ********************************************************************/
  152. VOID MigrateIEFontSetting(void)
  153. {
  154. HKEY hKeyInternational;
  155. HKEY hKeyScripts;
  156. // Open IE international setting registry key
  157. if (ERROR_SUCCESS ==
  158. RegOpenKeyEx(HKEY_CURRENT_USER, REGSTR_PATH_INTERNATIONAL, NULL, KEY_READ, &hKeyInternational))
  159. {
  160. DWORD dwIndex = 0;
  161. DWORD dwCreate = 0;
  162. TCHAR szCodePage[1024] = {0};
  163. // Open/Create scripts key
  164. if (ERROR_SUCCESS == RegCreateKeyEx(hKeyInternational, REGSTR_VAL_FONT_SCRIPTS, 0, NULL,
  165. REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,
  166. NULL, &hKeyScripts, &dwCreate))
  167. {
  168. // If scripts already exists, we're upgrading from IE5, no data porting needed
  169. if (dwCreate == REG_CREATED_NEW_KEY)
  170. {
  171. DWORD dwSize = ARRAYSIZE(szCodePage);
  172. TCHAR szFont[LF_FACESIZE];
  173. while (ERROR_SUCCESS == RegEnumKeyEx(hKeyInternational, dwIndex, szCodePage,
  174. &dwSize, NULL, NULL, NULL, NULL))
  175. {
  176. UINT uiCP = StrToInt(szCodePage);
  177. for (int i=0; i<ARRAYSIZE(CpToScript); i++)
  178. {
  179. if (uiCP == CpToScript[i].uiCodePage)
  180. {
  181. HKEY hKeyCodePage;
  182. if ( ERROR_SUCCESS == RegOpenKeyEx(hKeyInternational, szCodePage,
  183. NULL, KEY_READ, &hKeyCodePage))
  184. {
  185. HKEY hKeyScript;
  186. CHAR szScript[1024];
  187. wsprintfA(szScript, "%d", CpToScript[i].sid);
  188. // Port code page font data to script font data
  189. // If CP == 1252, we always need to use it to update Latin CP font info
  190. if ((ERROR_SUCCESS == RegCreateKeyExA(hKeyScripts, szScript, 0, NULL,
  191. REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,
  192. NULL, &hKeyScript, &dwCreate)) &&
  193. ((dwCreate == REG_CREATED_NEW_KEY) || (uiCP == 1252)))
  194. {
  195. DWORD cb = sizeof(szFont);
  196. if (ERROR_SUCCESS == RegQueryValueEx(hKeyCodePage,
  197. REGSTR_VAL_FIXED_FONT, NULL, NULL,
  198. (LPBYTE)szFont, &cb))
  199. {
  200. RegSetValueEx(hKeyScript, REGSTR_VAL_FIXED_FONT, 0,
  201. REG_SZ, (LPBYTE)szFont, cb);
  202. }
  203. cb = sizeof(szFont);
  204. if (ERROR_SUCCESS == RegQueryValueEx(hKeyCodePage,
  205. REGSTR_VAL_PROP_FONT, NULL, NULL,
  206. (LPBYTE)szFont, &cb))
  207. {
  208. RegSetValueEx(hKeyScript, REGSTR_VAL_PROP_FONT, 0,
  209. REG_SZ, (LPBYTE)szFont, cb);
  210. }
  211. RegCloseKey(hKeyScript);
  212. }
  213. RegCloseKey(hKeyCodePage);
  214. }
  215. }
  216. }
  217. dwIndex++;
  218. dwSize = ARRAYSIZE(szCodePage);
  219. }
  220. }
  221. RegCloseKey(hKeyScripts);
  222. }
  223. RegCloseKey(hKeyInternational);
  224. }
  225. }
  226. #define TSZUNATTENDEDINTRANET TSZWININETPATH TEXT("\\Unattend\\LocalIntranetSites")
  227. #define TSZUNATTENDEDTRUSTED TSZWININETPATH TEXT("\\Unattend\\TrustedSites")
  228. //Copy trusted/Intranet sites list from HKLM into ZoneMap
  229. void CopySitesList(DWORD dwZone, TCHAR *szSubKey)
  230. {
  231. HRESULT hr;
  232. HKEY hKey = NULL;
  233. IInternetSecurityManager* pInternetSecurityManager = NULL;
  234. TCHAR szValueName[32];
  235. TCHAR szUrl[128];
  236. DWORD cValueName, cbUrl;
  237. if (FAILED(CoInternetCreateSecurityManager(NULL, &pInternetSecurityManager,0)))
  238. {
  239. goto Cleanup; // no zone manager?
  240. }
  241. if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, szSubKey, 0, KEY_QUERY_VALUE, &hKey))
  242. {
  243. for (DWORD i=0; ; i++)
  244. {
  245. cValueName = sizeof(szValueName)/sizeof(szValueName[0]);
  246. cbUrl = sizeof(szUrl);
  247. hr = RegEnumValue(hKey, i, szValueName, &cValueName, NULL, NULL, (LPBYTE)szUrl, &cbUrl);
  248. if (hr == ERROR_NO_MORE_ITEMS)
  249. {
  250. break;
  251. }
  252. else if (hr == ERROR_SUCCESS)
  253. {
  254. pInternetSecurityManager->SetZoneMapping(dwZone, szUrl, SZM_CREATE);
  255. }
  256. }
  257. RegCloseKey(hKey);
  258. }
  259. Cleanup:
  260. if (pInternetSecurityManager)
  261. pInternetSecurityManager->Release();
  262. return;
  263. }
  264. #if 0
  265. //Remove sites list from ZoneMap;
  266. void ClearSitesList(DWORD dwZone)
  267. {
  268. IInternetSecurityManager* pInternetSecurityManager = NULL;
  269. IEnumString *pEnumString = NULL;
  270. LPOLESTR pOleStr;
  271. if (FAILED(CoInternetCreateSecurityManager(NULL, &pInternetSecurityManager,0)))
  272. {
  273. goto Cleanup; // no zone manager?
  274. }
  275. if (S_OK != pInternetSecurityManager->GetZoneMappings(dwZone, &pEnumString, 0))
  276. {
  277. goto Cleanup;
  278. }
  279. while (S_OK == pEnumString->Next(1, &pOleStr, NULL))
  280. {
  281. pInternetSecurityManager->SetZoneMapping(dwZone, pOleStr, SZM_DELETE);
  282. }
  283. Cleanup:
  284. if (pInternetSecurityManager)
  285. pInternetSecurityManager->Release();
  286. if (pEnumString)
  287. pEnumString->Release();
  288. return;
  289. }
  290. #endif
  291. /*----------------------------------------------------------
  292. Purpose: Install/uninstall user settings
  293. */
  294. STDAPI DllInstall(BOOL bInstall, LPCWSTR pszCmdLine)
  295. {
  296. #ifdef DEBUG
  297. if (IsFlagSet(g_dwBreakFlags, BF_ONAPIENTER))
  298. {
  299. TraceMsg(TF_ALWAYS, "Stopping in DllInstall");
  300. DEBUG_BREAK;
  301. }
  302. #endif
  303. if (bInstall)
  304. {
  305. if (pszCmdLine)
  306. {
  307. if(!StrCmpIW(pszCmdLine, L"HKCUHard"))
  308. {
  309. CallRegInstall("RegDll.HKCUHard");
  310. CopySitesList(URLZONE_TRUSTED, TSZUNATTENDEDTRUSTED);
  311. CopySitesList(URLZONE_INTRANET, TSZUNATTENDEDINTRANET);
  312. return S_OK;
  313. }
  314. if(!StrCmpIW(pszCmdLine, L"HKCUSoft"))
  315. {
  316. CallRegInstall("RegDll.HKCUSoft");
  317. return S_OK;
  318. }
  319. if(!StrCmpIW(pszCmdLine, L"HKLMHard"))
  320. {
  321. CallRegInstall("RegDll.HKLMHard");
  322. return S_OK;
  323. }
  324. if(!StrCmpIW(pszCmdLine, L"HKLMSoft"))
  325. {
  326. CallRegInstall("RegDll.HKLMSoft");
  327. return S_OK;
  328. }
  329. }
  330. //
  331. // We use to delete the whole key here - that doesn't work anymore
  332. // because other people write to this key and we don't want
  333. // to crush them. If you need to explicity delete a value
  334. // add it to ao_2 value
  335. // CallRegInstall("UnregDll");
  336. CallRegInstall("RegDll");
  337. // If we also have the integrated shell installed, throw in the options
  338. // related to the Integrated Shell.
  339. if (WhichPlatform() == PLATFORM_INTEGRATED)
  340. CallRegInstall("RegDll.IntegratedShell");
  341. // NT5's new shell has special reg key settings
  342. if (GetUIVersion() >= 5)
  343. CallRegInstall("RegDll.NT5");
  344. // Run Whistler-specific settings.
  345. if (IsOS(OS_WHISTLERORGREATER))
  346. {
  347. CallRegInstall("RegDll.Whistler");
  348. }
  349. // Port IE4 code page font setting
  350. MigrateIEFontSetting();
  351. }
  352. else
  353. {
  354. CallRegInstall("UnregDll");
  355. }
  356. return S_OK;
  357. }
  358. #define REGSTR_CCS_CONTROL_WINDOWS REGSTR_PATH_CURRENT_CONTROL_SET TEXT("\\WINDOWS")
  359. #define CSDVERSION TEXT("CSDVersion")
  360. BOOL IsNTSPx(BOOL fEqualOrGreater, UINT uMajorVer, UINT uSPVer)
  361. {
  362. HKEY hKey;
  363. DWORD dwSPVersion;
  364. DWORD dwSize;
  365. BOOL fResult = FALSE;
  366. OSVERSIONINFO VerInfo;
  367. VerInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  368. GetVersionEx(&VerInfo);
  369. // make sure we're on NT4 or greater (or specifically NT4, if required)
  370. if (VER_PLATFORM_WIN32_NT != VerInfo.dwPlatformId ||
  371. (!fEqualOrGreater && VerInfo.dwMajorVersion != uMajorVer) ||
  372. (fEqualOrGreater && VerInfo.dwMajorVersion < uMajorVer))
  373. return FALSE;
  374. if (fEqualOrGreater && VerInfo.dwMajorVersion > uMajorVer)
  375. return TRUE;
  376. // check for installed SP
  377. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, REGSTR_CCS_CONTROL_WINDOWS, 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
  378. {
  379. dwSize = sizeof(dwSPVersion);
  380. if (RegQueryValueEx(hKey, CSDVERSION, NULL, NULL, (unsigned char*)&dwSPVersion, &dwSize) == ERROR_SUCCESS)
  381. {
  382. dwSPVersion = dwSPVersion >> 8;
  383. }
  384. RegCloseKey(hKey);
  385. if (fEqualOrGreater)
  386. fResult = (dwSPVersion >= uSPVer ? TRUE : FALSE);
  387. else
  388. fResult = (dwSPVersion == uSPVer ? TRUE : FALSE);
  389. }
  390. return fResult;
  391. }