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.

367 lines
8.9 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 "cyzports.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. int
  170. MyMessageBoxWithErr(
  171. HWND hWnd,
  172. DWORD wText,
  173. DWORD wCaption,
  174. DWORD wType,
  175. DWORD wError
  176. )
  177. {
  178. int ival;
  179. LPVOID lpMessageBuffer;
  180. FormatMessage(
  181. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  182. NULL,
  183. wError,
  184. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  185. (LPTSTR) &lpMessageBuffer,
  186. 0,
  187. NULL );
  188. ival = MyMessageBox(hWnd,wText,wCaption,wType,lpMessageBuffer);
  189. LocalFree( lpMessageBuffer ); // Free the buffer allocated by the system
  190. return ival;
  191. }
  192. void
  193. SendWinIniChange(LPTSTR lpSection)
  194. {
  195. // NOTE: We have (are) gone through several iterations of which USER
  196. // api is the correct one to use. The main problem for the Control
  197. // Panel is to avoid being HUNG if another app (top-level window)
  198. // is HUNG. Another problem is that we pass a pointer to a message
  199. // string in our address space. SendMessage will 'thunk' this properly
  200. // for each window, but PostMessage and SendNotifyMessage will not.
  201. // That finally brings us to try to use SendMessageTimeout(). 9/21/92
  202. //
  203. // Try SendNotifyMessage in build 260 or later - kills earlier builds
  204. // SendNotifyMessage ((HWND)-1, WM_WININICHANGE, 0L, (LONG)lpSection);
  205. // PostMessage ((HWND)-1, WM_WININICHANGE, 0L, (LONG)lpSection);
  206. // [stevecat] 4/4/92
  207. //
  208. // SendMessage ((HWND)-1, WM_WININICHANGE, 0L, (LPARAM)lpSection);
  209. //
  210. // NOTE: The final parameter (LPDWORD lpdwResult) must be NULL
  211. SendMessageTimeout((HWND)-1,
  212. WM_WININICHANGE,
  213. 0L,
  214. (WPARAM) lpSection,
  215. SMTO_ABORTIFHUNG,
  216. 1000,
  217. NULL);
  218. }
  219. LPTSTR
  220. strscan(LPTSTR pszString,
  221. LPTSTR pszTarget)
  222. {
  223. LPTSTR psz;
  224. if (psz = _tcsstr( pszString, pszTarget))
  225. return (psz);
  226. else
  227. return (pszString + lstrlen(pszString));
  228. }
  229. ///////////////////////////////////////////////////////////////////////////////
  230. //
  231. // StripBlanks()
  232. //
  233. // Strips leading and trailing blanks from a string.
  234. // Alters the memory where the string sits.
  235. //
  236. ///////////////////////////////////////////////////////////////////////////////
  237. void
  238. StripBlanks(LPTSTR pszString)
  239. {
  240. LPTSTR pszPosn;
  241. //
  242. // strip leading blanks
  243. //
  244. pszPosn = pszString;
  245. while (*pszPosn == TEXT(' '))
  246. pszPosn++;
  247. if (pszPosn != pszString)
  248. lstrcpy(pszString, pszPosn);
  249. //
  250. // strip trailing blanks
  251. //
  252. if ((pszPosn = pszString + lstrlen(pszString)) != pszString) {
  253. pszPosn = CharPrev(pszString, pszPosn);
  254. while (*pszPosn == TEXT(' '))
  255. pszPosn = CharPrev(pszString, pszPosn);
  256. pszPosn = CharNext(pszPosn);
  257. *pszPosn = TEXT('\0');
  258. }
  259. }
  260. BOOL ReadRegistryByte(HKEY hKey,
  261. PTCHAR valueName,
  262. PBYTE regData)
  263. {
  264. DWORD regDataType = 0;
  265. DWORD regDataSize = 0;
  266. regDataSize = sizeof(*regData);
  267. if ((ERROR_SUCCESS != RegQueryValueEx(hKey,
  268. valueName,
  269. NULL,
  270. &regDataType,
  271. regData,
  272. &regDataSize))
  273. || (regDataSize != sizeof(BYTE))
  274. || (regDataType != REG_BINARY))
  275. {
  276. //
  277. // Read was unsuccessful or not a binary value, regData is not set
  278. //
  279. return FALSE;
  280. }
  281. //
  282. // Read was a success, regData contains the value read in
  283. //
  284. return TRUE;
  285. }