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.

242 lines
4.0 KiB

  1. /*++
  2. Copyright (c) 1993 Microsoft Corporation
  3. Module Name:
  4. ututil.c
  5. Abstract:
  6. Miscellaneous utility functions for unitext.exe.
  7. Author:
  8. Ted Miller (tedm) 16-June-1993
  9. Revision History:
  10. --*/
  11. #include "unitext.h"
  12. #include <process.h>
  13. //
  14. // BOOL
  15. // IsConsoleHandle(
  16. // IN HANDLE ConsoleHandle
  17. // );
  18. //
  19. #define IsConsoleHandle( h ) \
  20. ((( DWORD_PTR )( h )) & 1 )
  21. VOID
  22. MsgPrintfW(
  23. IN DWORD MessageId,
  24. ...
  25. )
  26. /*++
  27. Routine Description:
  28. Print a formatted message from the applications's resources.
  29. Arguments:
  30. MessageId - supplies id of the message to print.
  31. ... - supplies arguments to be substituted in the message.
  32. Return Value:
  33. None.
  34. --*/
  35. {
  36. va_list arglist;
  37. va_start(arglist,MessageId);
  38. vMsgPrintfW(MessageId,arglist);
  39. va_end(arglist);
  40. }
  41. VOID
  42. vMsgPrintfW(
  43. IN DWORD MessageId,
  44. IN va_list arglist
  45. )
  46. /*++
  47. Routine Description:
  48. Print a formatted message from the applications's resources.
  49. Arguments:
  50. MessageId - supplies id of the message to print.
  51. arglist - supplies arguments to be substituted in the message.
  52. Return Value:
  53. None.
  54. --*/
  55. {
  56. WCHAR MessageBuffer[2048];
  57. HANDLE StdOut;
  58. DWORD WrittenCount;
  59. DWORD CharCount;
  60. CharCount = FormatMessageW(
  61. FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_FROM_SYSTEM,
  62. NULL,
  63. MessageId,
  64. 0,
  65. MessageBuffer,
  66. sizeof(MessageBuffer)/sizeof(MessageBuffer[0]),
  67. &arglist
  68. );
  69. if(!CharCount) {
  70. ErrorAbort(MSG_BAD_MSG,MessageId);
  71. }
  72. if((StdOut = GetStdHandle(STD_OUTPUT_HANDLE)) == INVALID_HANDLE_VALUE) {
  73. return;
  74. }
  75. //
  76. // If the standard output handle is a console handle, write the string.
  77. //
  78. if(IsConsoleHandle(StdOut)) {
  79. WriteConsoleW(
  80. StdOut,
  81. MessageBuffer,
  82. CharCount,
  83. &WrittenCount,
  84. NULL
  85. );
  86. } else {
  87. CHAR TmpBuffer[2048];
  88. DWORD ByteCount;
  89. ByteCount = WideCharToMultiByte(
  90. CP_OEMCP,
  91. 0,
  92. MessageBuffer,
  93. CharCount,
  94. TmpBuffer,
  95. sizeof(TmpBuffer),
  96. NULL,
  97. NULL
  98. );
  99. WriteFile(
  100. StdOut,
  101. TmpBuffer,
  102. ByteCount,
  103. &WrittenCount,
  104. NULL
  105. );
  106. }
  107. }
  108. VOID
  109. ErrorAbort(
  110. IN DWORD MessageId,
  111. ...
  112. )
  113. /*++
  114. Routine Description:
  115. Print a message and exit.
  116. Arguments:
  117. MessageId - supplies id of the message to print.
  118. ... - supplies arguments to be substituted in the message.
  119. Return Value:
  120. None.
  121. --*/
  122. {
  123. va_list arglist;
  124. va_start(arglist,MessageId);
  125. vMsgPrintfW(MessageId,arglist);
  126. va_end(arglist);
  127. exit(0);
  128. }
  129. VOID
  130. MyReadFile(
  131. IN HANDLE FileHandle,
  132. OUT PVOID Buffer,
  133. IN DWORD BytesToRead,
  134. IN LPWSTR Filename
  135. )
  136. /*++
  137. Routine Description:
  138. Read from a file and don't return if an error occurs.
  139. Arguments:
  140. FileHandle - supplies handle of open file.
  141. Buffer - supplies buffer into which data will be read.
  142. BytesToRead - supplies number of bytes to read from the file.
  143. Filename - supplies name of file being read.
  144. Return Value:
  145. None.
  146. --*/
  147. {
  148. DWORD BytesRead;
  149. BOOL b;
  150. b = ReadFile(
  151. FileHandle,
  152. Buffer,
  153. BytesToRead,
  154. &BytesRead,
  155. NULL
  156. );
  157. if(!b || (BytesRead != BytesToRead)) {
  158. ErrorAbort(MSG_READ_ERROR,Filename,GetLastError());
  159. }
  160. }