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.

267 lines
5.1 KiB

  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. Console.c
  5. Abstract:
  6. This module contains support for displaying output on the console.
  7. Author:
  8. David J. Gilman (davegi) 25-Nov-1992
  9. Environment:
  10. User Mode
  11. --*/
  12. #include <stdarg.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include "wintools.h"
  16. //
  17. // BOOL
  18. // IsConsoleHandle(
  19. // IN HANDLE ConsoleHandle
  20. // );
  21. //
  22. #define IsConsoleHandle( h ) \
  23. ((( DWORD_PTR )( h )) & 1 )
  24. //
  25. // Function Prototypes
  26. //
  27. int
  28. VConsolePrintfW(
  29. IN UINT Format,
  30. IN va_list Args
  31. );
  32. int
  33. ConsolePrintfW(
  34. IN UINT Format,
  35. IN ...
  36. )
  37. /*++
  38. Routine Description:
  39. Display a printf style resource string on the console.
  40. Arguments:
  41. Format - Supplies a resource number for a printf style format string.
  42. ... - Supplies zero or more values based on the format
  43. descrpitors supplied in Format.
  44. Return Value:
  45. int - Returns the number of characters actually written.
  46. --*/
  47. {
  48. va_list Args;
  49. int CharsOut;
  50. //
  51. // Gain access to the replacement values.
  52. //
  53. va_start( Args, Format );
  54. //
  55. // Display the string and return the number of characters displayed.
  56. //
  57. CharsOut = VConsolePrintfW( Format, Args );
  58. va_end( Args );
  59. return CharsOut;
  60. }
  61. int
  62. VConsolePrintfW(
  63. IN UINT Format,
  64. IN va_list Args
  65. )
  66. /*++
  67. Routine Description:
  68. Display a printf style resource string on the console.
  69. Arguments:
  70. Format - Supplies a resource number for a printf style format string.
  71. Args - Supplies zero or more values based on the format
  72. descrpitors supplied in Format.
  73. Return Value:
  74. int - Returns the number of characters actually written.
  75. --*/
  76. {
  77. LPCWSTR FormatString;
  78. BOOL Success;
  79. WCHAR Buffer[ MAX_CHARS ];
  80. DWORD CharsIn;
  81. DWORD CharsOut;
  82. HANDLE Handle;
  83. //
  84. // Attempt to retrieve the actual string resource.
  85. //
  86. FormatString = GetString( Format );
  87. DbgPointerAssert( FormatString );
  88. if ( FormatString == NULL ) {
  89. return 0;
  90. }
  91. //
  92. // Format the supplied string with the supplied arguments.
  93. //
  94. CharsIn = vswprintf( Buffer, FormatString, Args );
  95. //
  96. // Attempt to retrieve the standard output handle.
  97. //
  98. Handle = GetStdHandle( STD_OUTPUT_HANDLE );
  99. DbgAssert( Handle != INVALID_HANDLE_VALUE );
  100. if ( Handle == INVALID_HANDLE_VALUE ) {
  101. return 0;
  102. }
  103. //
  104. // If the standard output handle is a console handle, write the string.
  105. //
  106. if ( IsConsoleHandle( Handle )) {
  107. Success = WriteConsoleW(
  108. Handle,
  109. Buffer,
  110. CharsIn,
  111. &CharsOut,
  112. NULL
  113. );
  114. } else {
  115. CHAR TmpBuffer[ MAX_CHARS ];
  116. int rc;
  117. //
  118. // davegi 11/25/92
  119. // This only exists because other tools can't handle Unicode data.
  120. //
  121. // The standard output handle is not a console handle so convert the
  122. // output to ANSI and then write it.
  123. //
  124. rc = WideCharToMultiByte(
  125. CP_ACP,
  126. 0,
  127. Buffer,
  128. CharsIn,
  129. TmpBuffer,
  130. sizeof( TmpBuffer ),
  131. NULL,
  132. NULL
  133. );
  134. DbgAssert( rc != 0 );
  135. if ( rc == 0 ) {
  136. return 0;
  137. }
  138. Success = WriteFile(
  139. Handle,
  140. TmpBuffer,
  141. CharsIn,
  142. &CharsOut,
  143. NULL
  144. );
  145. }
  146. DbgAssert( Success );
  147. DbgAssert( CharsIn == CharsOut );
  148. //
  149. // Return the number of characters written.
  150. //
  151. if ( Success ) {
  152. return CharsOut;
  153. } else {
  154. return 0;
  155. }
  156. }
  157. VOID
  158. ErrorExitW(
  159. IN UINT ExitCode,
  160. IN UINT Format,
  161. IN ...
  162. )
  163. /*++
  164. Routine Description:
  165. Display a printf style resource string on the console and the exit.
  166. Arguments:
  167. ExistCode - Supplies the exit code for the process.
  168. Format - Supplies a resource number for a printf style format string.
  169. ... - Supplies zero or more values based on the format
  170. descrpitors supplied in Format.
  171. Return Value:
  172. None.
  173. --*/
  174. {
  175. va_list Args;
  176. //
  177. // Gain access to the replacement values.
  178. //
  179. va_start( Args, Format );
  180. //
  181. // Display the string and retunr the number of characters displayed.
  182. //
  183. VConsolePrintfW( Format, Args );
  184. va_end( Args );
  185. //
  186. // Exit the process with the supplied exit code.
  187. //
  188. ExitProcess( ExitCode );
  189. }