10

I made a WinForms application running on the .NET Framework 4.0. It writes on the parent console using the Console.WriteLine() method, after calling (once, at startup) the Win32 API function AttachConsole(-1).

It works flawlessly as long as I just need to show the output on screen. Unfortunately when I use a batch with a pipe redirection operator like this:

application.exe > output.txt

it just creates an empty file. Maybe there's some problem related to the actual pipe being addressed when I use AttachConsole? Why the command prompt can't catch the data and put it on the file? Does anyone know about any problems related to such a scenario?

Pedro Gaspar
  • 777
  • 8
  • 35
Diego D
  • 6,156
  • 2
  • 17
  • 30
  • This can't work. Project + Properties, Application tab, change Output type to Console Application. Your app gets a private console, also works when it isn't launched from a command prompt and doesn't interfere with it, and supports redirection. – Hans Passant Jul 17 '12 at 17:22
  • 3
    @HansPassant: A GUI app that doesn't automatically get its own console and does support redirection is actually useful. For example, I have a GUI text viewer I can use from a command-prompt: `dir bigdirectory | view`. Also, a GUI app that can write debugging output to a console or file is useful and you can fix the messy command-prompt problem with "start /wait". I'll post code to make this work in my answer shortly. – arx Jul 17 '12 at 17:36
  • The first reply posted by arx worked fine. Anyway to explain why I wanted such a behaviour, it was because I needed to send the realtime status updates to the console. When I found out later the pipe was not correctly redirected, I needed a solution without rewriting the application. The answer given was perfect. – Diego D Jul 18 '12 at 11:56

1 Answers1

15

Console.Out is initialised lazily. The first time you reference it the runtime calls GetStdHandle(STD_OUTPUT_HANDLE) to get the standard output handle. If this call occurs before the call to AttachConsole you get the handle to the file for redirection. If this call occurs afterwards you get the console output handle.

The following class fixes up the standard output and error handles. If you launch your application from a console you'll notice that any output appears after the next prompt. You can avoid this with start /wait.

using System;
using System.Runtime.InteropServices;

namespace SomeProject
{
    class GuiRedirect
    {
        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern bool AttachConsole(int dwProcessId);
        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern IntPtr GetStdHandle(StandardHandle nStdHandle);
        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern bool SetStdHandle(StandardHandle nStdHandle, IntPtr handle);
        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern FileType GetFileType(IntPtr handle);

        private enum StandardHandle : uint
        {
            Input = unchecked((uint)-10),
            Output = unchecked((uint)-11),
            Error = unchecked((uint)-12)
        }

        private enum FileType : uint
        {
            Unknown = 0x0000,
            Disk = 0x0001,
            Char = 0x0002,
            Pipe = 0x0003
        }

        private static bool IsRedirected(IntPtr handle)
        {
            FileType fileType = GetFileType(handle);

            return (fileType == FileType.Disk) || (fileType == FileType.Pipe);
        }

        public static void Redirect()
        {
            if (IsRedirected(GetStdHandle(StandardHandle.Output)))
            {
                var initialiseOut = Console.Out;
            }

            bool errorRedirected = IsRedirected(GetStdHandle(StandardHandle.Error));
            if (errorRedirected)
            {
                var initialiseError = Console.Error;
            }

            AttachConsole(-1);

            if (!errorRedirected)
                SetStdHandle(StandardHandle.Error, GetStdHandle(StandardHandle.Output));
        }
    }
}
arx
  • 16,686
  • 2
  • 44
  • 61
  • I tried it and worked as described. The sample code you posted is so well encapsulated I was able to use it without screwing the original class. Thanks – Diego D Jul 18 '12 at 11:52
  • 1
    This didn't seem to redirect Console.Error for me. – LukeN Sep 05 '12 at 08:39
  • How are you launching your app? What Windows version is it? – arx Sep 05 '12 at 13:03
  • Window 7, launched from cmd.exe with 'program.exe 1> out.txt 2>$1' – LukeN Sep 07 '12 at 00:42
  • Don't you mean `program.exe > out.txt 2>&1`? Which seems to work fine. – arx Sep 07 '12 at 12:07
  • I had to add the /B flag to the start command, but other then that it worked like a charm. I will link in some other threads to this answer where only parts of the solution were proposed :) – kkCosmo Mar 20 '17 at 10:34