Reading redirected stdout from a spawned process

G

Guest

Hello,

I have a C# application in which I start another process which produces
output to stdout and stderr. In fact, that process is the uSoft VS2005 C/C++
compiler itself! I would like to capture the results of the compile and
display them in a RichTextBox. The problem I'm having is that when I
intentionally introduce an error in the C code I'm compiling, I can't read
the error output in my C# program. I've tried redirecting both stdout and
stderr (not at the same time) but I can never capture the error messages,
only the start of the compile itself. If I simply open a command window and
manually do th compile from there, I see the error messages fine. Below is
an example of a simplified version of the C# code I'm using. This code uses
stderr but I've also tried it for stdout (the C/C++ compiler actually appears
to output its code syntax error messages to stdout rather than stderr):

Process compile = new Process();
compile.StartInfo.FileName = "cl.exe";
compile.StartInfo.Arguments = "main.c";
compile.StartInfo.CreateNoWindow = true;
richTextBox_LogDisplay.AppendText(
compile.StartInfo.FileName + " " +
compilerArguments + "\n");

// Set UseShellExecute to false for redirection.
compile.StartInfo.UseShellExecute = false;
// Redirect the standard output of the compile command.
compile.StartInfo.RedirectStandardError = true;

compile.Start();
// Synchronously read the standard output of the spawned process.
string output = compile.StandardError.ReadToEnd();
if (output != null)
{
richTextBox_LogDisplay.AppendText(output);
richTextBox_LogDisplay.AppendText("\n");
}
compile.WaitForExit();
 
G

Guest

Ray Mitchell said:
Hello,

I have a C# application in which I start another process which produces
output to stdout and stderr. In fact, that process is the uSoft VS2005 C/C++
compiler itself! I would like to capture the results of the compile and
display them in a RichTextBox. The problem I'm having is that when I
intentionally introduce an error in the C code I'm compiling, I can't read
the error output in my C# program. I've tried redirecting both stdout and
stderr (not at the same time) but I can never capture the error messages,
only the start of the compile itself. If I simply open a command window and
manually do th compile from there, I see the error messages fine. Below is
an example of a simplified version of the C# code I'm using. This code uses
stderr but I've also tried it for stdout (the C/C++ compiler actually appears
to output its code syntax error messages to stdout rather than stderr):

Process compile = new Process();
compile.StartInfo.FileName = "cl.exe";
compile.StartInfo.Arguments = "main.c";
compile.StartInfo.CreateNoWindow = true;
richTextBox_LogDisplay.AppendText(
compile.StartInfo.FileName + " " +
compilerArguments + "\n");

// Set UseShellExecute to false for redirection.
compile.StartInfo.UseShellExecute = false;
// Redirect the standard output of the compile command.
compile.StartInfo.RedirectStandardError = true;

compile.Start();
// Synchronously read the standard output of the spawned process.
string output = compile.StandardError.ReadToEnd();
if (output != null)
{
richTextBox_LogDisplay.AppendText(output);
richTextBox_LogDisplay.AppendText("\n");
}
compile.WaitForExit();


****************************************************************

Sorry, but I mistakenly hit the post button before I finished/corrected my
post. Here it is again...

Hello,

I have a C# application in which I start another process which produces
output to stdout and stderr. In fact, that process is the uSoft VS2005 C/C++
compiler itself! I would like to capture the results of the compile and
display them in a RichTextBox. The problem I'm having is that when I
intentionally introduce an error in the C code I'm compiling, I can't read
the error output in my C# program. I've tried redirecting both stdout and
stderr (not at the same time) but I can never capture the error messages,
only the start of the compile itself, which looks fine. If I simply open a
command window and manually do the compile from there, I do see the error
messages. Below is an example of a simplified version of the C# code I'm
using. This code uses stderr but I've also tried it for stdout (the C/C++
compiler actually appears to output its code syntax error messages to stdout
rather than stderr):

Process compile = new Process();
compile.StartInfo.FileName = "cl.exe";
compile.StartInfo.Arguments = "main.c";
compile.StartInfo.CreateNoWindow = true;
richTextBox_LogDisplay.AppendText(
compile.StartInfo.FileName + " " +
compile.StartInfo.Arguments + "\n");

// Set UseShellExecute to false for redirection.
compile.StartInfo.UseShellExecute = false;
// Redirect the standard output of the compile command.
compile.StartInfo.RedirectStandardError = true;

compile.Start();
// Synchronously read the standard output of the spawned process.
string output = compile.StandardError.ReadToEnd();
if (output != null)
{
richTextBox_LogDisplay.AppendText(output);
richTextBox_LogDisplay.AppendText("\n");
}
compile.WaitForExit();

I thought that possibly I might need more time before doing the ReadToEnd so
I put a 5-second sleep in after doing the Start, but it made no difference.
I also tried moving the WaitForExit right after the Start, but it made no
difference either.

Thanks,
Ray Mitchell
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top