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.

258 lines
5.2 KiB

  1. #include "precomp.h"
  2. #include "AVUtil.h"
  3. VOID
  4. DebugPrintf(
  5. LPCSTR pszFmt,
  6. ...
  7. )
  8. {
  9. char szT[1024];
  10. va_list vaArgList;
  11. va_start(vaArgList, pszFmt);
  12. _vsnprintf(szT, 1023, pszFmt, vaArgList);
  13. // make sure we have a '\n' at the end of the string
  14. int len = strlen(szT);
  15. if (szT[len - 1] != '\n') {
  16. strcpy(szT + len, "\n");
  17. }
  18. OutputDebugStringA(szT);
  19. va_end(vaArgList);
  20. }
  21. ///////////////////////////////////////////////////////////////////////////
  22. //
  23. // Report an error using a dialog box or a console message.
  24. // The message format string is loaded from the resources.
  25. //
  26. void __cdecl
  27. AVErrorResourceFormat(
  28. UINT uIdResourceFormat,
  29. ...
  30. )
  31. {
  32. WCHAR szMessage[2048];
  33. WCHAR strFormat[2048];
  34. BOOL bResult;
  35. va_list prms;
  36. //
  37. // Load the format string from the resources
  38. //
  39. bResult = AVLoadString(uIdResourceFormat,
  40. strFormat,
  41. ARRAY_LENGTH(strFormat));
  42. ASSERT(bResult);
  43. if (bResult) {
  44. va_start (prms, uIdResourceFormat);
  45. //
  46. // Format the message in our local buffer
  47. //
  48. _vsnwprintf(szMessage,
  49. ARRAY_LENGTH(szMessage),
  50. strFormat,
  51. prms);
  52. if (g_bConsoleMode) {
  53. printf("Error: %ls\n", szMessage);
  54. } else {
  55. //
  56. // GUI mode
  57. //
  58. MessageBox(NULL, szMessage, L"Application Verifier",
  59. MB_OK | MB_ICONSTOP );
  60. }
  61. va_end(prms);
  62. }
  63. }
  64. ///////////////////////////////////////////////////////////////////////////
  65. //
  66. // Load a string from resources.
  67. // Return TRUE if we successfully loaded and FALSE if not.
  68. //
  69. BOOL
  70. AVLoadString(
  71. ULONG uIdResource,
  72. WCHAR* szBuffer,
  73. ULONG uBufferLength
  74. )
  75. {
  76. ULONG uLoadStringResult;
  77. if (uBufferLength < 1) {
  78. ASSERT(FALSE);
  79. return FALSE;
  80. }
  81. uLoadStringResult = LoadStringW(g_hInstance,
  82. uIdResource,
  83. szBuffer,
  84. uBufferLength);
  85. return (uLoadStringResult > 0);
  86. }
  87. ///////////////////////////////////////////////////////////////////////////
  88. //
  89. // Load a string from resources.
  90. // Return TRUE if we successfully loaded and FALSE if not.
  91. //
  92. BOOL
  93. AVLoadString(
  94. ULONG uIdResource,
  95. wstring& strText
  96. )
  97. {
  98. WCHAR szText[4096];
  99. BOOL bSuccess;
  100. bSuccess = AVLoadString(uIdResource,
  101. szText,
  102. ARRAY_LENGTH(szText));
  103. if (bSuccess) {
  104. strText = szText;
  105. } else {
  106. strText = L"";
  107. }
  108. return bSuccess;
  109. }
  110. /////////////////////////////////////////////////////////////////////////////
  111. BOOL
  112. AVRtlCharToInteger(
  113. IN LPCWSTR String,
  114. IN ULONG Base OPTIONAL,
  115. OUT PULONG Value
  116. )
  117. {
  118. WCHAR c, Sign;
  119. ULONG Result, Digit, Shift;
  120. while ((Sign = *String++) <= L' ') {
  121. if (!*String) {
  122. String--;
  123. break;
  124. }
  125. }
  126. c = Sign;
  127. if (c == L'-' || c == L'+') {
  128. c = *String++;
  129. }
  130. if ( Base == 0 ) {
  131. Base = 10;
  132. Shift = 0;
  133. if (c == L'0' ) {
  134. c = *String++;
  135. if (c == L'x') {
  136. Base = 16;
  137. Shift = 4;
  138. } else
  139. if (c == L'o') {
  140. Base = 8;
  141. Shift = 3;
  142. } else
  143. if (c == L'b') {
  144. Base = 2;
  145. Shift = 1;
  146. } else {
  147. String--;
  148. }
  149. c = *String++;
  150. }
  151. } else {
  152. switch ( Base ) {
  153. case 16: Shift = 4; break;
  154. case 8: Shift = 3; break;
  155. case 2: Shift = 1; break;
  156. case 10: Shift = 0; break;
  157. default: return FALSE;
  158. }
  159. }
  160. Result = 0;
  161. while (c) {
  162. if (c >= _T( '0' ) && c <= _T( '9' ) ) {
  163. Digit = c - '0';
  164. } else
  165. if (c >= _T( 'A' ) && c <= _T( 'F' ) ) {
  166. Digit = c - 'A' + 10;
  167. } else
  168. if (c >= _T( 'a' ) && c <= _T( 'f' ) ) {
  169. Digit = c - _T( 'a' ) + 10;
  170. } else {
  171. break;
  172. }
  173. if (Digit >= Base) {
  174. break;
  175. }
  176. if (Shift == 0) {
  177. Result = (Base * Result) + Digit;
  178. } else {
  179. Result = (Result << Shift) | Digit;
  180. }
  181. c = *String++;
  182. }
  183. if (Sign == _T('-')) {
  184. Result = (ULONG)(-(LONG)Result);
  185. }
  186. *Value = Result;
  187. return TRUE;
  188. }
  189. /////////////////////////////////////////////////////////////////////////////
  190. BOOL
  191. AVWriteStringHexValueToRegistry(
  192. HKEY hKey,
  193. LPCWSTR szValueName,
  194. DWORD dwValue
  195. )
  196. {
  197. LONG lResult;
  198. WCHAR szValue[ 32 ];
  199. swprintf(szValue,
  200. L"0x%08X",
  201. dwValue);
  202. lResult = RegSetValueEx(hKey,
  203. szValueName,
  204. 0,
  205. REG_SZ,
  206. (BYTE *)szValue,
  207. wcslen(szValue) * sizeof(WCHAR) + sizeof(WCHAR));
  208. return (lResult == ERROR_SUCCESS);
  209. }