C#/ C ++重定向另一个进程的标准输入

时间:2018-10-29 12:29:12

标签: c# c++

我已经解决了几天这个问题,但仍然找不到解决方案。因此,我有一个.exe文件,其中包含用C ++编写的程序(我无法更改它,因此将其视为黑匣子)。它的工作非常简单:在循环中,STDIN读取一个字符串,然后STDOUT写入一个输出(仅作为服务)。当我在资源管理器中启动它时,它可以正常工作。但是,当我尝试使用另一个C#程序通过重定向的STDIN和STDOUT启动它时,出现以下错误:在STDIN中写入数据时,直到关闭STDTIN,我才能从STDOUT获取输出(这是不可接受的,因为我需要一遍又一遍地使用此过程g)。这是代码:

ProcessStartInfo startInfo = new ProcessStartInfo(@"c:\Dir\black_box.exe");
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.WorkingDirectory = @"c:\Dir";

// Launch process
var proc = Process.Start(startInfo);

// Write data to stdin
proc.StandardInput.WriteLine("Blah Blah Blah");

// This is the way it should work. But it does not.
string result = proc.StandardOutput.ReadToEnd();
// Other versions of Read() does not work either

同时STDERR正常工作。

0 个答案:

没有答案
相关问题