AttachConsole()显示管道上的数据,但>运算符未正确重定向到文件

Die*_*ita 6 .net console winapi pipe

我在.NET Framework 4.0上运行了一个WinForms应用程序.Console.WriteLine()在调用(一次,启动时)Win32 API函数后,它使用该方法在父控制台上写入AttachConsole(-1).

只要我只需要在屏幕上显示输出,它就能完美运行.不幸的是,当我使用带有管道重定向操作符的批处理时,如下所示:

application.exe > output.txt
Run Code Online (Sandbox Code Playgroud)

它只是创建一个空文件.也许在我使用时遇到与实际管道有关的问题AttachConsole?为什么命令提示符无法捕获数据并将其放在文件中?有没有人知道与这种情况有关的任何问题?

arx*_*arx 13

Console.Out是懒洋洋地初始化.第一次引用它时,运行时调用GetStdHandle(STD_OUTPUT_HANDLE)以获取标准输出句柄.如果在调用之前发生此调用,则AttachConsole获取文件的句柄以进行重定向.如果此后发生此调用,则会获得控制台输出句柄.

以下类修复了标准输出和错误句柄.如果从控制台启动应用程序,您会注意到在下一个提示后出现任何输出.你可以避免这种情况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));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)