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)
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)