AttachConsole() shows data on pipe but the > operator doesn't correctly redirect to file

前端 未结 1 1045
暖寄归人
暖寄归人 2020-12-17 23:31

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 s

相关标签:
1条回答
  • 2020-12-17 23:49

    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));
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题