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.

413 lines
8.7 KiB

  1. //
  2. // Application Verifier UI
  3. // Copyright (c) Microsoft Corporation, 2001
  4. //
  5. //
  6. //
  7. // module: AVGlobal.cpp
  8. // author: DMihai
  9. // created: 02/23/2001
  10. //
  11. // Description:
  12. //
  13. //
  14. #include "stdafx.h"
  15. #include "appverif.h"
  16. #include "AVUtil.h"
  17. #include "AVGlobal.h"
  18. #ifdef _DEBUG
  19. #define new DEBUG_NEW
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23. ///////////////////////////////////////////////////////////////////////////
  24. //
  25. // Report an error using a dialog box or a console message.
  26. // The message format string is loaded from the resources.
  27. //
  28. void __cdecl AVErrorResourceFormat( UINT uIdResourceFormat,
  29. ... )
  30. {
  31. TCHAR szMessage[ 256 ];
  32. TCHAR strFormat[ 256 ];
  33. BOOL bResult;
  34. va_list prms;
  35. //
  36. // Load the format string from the resources
  37. //
  38. bResult = AVLoadString( uIdResourceFormat,
  39. strFormat,
  40. ARRAY_LENGTH( strFormat ) );
  41. ASSERT( bResult );
  42. if( bResult )
  43. {
  44. va_start (prms, uIdResourceFormat);
  45. //
  46. // Format the message in our local buffer
  47. //
  48. _vsntprintf ( szMessage,
  49. ARRAY_LENGTH( szMessage ),
  50. strFormat,
  51. prms);
  52. if( g_bCommandLineMode )
  53. {
  54. //
  55. // Command console mode
  56. //
  57. _putts( szMessage );
  58. TRACE( _T( "%s\n" ), szMessage );
  59. }
  60. else
  61. {
  62. //
  63. // GUI mode
  64. //
  65. AfxMessageBox( szMessage,
  66. MB_OK | MB_ICONSTOP );
  67. }
  68. va_end (prms);
  69. }
  70. }
  71. ///////////////////////////////////////////////////////////////////////////
  72. //
  73. // Print out a message to the console
  74. // The message string is loaded from the resources.
  75. //
  76. void __cdecl AVTPrintfResourceFormat( UINT uIdResourceFormat,
  77. ... )
  78. {
  79. TCHAR szMessage[ 256 ];
  80. TCHAR strFormat[ 256 ];
  81. BOOL bResult;
  82. va_list prms;
  83. ASSERT( g_bCommandLineMode );
  84. //
  85. // Load the format string from the resources
  86. //
  87. bResult = AVLoadString( uIdResourceFormat,
  88. strFormat,
  89. ARRAY_LENGTH( strFormat ) );
  90. ASSERT( bResult );
  91. if( bResult )
  92. {
  93. va_start (prms, uIdResourceFormat);
  94. //
  95. // Format the message in our local buffer
  96. //
  97. _vsntprintf ( szMessage,
  98. ARRAY_LENGTH( szMessage ),
  99. strFormat,
  100. prms);
  101. _putts( szMessage );
  102. va_end (prms);
  103. }
  104. }
  105. ///////////////////////////////////////////////////////////////////////////
  106. //
  107. // Print out a simple (non-formatted) message to the console
  108. // The message string is loaded from the resources.
  109. //
  110. void AVPrintStringFromResources( UINT uIdString )
  111. {
  112. TCHAR szMessage[ 256 ];
  113. ASSERT( g_bCommandLineMode );
  114. VERIFY( AVLoadString( uIdString,
  115. szMessage,
  116. ARRAY_LENGTH( szMessage ) ) );
  117. _putts( szMessage );
  118. }
  119. ///////////////////////////////////////////////////////////////////////////
  120. //
  121. // Report an error using a dialog box or a console message.
  122. // The message string is loaded from the resources.
  123. //
  124. void AVMesssageFromResource( UINT uIdString )
  125. {
  126. TCHAR szMessage[ 256 ];
  127. VERIFY( AVLoadString( uIdString,
  128. szMessage,
  129. ARRAY_LENGTH( szMessage ) ) );
  130. if( g_bCommandLineMode )
  131. {
  132. //
  133. // Command console mode
  134. //
  135. _putts( szMessage );
  136. }
  137. else
  138. {
  139. //
  140. // GUI mode
  141. //
  142. AfxMessageBox( szMessage,
  143. MB_OK | MB_ICONINFORMATION );
  144. }
  145. }
  146. ///////////////////////////////////////////////////////////////////////////
  147. //
  148. // Display a message box with a message from the resources.
  149. //
  150. INT AVMesssageBoxFromResource( UINT uIdString,
  151. UINT uMsgBoxType )
  152. {
  153. TCHAR szMessage[ 256 ];
  154. VERIFY( AVLoadString( uIdString,
  155. szMessage,
  156. ARRAY_LENGTH( szMessage ) ) );
  157. ASSERT( FALSE == g_bCommandLineMode );
  158. //
  159. // GUI mode
  160. //
  161. return AfxMessageBox( szMessage,
  162. uMsgBoxType );
  163. }
  164. ///////////////////////////////////////////////////////////////////////////
  165. //
  166. // Load a string from resources.
  167. // Return TRUE if we successfully loaded and FALSE if not.
  168. //
  169. BOOL AVLoadString( ULONG uIdResource,
  170. TCHAR *szBuffer,
  171. ULONG uBufferLength )
  172. {
  173. ULONG uLoadStringResult;
  174. if( uBufferLength < 1 )
  175. {
  176. ASSERT( FALSE );
  177. return FALSE;
  178. }
  179. uLoadStringResult = LoadString (
  180. g_hProgramModule,
  181. uIdResource,
  182. szBuffer,
  183. uBufferLength );
  184. //
  185. // We should never try to load non-existent strings.
  186. //
  187. ASSERT (uLoadStringResult > 0);
  188. return (uLoadStringResult > 0);
  189. }
  190. ///////////////////////////////////////////////////////////////////////////
  191. //
  192. // Load a string from resources.
  193. // Return TRUE if we successfully loaded and FALSE if not.
  194. //
  195. BOOL AVLoadString( ULONG uIdResource,
  196. CString &strText )
  197. {
  198. TCHAR szText[ 256 ];
  199. BOOL bSuccess;
  200. bSuccess = AVLoadString( uIdResource,
  201. szText,
  202. ARRAY_LENGTH( szText ) );
  203. if( TRUE == bSuccess )
  204. {
  205. strText = szText;
  206. }
  207. else
  208. {
  209. strText = "";
  210. }
  211. return bSuccess;
  212. }
  213. /////////////////////////////////////////////////////////////////////////////
  214. BOOL AVSetWindowText( CWnd &Wnd,
  215. ULONG uIdResourceString )
  216. {
  217. BOOL bLoaded;
  218. CString strText;
  219. //
  220. // It's safe to use CString::LoadString here because we are
  221. // in GUI mode
  222. //
  223. ASSERT( FALSE == g_bCommandLineMode );
  224. bLoaded = strText.LoadString( uIdResourceString );
  225. ASSERT( TRUE == bLoaded );
  226. Wnd.SetWindowText( strText );
  227. return ( TRUE == bLoaded );
  228. }
  229. /////////////////////////////////////////////////////////////////////////////
  230. BOOL
  231. AVRtlCharToInteger( LPCTSTR String,
  232. IN ULONG Base OPTIONAL,
  233. OUT PULONG Value )
  234. {
  235. TCHAR c, Sign;
  236. ULONG Result, Digit, Shift;
  237. while ((Sign = *String++) <= _T( ' ' )) {
  238. if (!*String) {
  239. String--;
  240. break;
  241. }
  242. }
  243. c = Sign;
  244. if (c == _T( '-' ) || c == _T( '+' ) ) {
  245. c = *String++;
  246. }
  247. if (!ARGUMENT_PRESENT( (ULONG_PTR)(Base) )) {
  248. Base = 10;
  249. Shift = 0;
  250. if (c == _T( '0' ) ) {
  251. c = *String++;
  252. if (c == _T( 'x' ) ) {
  253. Base = 16;
  254. Shift = 4;
  255. }
  256. else
  257. if (c == _T( 'o' ) ) {
  258. Base = 8;
  259. Shift = 3;
  260. }
  261. else
  262. if (c == _T( 'b' ) ) {
  263. Base = 2;
  264. Shift = 1;
  265. }
  266. else {
  267. String--;
  268. }
  269. c = *String++;
  270. }
  271. }
  272. else {
  273. switch( Base ) {
  274. case 16: Shift = 4; break;
  275. case 8: Shift = 3; break;
  276. case 2: Shift = 1; break;
  277. case 10: Shift = 0; break;
  278. default: return FALSE;
  279. }
  280. }
  281. Result = 0;
  282. while (c) {
  283. if (c >= _T( '0' ) && c <= _T( '9' ) ) {
  284. Digit = c - '0';
  285. }
  286. else
  287. if (c >= _T( 'A' ) && c <= _T( 'F' ) ) {
  288. Digit = c - 'A' + 10;
  289. }
  290. else
  291. if (c >= _T( 'a' ) && c <= _T( 'f' ) ) {
  292. Digit = c - _T( 'a' ) + 10;
  293. }
  294. else {
  295. break;
  296. }
  297. if (Digit >= Base) {
  298. break;
  299. }
  300. if (Shift == 0) {
  301. Result = (Base * Result) + Digit;
  302. }
  303. else {
  304. Result = (Result << Shift) | Digit;
  305. }
  306. c = *String++;
  307. }
  308. if (Sign == _T( '-' ) ) {
  309. Result = (ULONG)(-(LONG)Result);
  310. }
  311. *Value = Result;
  312. return TRUE;
  313. }
  314. /////////////////////////////////////////////////////////////////////////////
  315. BOOL
  316. AVWriteStringHexValueToRegistry( HKEY hKey,
  317. LPCTSTR szValueName,
  318. DWORD dwValue )
  319. {
  320. LONG lResult;
  321. TCHAR szValue[ 32 ];
  322. _stprintf(
  323. szValue,
  324. _T( "0x%08X" ),
  325. dwValue );
  326. lResult = RegSetValueEx(
  327. hKey,
  328. szValueName,
  329. 0,
  330. REG_SZ,
  331. (BYTE *)szValue,
  332. _tcslen( szValue ) * sizeof( TCHAR ) + sizeof( TCHAR ) );
  333. return ( lResult == ERROR_SUCCESS );
  334. }