473,320 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Reading redirected stdout from a spawned process

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
1 3743


"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bernhard Kuemel | last post by:
Hi! I want to read/write commands and program input to/from /bin/bash several times before I close the stdin pipe. However, reading from cat hangs unless I first close the stdin pipe. <?php...
2
by: Birch | last post by:
I have a python script that uses the print function throughout, and as well uses calls to os.system() to spawn DOS commandline executables. My issue is that I redirect all of the output from this...
1
by: Michael W. Cocke | last post by:
This is probably incredibly obvious, but I'm just not getting it. I need to accept an arg (I don't care if it's from getopt or @ARGV). and I need to be able to print to STDOUT. So far, so good. ...
3
by: Patrick Porter | last post by:
Im trying to capture the stdout from running a process from within my app. I've been dinking around using the code straight off the MS site. i can get "ipconfig / all" (for example) to send back...
0
by: Christoph Haas | last post by:
Evening, I'm having trouble with running a process through Python 2.4's subprocess module. Example code: ======================================================== def run(command): run =...
3
by: Pappy | last post by:
SHORT VERSION: Python File B changes sys.stdout to a file so all 'prints' are written to the file. Python file A launches python file B with os.popen("./B 2>&^1 >dev/null &"). Python B's output...
6
by: gregpinero | last post by:
Let's say I have this Python file called loop.py: import sys print 'hi' sys.stdout.flush() while 1: pass And I want to call it from another Python process and read the value 'hi'. How...
4
by: Wilbert Berendsen | last post by:
Hi, using pty.spawn() it seems that stderr output of the spawned process is directed to stdout. Is there a way to keep stderr separate and only direct stdin and stdout to the pty? TIA, w...
2
by: subrahmanya | last post by:
Hi All Does any one has idea about how to set stdout and stderr to a spawned process? I don't want to have all the handles of the parent process in the child process but I want only one of it...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.