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.

341 lines
8.3 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 "cyyports.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. }