Windows 如何将文件参数重定向到标准输出?(Windows 相当于`/dev/stdout`)

Jer*_*ers 14 windows pipe stdout stdin

视窗控制台:

  • 工具 A 可以将二进制数据写入文件,但无法告诉它使用标准输出。
  • 工具 B 可以从 stdin 读取二进制数据并处理其中的信息。

如何在不使用中间文件的情况下通过 B 获得 A 的输出?

换句话说:Windows 相当于/dev/stdout什么?

lor*_*nix 23

Windows 确实有一个类似的 /dev/stdout,CON:

我想它仍然有效,考虑到微软正在进行的“遗留兼容性”计划。

啊。。找到了。 Microsoft 支持提供了一个保留名称列表。您不能将这些名称命名为文件,并且它们在用作输入或输出时具有特殊含义。

您可能可以使用 CON 作为输出设备发送到标准输出。

列表:

   Name    Function
   ----    --------
   CON     Keyboard and display
   PRN     System list device, usually a parallel port
   AUX     Auxiliary device, usually a serial port
   CLOCK$  System real-time clock
   NUL     Bit-bucket device
   A:-Z:   Drive letters
   COM1    First serial communications port
   LPT1    First parallel printer port
   LPT2    Second parallel printer port
   LPT3    Third parallel printer port
   COM2    Second serial communications port
   COM3    Third serial communications port
   COM4    Fourth serial communications port
Run Code Online (Sandbox Code Playgroud)

  • 我觉得我错过了一些东西——或者也许你错过了一些东西。在 Unix 中,``/dev/tty`` 不等同于``/dev/stdout``。 Windows 的“CON”相当于“/dev/tty”,而不是“/dev/stdout”。这不是问题的答案。 (5认同)
  • 谢谢,它确实有效(即使在 Win 8.1 中)。我们使用 Unity 以批处理模式运行构建:`Unity.exe -batchmode -projectPath C:\***\Application -logFile -buildWebPlayer web -quit`。没有参数(文件名)给 `-logFile` - 它必须将输出打印到控制台,但它没有。添加 CON(即 -`-logFile CON`)后 - 它确实 :-) (3认同)
  • 答案是错误的。CON 设备显示。写入 CON 既不能重定向(如在 `program > file.txt` 中),也不能在管道中使用(如在 `program | another_program` 中将数据传递到另一个程序的标准输入)。写入 CON 的输出总是显示出来。正确答案是“Windows 没有等效的 /dev/stdout” (3认同)

use*_*686 6

Windows 没有直接等价于/dev/stdout.


这是我尝试编写一个创建命名管道的 C# 程序,它可以作为文件名提供给程序 A。需要 .NET v4。

(C# 因为编译器带有 .NET 运行时,而现在什么计算机没有 .NET?)

管道服务器.cs

using System;
using System.IO;
using System.IO.Pipes;

class PipeServer {
    static int Main(string[] args) {
        string usage = "Usage: PipeServer <name> <in | out>";
        if (args.Length != 2) {
            Console.WriteLine(usage);
            return 1;
        }

        string name = args[0];
        if (String.Compare(args[1], "in") == 0) {
            Pipe(name, PipeDirection.In);
        }
        else if (String.Compare(args[1], "out") == 0) {
            Pipe(name, PipeDirection.Out);
        }
        else {
            Console.WriteLine(usage);
            return 1;
        }
        return 0;
    }

    static void Pipe(string name, PipeDirection dir) {
        NamedPipeServerStream pipe = new NamedPipeServerStream(name, dir, 1);
        pipe.WaitForConnection();
        try {
            switch (dir) {
                case PipeDirection.In:
                    pipe.CopyTo(Console.OpenStandardOutput());
                    break;
                case PipeDirection.Out:
                    Console.OpenStandardInput().CopyTo(pipe);
                    break;
                default:
                    Console.WriteLine("unsupported direction {0}", dir);
                    return;
            }
        } catch (IOException e) {
            Console.WriteLine("error: {0}", e.Message);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

编译:

csc PipeServer.cs /r:System.Core.dll
Run Code Online (Sandbox Code Playgroud)

csc 可以在 %SystemRoot%\Microsoft.NET\Framework64\<version>\csc.exe

例如,在 32 位 Windows XP 上使用 .NET Client Profile v4.0.30319:

"C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\csc.exe" PipeServer.cs /r:System.Core.dll
Run Code Online (Sandbox Code Playgroud)

跑:

PipeServer foo in | programtwo
Run Code Online (Sandbox Code Playgroud)

在第一窗口中,并且:

programone \\.\pipe\foo
Run Code Online (Sandbox Code Playgroud)

在窗口二。


小智 5

根据grawity的回答,我创建了一个扩展版本,它允许直接启动进程而不必使用多个终端窗口。

一般用法:

PipeServer [in|out] [process name] [argument 1] [argument 2] [...]
Run Code Online (Sandbox Code Playgroud)

然后字符串“{pipe}”被重定向路径替换。

真实世界的例子:

PipeServer.exe in "C:\Keil\UV4\Uv4.exe" -b "C:\Project\Project.uvproj" -j0 -o "{pipe}"
Run Code Online (Sandbox Code Playgroud)

该命令行可以直接插入到例如 Eclipse 中,以将某个外部构建器的构建日志重定向到 StdOut。

这可能是它得到的最好的...

PipeServer [in|out] [process name] [argument 1] [argument 2] [...]
Run Code Online (Sandbox Code Playgroud)