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.

386 lines
12 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. // function prototypes
  17. VOID _cdecl FormatErrorMessage(TCHAR * pszMsg,DWORD cbMsg,TCHAR * pszFmt,va_list ArgList);
  18. extern VOID GetRNAErrorText(UINT uErr,CHAR * pszErrText,DWORD cbErrText);
  19. extern VOID GetMAPIErrorText(UINT uErr,CHAR * pszErrText,DWORD cbErrText);
  20. /*******************************************************************
  21. NAME: MsgBox
  22. SYNOPSIS: Displays a message box with the specified string ID
  23. ********************************************************************/
  24. int MsgBox(HWND hWnd,UINT nMsgID,UINT uIcon,UINT uButtons)
  25. {
  26. TCHAR szMsgBuf[MAX_RES_LEN+1];
  27. TCHAR szSmallBuf[SMALL_BUF_LEN+1];
  28. MLLoadShellLangString(IDS_APPNAME,szSmallBuf,sizeof(szSmallBuf));
  29. MLLoadShellLangString(nMsgID,szMsgBuf,sizeof(szMsgBuf));
  30. MessageBeep(uIcon);
  31. return (MessageBox(hWnd,szMsgBuf,szSmallBuf,uIcon | uButtons));
  32. }
  33. /*******************************************************************
  34. NAME: MsgBoxSz
  35. SYNOPSIS: Displays a message box with the specified text
  36. ********************************************************************/
  37. int MsgBoxSz(HWND hWnd,LPTSTR szText,UINT uIcon,UINT uButtons)
  38. {
  39. TCHAR szSmallBuf[SMALL_BUF_LEN+1];
  40. MLLoadShellLangString(IDS_APPNAME,szSmallBuf,sizeof(szSmallBuf));
  41. MessageBeep(uIcon);
  42. return (MessageBox(hWnd,szText,szSmallBuf,uIcon | uButtons));
  43. }
  44. /*******************************************************************
  45. NAME: MsgBoxParam
  46. SYNOPSIS: Displays a message box with the specified string ID
  47. NOTES: extra parameters are string pointers inserted into nMsgID.
  48. ********************************************************************/
  49. int _cdecl MsgBoxParam(HWND hWnd,UINT nMsgID,UINT uIcon,UINT uButtons,...)
  50. {
  51. va_list nextArg;
  52. BUFFER Msg(3*MAX_RES_LEN+1); // nice n' big for room for inserts
  53. BUFFER MsgFmt(MAX_RES_LEN+1);
  54. if (!Msg || !MsgFmt) {
  55. return MsgBox(hWnd,IDS_ERROutOfMemory,MB_ICONSTOP,MB_OK);
  56. }
  57. MLLoadShellLangString(nMsgID,MsgFmt.QueryPtr(),MsgFmt.QuerySize());
  58. va_start(nextArg, uButtons);
  59. FormatErrorMessage(Msg.QueryPtr(),Msg.QuerySize(),
  60. MsgFmt.QueryPtr(),nextArg);
  61. va_end(nextArg);
  62. return MsgBoxSz(hWnd,Msg.QueryPtr(),uIcon,uButtons);
  63. }
  64. BOOL EnableDlgItem(HWND hDlg,UINT uID,BOOL fEnable)
  65. {
  66. return EnableWindow(GetDlgItem(hDlg,uID),fEnable);
  67. }
  68. /*******************************************************************
  69. NAME: LoadSz
  70. SYNOPSIS: Loads specified string resource into buffer
  71. EXIT: returns a pointer to the passed-in buffer
  72. NOTES: If this function fails (most likely due to low
  73. memory), the returned buffer will have a leading NULL
  74. so it is generally safe to use this without checking for
  75. failure.
  76. ********************************************************************/
  77. LPTSTR LoadSz(UINT idString,LPTSTR lpszBuf,UINT cbBuf)
  78. {
  79. ASSERT(lpszBuf);
  80. // Clear the buffer and load the string
  81. if ( lpszBuf )
  82. {
  83. *lpszBuf = '\0';
  84. MLLoadString( idString, lpszBuf, cbBuf );
  85. }
  86. return lpszBuf;
  87. }
  88. /*******************************************************************
  89. NAME: FormatErrorMessage
  90. SYNOPSIS: Builds an error message by calling FormatMessage
  91. NOTES: Worker function for DisplayErrorMessage
  92. ********************************************************************/
  93. VOID _cdecl FormatErrorMessage(TCHAR * pszMsg,DWORD cbMsg,TCHAR * pszFmt,va_list ArgList)
  94. {
  95. ASSERT(pszMsg);
  96. ASSERT(pszFmt);
  97. // build the message into the pszMsg buffer
  98. DWORD dwCount = FormatMessage(FORMAT_MESSAGE_FROM_STRING,
  99. pszFmt,0,0,pszMsg,cbMsg,&ArgList);
  100. ASSERT(dwCount > 0);
  101. }
  102. /*----------------------------------------------------------
  103. Purpose: Calls the ADVPACK entry-point which executes an inf
  104. file section.
  105. */
  106. HRESULT CallRegInstall(LPSTR szSection)
  107. {
  108. HRESULT hr = E_FAIL;
  109. STRENTRY seReg[] = {
  110. #ifdef WINNT
  111. { "CHANNELBARINIT", "no" }, // channel bar off by default on NT
  112. #else
  113. { "CHANNELBARINIT", "yes" } // channel bar on by default on Win95/98
  114. #endif
  115. };
  116. STRTABLE stReg = { ARRAYSIZE(seReg), seReg };
  117. RegInstall(ghInstance, szSection, &stReg);
  118. return hr;
  119. }
  120. //
  121. // Code page to Script mapping table
  122. // Can't load MLang during setup, so, we port this table from MLANG
  123. //
  124. typedef struct tagCPTOSCRIPT{
  125. UINT uiCodePage;
  126. SCRIPT_ID sid;
  127. } CPTOSCRIPT;
  128. const CPTOSCRIPT CpToScript [] =
  129. {
  130. {1252, sidAsciiLatin},
  131. {1250, sidAsciiLatin},
  132. {1254, sidAsciiLatin},
  133. {1257, sidAsciiLatin},
  134. {1258, sidAsciiLatin},
  135. {1251, sidCyrillic },
  136. {1253, sidGreek },
  137. {1255, sidHebrew },
  138. {1256, sidArabic },
  139. {874, sidThai },
  140. {932, sidKana },
  141. {936, sidHan },
  142. {949, sidHangul },
  143. {950, sidBopomofo },
  144. {50000, sidUserDefined},
  145. };
  146. /*******************************************************************
  147. NAME: MigrateIEFontSetting
  148. SYNOPSIS: Port IE4 font setting data to IE5 script settings
  149. NOTES:
  150. ********************************************************************/
  151. VOID MigrateIEFontSetting(void)
  152. {
  153. HKEY hKeyInternational;
  154. HKEY hKeyScripts;
  155. // Open IE international setting registry key
  156. if (ERROR_SUCCESS ==
  157. RegOpenKeyEx(HKEY_CURRENT_USER, REGSTR_PATH_INTERNATIONAL, NULL, KEY_READ, &hKeyInternational))
  158. {
  159. DWORD dwIndex = 0;
  160. DWORD dwCreate = 0;
  161. TCHAR szCodePage[1024] = {0};
  162. // Open/Create scripts key
  163. if (ERROR_SUCCESS == RegCreateKeyEx(hKeyInternational, REGSTR_VAL_FONT_SCRIPTS, 0, NULL,
  164. REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,
  165. NULL, &hKeyScripts, &dwCreate))
  166. {
  167. // If scripts already exists, we're upgrading from IE5, no data porting needed
  168. if (dwCreate == REG_CREATED_NEW_KEY)
  169. {
  170. DWORD dwSize = ARRAYSIZE(szCodePage);
  171. TCHAR szFont[LF_FACESIZE];
  172. while (ERROR_SUCCESS == RegEnumKeyEx(hKeyInternational, dwIndex, szCodePage,
  173. &dwSize, NULL, NULL, NULL, NULL))
  174. {
  175. UINT uiCP = StrToInt(szCodePage);
  176. for (int i=0; i<ARRAYSIZE(CpToScript); i++)
  177. {
  178. if (uiCP == CpToScript[i].uiCodePage)
  179. {
  180. HKEY hKeyCodePage;
  181. if ( ERROR_SUCCESS == RegOpenKeyEx(hKeyInternational, szCodePage,
  182. NULL, KEY_READ, &hKeyCodePage))
  183. {
  184. HKEY hKeyScript;
  185. CHAR szScript[1024];
  186. wsprintfA(szScript, "%d", CpToScript[i].sid);
  187. // Port code page font data to script font data
  188. // If CP == 1252, we always need to use it to update Latin CP font info
  189. if ((ERROR_SUCCESS == RegCreateKeyExA(hKeyScripts, szScript, 0, NULL,
  190. REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,
  191. NULL, &hKeyScript, &dwCreate)) &&
  192. ((dwCreate == REG_CREATED_NEW_KEY) || (uiCP == 1252)))
  193. {
  194. DWORD cb = sizeof(szFont);
  195. if (ERROR_SUCCESS == RegQueryValueEx(hKeyCodePage,
  196. REGSTR_VAL_FIXED_FONT, NULL, NULL,
  197. (LPBYTE)szFont, &cb))
  198. {
  199. RegSetValueEx(hKeyScript, REGSTR_VAL_FIXED_FONT, 0,
  200. REG_SZ, (LPBYTE)szFont, cb);
  201. }
  202. cb = sizeof(szFont);
  203. if (ERROR_SUCCESS == RegQueryValueEx(hKeyCodePage,
  204. REGSTR_VAL_PROP_FONT, NULL, NULL,
  205. (LPBYTE)szFont, &cb))
  206. {
  207. RegSetValueEx(hKeyScript, REGSTR_VAL_PROP_FONT, 0,
  208. REG_SZ, (LPBYTE)szFont, cb);
  209. }
  210. RegCloseKey(hKeyScript);
  211. }
  212. RegCloseKey(hKeyCodePage);
  213. }
  214. }
  215. }
  216. dwIndex++;
  217. dwSize = ARRAYSIZE(szCodePage);
  218. }
  219. }
  220. RegCloseKey(hKeyScripts);
  221. }
  222. RegCloseKey(hKeyInternational);
  223. }
  224. }
  225. /*----------------------------------------------------------
  226. Purpose: Install/uninstall user settings
  227. */
  228. STDAPI DllInstall(BOOL bInstall, LPCWSTR pszCmdLine)
  229. {
  230. #ifdef DEBUG
  231. if (IsFlagSet(g_dwBreakFlags, BF_ONAPIENTER))
  232. {
  233. TraceMsg(TF_ALWAYS, "Stopping in DllInstall");
  234. DEBUG_BREAK;
  235. }
  236. #endif
  237. if (bInstall)
  238. {
  239. //
  240. // We use to delete the whole key here - that doesn't work anymore
  241. // because other people write to this key and we don't want
  242. // to crush them. If you need to explicity delete a value
  243. // add it to ao_2 value
  244. // CallRegInstall("UnregDll");
  245. CallRegInstall("RegDll");
  246. // If we also have the integrated shell installed, throw in the options
  247. // related to the Integrated Shell.
  248. if (WhichPlatform() == PLATFORM_INTEGRATED)
  249. CallRegInstall("RegDll.IntegratedShell");
  250. // NT5's new shell has special reg key settings
  251. if (GetUIVersion() >= 5)
  252. CallRegInstall("RegDll.NT5");
  253. // Run Whistler-specific settings.
  254. if (IsOS(OS_WHISTLERORGREATER))
  255. {
  256. CallRegInstall("RegDll.Whistler");
  257. }
  258. // Port IE4 code page font setting
  259. MigrateIEFontSetting();
  260. }
  261. else
  262. {
  263. CallRegInstall("UnregDll");
  264. }
  265. return S_OK;
  266. }
  267. #define REGSTR_CCS_CONTROL_WINDOWS REGSTR_PATH_CURRENT_CONTROL_SET TEXT("\\WINDOWS")
  268. #define CSDVERSION TEXT("CSDVersion")
  269. BOOL IsNTSPx(BOOL fEqualOrGreater, UINT uMajorVer, UINT uSPVer)
  270. {
  271. HKEY hKey;
  272. DWORD dwSPVersion;
  273. DWORD dwSize;
  274. BOOL fResult = FALSE;
  275. OSVERSIONINFO VerInfo;
  276. VerInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  277. GetVersionEx(&VerInfo);
  278. // make sure we're on NT4 or greater (or specifically NT4, if required)
  279. if (VER_PLATFORM_WIN32_NT != VerInfo.dwPlatformId ||
  280. (!fEqualOrGreater && VerInfo.dwMajorVersion != uMajorVer) ||
  281. (fEqualOrGreater && VerInfo.dwMajorVersion < uMajorVer))
  282. return FALSE;
  283. if (fEqualOrGreater && VerInfo.dwMajorVersion > uMajorVer)
  284. return TRUE;
  285. // check for installed SP
  286. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, REGSTR_CCS_CONTROL_WINDOWS, 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
  287. {
  288. dwSize = sizeof(dwSPVersion);
  289. if (RegQueryValueEx(hKey, CSDVERSION, NULL, NULL, (unsigned char*)&dwSPVersion, &dwSize) == ERROR_SUCCESS)
  290. {
  291. dwSPVersion = dwSPVersion >> 8;
  292. }
  293. RegCloseKey(hKey);
  294. if (fEqualOrGreater)
  295. fResult = (dwSPVersion >= uSPVer ? TRUE : FALSE);
  296. else
  297. fResult = (dwSPVersion == uSPVer ? TRUE : FALSE);
  298. }
  299. return fResult;
  300. }