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.

451 lines
11 KiB

  1. /** FILE: util.c *********** Module Header ********************************
  2. *
  3. * Ports applet utility library routines. This file contains string,
  4. * cursor, SendWinIniChange() routines.
  5. *
  6. * History:
  7. * 15:30 on Thur 25 Apr 1991 -by- Steve Cathcart [stevecat]
  8. * Took base code from Win 3.1 source
  9. * 10:30 on Tues 04 Feb 1992 -by- Steve Cathcart [stevecat]
  10. * Updated code to latest Win 3.1 sources
  11. * 15:30 on Thur 03 May 1994 -by- Steve Cathcart [stevecat]
  12. * Increased MyMessageBox buffers, Restart dialog changes
  13. * 17:00 on Mon 18 Sep 1995 -by- Steve Cathcart [stevecat]
  14. * Changes for product update - SUR release NT v4.0
  15. * Nov 1997 -by- Doron Holan [stevecat]
  16. * Removed obsolete cpl code
  17. *
  18. * Copyright (C) 1990-1995 Microsoft Corporation
  19. *
  20. *************************************************************************/
  21. /* Notes -
  22. Global Functions:
  23. U T I L I T Y
  24. BackslashTerm () - add backslash char to path
  25. ErrMemDlg () - display Memory Error message box
  26. MyAtoi () - To convert from Unicode to ANSI string before calling atoi
  27. myatoi () - local implementation of atoi for Unicode strings
  28. MyItoa () - To convert from ANSI to Unicode string after calling itoa
  29. MyMessageBox () - display message to user, with parameters
  30. MyUltoa () - To convert from Unicode to ANSI string before calling ultoa
  31. SendWinIniChange () - broadcast system change message via USER
  32. strscan () - Find a string within another string
  33. StripBlanks () - Strip leading and trailing blanks from a string
  34. Local Functions:
  35. */
  36. //==========================================================================
  37. // Include files
  38. //==========================================================================
  39. // C Runtime
  40. #include <stddef.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <stdarg.h>
  44. // Application specific
  45. #include "ports.h"
  46. #define INT_SIZE_LENGTH 20
  47. #define LONG_SIZE_LENGTH 40
  48. LPTSTR
  49. BackslashTerm(LPTSTR pszPath)
  50. {
  51. LPTSTR pszEnd;
  52. pszEnd = pszPath + lstrlen(pszPath);
  53. //
  54. // Get the end of the source directory
  55. //
  56. switch(*CharPrev(pszPath, pszEnd)) {
  57. case TEXT('\\'):
  58. case TEXT(':'):
  59. break;
  60. default:
  61. *pszEnd++ = TEXT('\\');
  62. *pszEnd = TEXT('\0');
  63. }
  64. return pszEnd;
  65. }
  66. void
  67. ErrMemDlg(HWND hParent)
  68. {
  69. MessageBox(hParent, g_szErrMem, g_szPortsApplet,
  70. MB_OK | MB_ICONHAND | MB_SYSTEMMODAL );
  71. }
  72. ///////////////////////////////////////////////////////////////////////////////
  73. //
  74. // MyAtoi
  75. //
  76. // Desc: To convert from Unicode to ANSI string before
  77. // calling CRT atoi and atol functions.
  78. //
  79. ///////////////////////////////////////////////////////////////////////////////
  80. int
  81. MyAtoi(LPTSTR string)
  82. {
  83. CHAR szAnsi[ INT_SIZE_LENGTH ];
  84. BOOL fDefCharUsed;
  85. #ifdef UNICODE
  86. WideCharToMultiByte(CP_ACP, 0, string, INT_SIZE_LENGTH,
  87. szAnsi, INT_SIZE_LENGTH, NULL, &fDefCharUsed);
  88. return atoi(szAnsi);
  89. #else
  90. return atoi(string);
  91. #endif
  92. }
  93. int
  94. myatoi(LPTSTR pszInt)
  95. {
  96. int retval;
  97. TCHAR cSave;
  98. for (retval = 0; *pszInt; ++pszInt) {
  99. if ((cSave = (TCHAR) (*pszInt - TEXT('0'))) > (TCHAR) 9)
  100. break;
  101. retval = (int) (retval * 10 + (int) cSave);
  102. }
  103. return (retval);
  104. }
  105. ///////////////////////////////////////////////////////////////////////////////
  106. //
  107. // MyItoa
  108. //
  109. // Desc: To convert from ANSI to Unicode string after calling
  110. // CRT itoa function.
  111. //
  112. ///////////////////////////////////////////////////////////////////////////////
  113. LPTSTR
  114. MyItoa(INT value, LPTSTR string, INT radix)
  115. {
  116. CHAR szAnsi[INT_SIZE_LENGTH];
  117. #ifdef UNICODE
  118. _itoa(value, szAnsi, radix);
  119. MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szAnsi, -1,
  120. string, INT_SIZE_LENGTH );
  121. #else
  122. _itoa(value, string, radix);
  123. #endif
  124. return (string);
  125. } // end of MyItoa()
  126. LPTSTR
  127. MyUltoa(unsigned long value,
  128. LPTSTR string,
  129. INT radix)
  130. {
  131. CHAR szAnsi[ LONG_SIZE_LENGTH ];
  132. #ifdef UNICODE
  133. _ultoa(value, szAnsi, radix);
  134. MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szAnsi, -1,
  135. string, LONG_SIZE_LENGTH );
  136. #else
  137. _ultoa(value, string, radix);
  138. #endif
  139. return( string );
  140. } // end of MyUltoa()
  141. int
  142. MyMessageBox(HWND hWnd,
  143. DWORD wText,
  144. DWORD wCaption,
  145. DWORD wType,
  146. ...)
  147. {
  148. TCHAR szText[4 * PATHMAX],
  149. szCaption[2 * PATHMAX];
  150. int ival;
  151. va_list parg;
  152. va_start(parg, wType);
  153. if (wText == INITS)
  154. goto NoMem;
  155. if (!LoadString(g_hInst, wText, szCaption, CharSizeOf(szCaption)))
  156. goto NoMem;
  157. wvsprintf(szText, szCaption, parg);
  158. if (!LoadString(g_hInst, wCaption, szCaption, CharSizeOf(szCaption)))
  159. goto NoMem;
  160. if ((ival = MessageBox(hWnd, szText, szCaption, wType)) == 0)
  161. goto NoMem;
  162. va_end(parg);
  163. return ival;
  164. NoMem:
  165. va_end(parg);
  166. ErrMemDlg(hWnd);
  167. return 0;
  168. }
  169. void
  170. SendWinIniChange(LPTSTR lpSection)
  171. {
  172. // NOTE: We have (are) gone through several iterations of which USER
  173. // api is the correct one to use. The main problem for the Control
  174. // Panel is to avoid being HUNG if another app (top-level window)
  175. // is HUNG. Another problem is that we pass a pointer to a message
  176. // string in our address space. SendMessage will 'thunk' this properly
  177. // for each window, but PostMessage and SendNotifyMessage will not.
  178. // That finally brings us to try to use SendMessageTimeout(). 9/21/92
  179. //
  180. // Try SendNotifyMessage in build 260 or later - kills earlier builds
  181. // SendNotifyMessage ((HWND)-1, WM_WININICHANGE, 0L, (LONG)lpSection);
  182. // PostMessage ((HWND)-1, WM_WININICHANGE, 0L, (LONG)lpSection);
  183. // [stevecat] 4/4/92
  184. //
  185. // SendMessage ((HWND)-1, WM_WININICHANGE, 0L, (LPARAM)lpSection);
  186. //
  187. // NOTE: The final parameter (LPDWORD lpdwResult) must be NULL
  188. SendMessageTimeout((HWND)-1,
  189. WM_WININICHANGE,
  190. 0L,
  191. (WPARAM) lpSection,
  192. SMTO_ABORTIFHUNG,
  193. 1000,
  194. NULL);
  195. }
  196. LPTSTR
  197. strscan(LPTSTR pszString,
  198. LPTSTR pszTarget)
  199. {
  200. LPTSTR psz;
  201. if (psz = _tcsstr( pszString, pszTarget))
  202. return (psz);
  203. else
  204. return (pszString + lstrlen(pszString));
  205. }
  206. ///////////////////////////////////////////////////////////////////////////////
  207. //
  208. // StripBlanks()
  209. //
  210. // Strips leading and trailing blanks from a string.
  211. // Alters the memory where the string sits.
  212. //
  213. ///////////////////////////////////////////////////////////////////////////////
  214. void
  215. StripBlanks(LPTSTR pszString)
  216. {
  217. LPTSTR pszPosn;
  218. //
  219. // strip leading blanks
  220. //
  221. pszPosn = pszString;
  222. while (*pszPosn == TEXT(' '))
  223. pszPosn++;
  224. if (pszPosn != pszString)
  225. lstrcpy(pszString, pszPosn);
  226. //
  227. // strip trailing blanks
  228. //
  229. if ((pszPosn = pszString + lstrlen(pszString)) != pszString) {
  230. pszPosn = CharPrev(pszString, pszPosn);
  231. while (*pszPosn == TEXT(' '))
  232. pszPosn = CharPrev(pszString, pszPosn);
  233. pszPosn = CharNext(pszPosn);
  234. *pszPosn = TEXT('\0');
  235. }
  236. }
  237. BOOL ReadRegistryByte(HKEY hKey,
  238. PTCHAR valueName,
  239. PBYTE regData)
  240. {
  241. DWORD regDataType = 0;
  242. DWORD regDataSize = 0;
  243. regDataSize = sizeof(*regData);
  244. if ((ERROR_SUCCESS != RegQueryValueEx(hKey,
  245. valueName,
  246. NULL,
  247. &regDataType,
  248. regData,
  249. &regDataSize))
  250. || (regDataSize != sizeof(BYTE))
  251. || (regDataType != REG_BINARY))
  252. {
  253. //
  254. // Read was unsuccessful or not a binary value, regData is not set
  255. //
  256. return FALSE;
  257. }
  258. //
  259. // Read was a success, regData contains the value read in
  260. //
  261. return TRUE;
  262. }
  263. // @@BEGIN_DDKSPLIT
  264. #if 0
  265. //
  266. // Turn hourglass on or off
  267. //
  268. void HourGlass( BOOL bOn )
  269. {
  270. if( !GetSystemMetrics( SM_MOUSEPRESENT ) )
  271. ShowCursor( bOn );
  272. SetCursor( LoadCursor( NULL, bOn ? IDC_WAIT : IDC_ARROW ) );
  273. }
  274. ///////////////////////////////////////////////////////////////////////////////
  275. //
  276. // RestartDlg
  277. //
  278. // The following function is the dialog procedure for bringing up a system
  279. // restart message. This dialog is called whenever the user is advised to
  280. // reboot the system. The dialog contains an IDOK and IDCANCEL button, which
  281. // instructs the function whether to restart windows immediately.
  282. //
  283. // Parameters:
  284. //
  285. // lParam - The LOWORD portion contains an index to the resource string
  286. // used to compose the restart message. This string is inserted
  287. // before the string IDS_RESTART.
  288. //
  289. // Return Value: The usual dialog return value.
  290. //
  291. ///////////////////////////////////////////////////////////////////////////////
  292. BOOL RestartDlg( HWND hDlg, UINT message, DWORD wParam, LONG lParam )
  293. {
  294. TCHAR szMessage[ 200 ];
  295. TCHAR szTemp[ 100 ];
  296. BOOLEAN PreviousPriv;
  297. switch (message)
  298. {
  299. case WM_INITDIALOG:
  300. //
  301. // Set up the restart message
  302. //
  303. LoadString( g_hInst, LOWORD(lParam), szMessage,
  304. CharSizeOf( szMessage ) );
  305. if( LoadString( g_hInst, IDS_RESTART, szTemp, CharSizeOf( szTemp ) ) )
  306. lstrcat( szMessage, szTemp );
  307. SetDlgItemText( hDlg, RESTART_TEXT, szMessage );
  308. break;
  309. case WM_COMMAND:
  310. switch( LOWORD( wParam ) )
  311. {
  312. case IDOK:
  313. RtlAdjustPrivilege( SE_SHUTDOWN_PRIVILEGE,
  314. TRUE, FALSE, &PreviousPriv );
  315. ExitWindowsEx( EWX_REBOOT | EWX_SHUTDOWN, (DWORD) (-1) );
  316. break;
  317. case IDCANCEL:
  318. EndDialog( hDlg, 0L );
  319. break;
  320. default:
  321. return( FALSE );
  322. }
  323. return( TRUE );
  324. default:
  325. return( FALSE );
  326. }
  327. return( FALSE );
  328. }
  329. //
  330. // This does what is necessary to bring up a dialog box
  331. //
  332. int DoDialogBoxParam( int nDlg,
  333. HWND hParent,
  334. DLGPROC lpProc,
  335. DWORD dwHelpContext,
  336. DWORD dwParam)
  337. {
  338. DWORD dwSave;
  339. dwSave = g_dwContext;
  340. g_dwContext = dwHelpContext;
  341. nDlg = DialogBoxParam( g_hInst, (LPTSTR) MAKEINTRESOURCE( nDlg ),
  342. hParent, lpProc, dwParam);
  343. g_dwContext = dwSave;
  344. if( nDlg == -1 )
  345. MyMessageBox( hParent, INITS, IDS_INIT_NAME, IDOK );
  346. return( nDlg );
  347. }
  348. #endif // 0
  349. // @@END_DDKSPLIT