Author: hbelusca
Date: Sun Jun 30 19:55:39 2013
New Revision: 59401

URL: http://svn.reactos.org/svn/reactos?rev=59401&view=rev
Log:
[CMD]
Add a helper function which checks whether we are going to output text to a 
console handle or to a file handle.
Use it in ConWrite and in ConPrintfPaging.

Modified:
    trunk/reactos/base/shell/cmd/console.c

Modified: trunk/reactos/base/shell/cmd/console.c
URL: 
http://svn.reactos.org/svn/reactos/trunk/reactos/base/shell/cmd/console.c?rev=59401&r1=59400&r2=59401&view=diff
==============================================================================
--- trunk/reactos/base/shell/cmd/console.c      [iso-8859-1] (original)
+++ trunk/reactos/base/shell/cmd/console.c      [iso-8859-1] Sun Jun 30 
19:55:39 2013
@@ -25,6 +25,29 @@
 UINT InputCodePage;
 UINT OutputCodePage;
 
+
+BOOL IsConsoleHandle(HANDLE hHandle)
+{
+    DWORD dwMode;
+
+    /* Check whether the handle may be that of a console... */
+    if ((GetFileType(hHandle) & FILE_TYPE_CHAR) == 0) return FALSE;
+
+    /*
+     * It may be. Perform another test... The idea comes from the
+     * MSDN description of the WriteConsole API:
+     *
+     * "WriteConsole fails if it is used with a standard handle
+     *  that is redirected to a file. If an application processes
+     *  multilingual output that can be redirected, determine whether
+     *  the output handle is a console handle (one method is to call
+     *  the GetConsoleMode function and check whether it succeeds).
+     *  If the handle is a console handle, call WriteConsole. If the
+     *  handle is not a console handle, the output is redirected and
+     *  you should call WriteFile to perform the I/O."
+     */
+    return GetConsoleMode(hHandle, &dwMode);
+}
 
 VOID ConInDisable(VOID)
 {
@@ -116,8 +139,12 @@
     DWORD dwWritten;
     HANDLE hOutput = GetStdHandle(nStdHandle);
 
-    if (WriteConsole(hOutput, str, len, &dwWritten, NULL))
-        return;
+    /* Check whether we are writing to a console and if so, write to it */
+    if (IsConsoleHandle(hOutput))
+    {
+        if (WriteConsole(hOutput, str, len, &dwWritten, NULL))
+            return;
+    }
 
     /* We're writing to a file or pipe instead of the console. Convert the
      * string from TCHARs to the desired output format, if the two differ */
@@ -220,9 +247,8 @@
     if (szFormat == NULL)
         return 0;
 
-
     //get the size of the visual screen that can be printed too
-    if (!GetConsoleScreenBufferInfo(hOutput, &csbi))
+    if (!IsConsoleHandle(hOutput) || !GetConsoleScreenBufferInfo(hOutput, 
&csbi))
     {
         // we assuming its a file handle
         ConPrintf(szFormat, arg_ptr, nStdHandle);


Reply via email to