I'm writing a GUI for a third-party console application and I want it to capture the output of a console window and add it to a text box in the GUI. Problem is, when I try to redirect the output stream of the target process, the target process crashes with the following error message:
CTextConsoleWin32::GetLine: !GetNumberOfConsoleInputEvents
The code which triggers this error is the following:
// Called once after the application has initialized.
private void StartServer()
{
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.FileName = srcdsExeFile;
processStartInfo.UseShellExecute = false;
processStartInfo.CreateNoWindow = true;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.RedirectStandardError = true;
processStartInfo.RedirectStandardInput = true;
serverProcess = Process.Start(processStartInfo);
serverProcess.EnableRaisingEvents = true;
serverProcess.Exited += new EventHandler(Server_Exited);
serverProcess.OutputDataReceived += ServerProcess_OutputDataReceived;
serverProcess.ErrorDataReceived += ServerProcess_ErrorDataReceived;
serverProcess.BeginOutputReadLine();
serverProcess.BeginErrorReadLine();
}
private void ServerProcess_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
Console.Output.WriteLine("\n\nServer Error: " + e.Data + "\n\n");
}
private void ServerProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Console.Output.WriteLine(e.Data);
}
The above code works for a minute or so while the external application is doing its initialization, but crashes after a specific point in the initialization process.
After doing some research it turns out that the third-party console application relies on the output stream to be a console, which is why it crashes when I try to redirect it to something that isn't a console. However, trying to access the output stream without redirecting it causes another error saying I have to redirect it first.
Which brings me to my actual question:
Is it possible to read the output of a console application without redirecting the output stream?