如何解决如何伪装成终端并强制子进程始终在 Windows 上刷新其标准输出?
如何伪装成终端并强制子进程在 Windows 上刷新其标准输出?
我想重定向交互式控制台程序的标准输入和输出。
这是一个用 C 编写的示例交互式程序
#include <stdio.h>
char buf[1000];
int main(){
printf("Interactive.exe Start\n>>");
while(1){
//fflush(stdout);//seems to be critical if stdout is piped
fgets(buf,1000,stdin);
printf("Received %s>>",buf);
}
}
这是用 C# 编写的主程序。它将子进程的标准输出回显到自己的标准输出,并将自己的标准输入写入子进程。
using System;
using System.IO;
using System.Diagnostics;
class RedirectExample{
public static void Main(){
using (Process myProcess = new Process()){
myProcess.StartInfo.FileName = "Interactive.exe";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.OutputDataReceived += new DataReceivedEventHandler((sender,e) => {
if (!String.IsNullOrEmpty(e.Data)){
Console.WriteLine(e.Data);
}
});
myProcess.Start();
myProcess.BeginOutputReadLine();
StreamWriter myStreamWriter = myProcess.StandardInput;
string inputText=null;
Console.WriteLine("Type Exit to exit");
do{
inputText = Console.ReadLine();
myStreamWriter.WriteLine(inputText);
}while(!inputText.Equals("Exit"));
myProcess.Kill();
myProcess.Close();
}
}
}
C 程序在 cmd 中运行良好,无论是否调用 fflush(stdout)。但是如果它的标准输出被重定向,它在使用 fflush(stdout) 之前不会输出任何内容。 我的目标控制台程序也有同样的问题。但是我不能修改它的代码。不知道是不是C写的,调用了libc的打印函数。
在 linux 上,似乎有一个假装终端的解决方案:How to trick an application into thinking its stdout is a terminal,not a pipe。如何在 Windows 上执行此操作?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。