Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

Reading redirected stdout from a spawned process

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ray Mitchell - 29 Sep 2007 09:51 GMT
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();
Ray Mitchell - 29 Sep 2007 10:03 GMT
> Hello,
>
[quoted text clipped - 33 lines]
>    }
>    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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.