By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,018 Members | 1,441 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,018 IT Pros & Developers. It's quick & easy.

Reading redirected stdout from a spawned process

P: n/a
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();

Sep 29 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a


"Ray Mitchell" wrote:
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

Sep 29 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.