Click here to Skip to main content
15,892,480 members
Home / Discussions / C#
   

C#

 
AnswerRe: USB Device with C# Pin
I.explore.code30-Sep-07 20:50
I.explore.code30-Sep-07 20:50 
AnswerRe: USB Device with C# Pin
Luc Pattyn1-Oct-07 1:04
sitebuilderLuc Pattyn1-Oct-07 1:04 
QuestionAutoFit an Image Pin
confusedme30-Sep-07 18:41
confusedme30-Sep-07 18:41 
AnswerRe: AutoFit an Image Pin
Christian Graus30-Sep-07 19:07
protectorChristian Graus30-Sep-07 19:07 
GeneralRe: AutoFit an Image Pin
confusedme30-Sep-07 20:01
confusedme30-Sep-07 20:01 
GeneralRe: AutoFit an Image Pin
Christian Graus30-Sep-07 21:00
protectorChristian Graus30-Sep-07 21:00 
Questionvalidate date.... plz help Pin
P_Elza30-Sep-07 18:36
P_Elza30-Sep-07 18:36 
QuestionCapturing stdout from a processes started in C# Pin
Ray Mitchell30-Sep-07 18:00
Ray Mitchell30-Sep-07 18:00 
Hello,

I want to start a process from within a C# application, then capture that process's stdout output and display it in a C# multiline TextBox. The process I want to start happens to be the Microsoft Visual Studio 2005 command line C/C++ compiler, cl.exe. Just to get you up to speed on the problem though, below is what I get if I simply open a command window and run the command line compiler cl.exe from there on file main.c.

If I do:

cl main.c

I get the following output to my command window if it succeeds:

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

main.c
Microsoft (R) Incremental Linker Version 8.00.50727.762
Copyright (C) Microsoft Corporation. All rights reserved.

/out:main.exe
main.obj

If I redirect stdout into a file, the file contains:

main.c
Microsoft (R) Incremental Linker Version 8.00.50727.762
Copyright (C) Microsoft Corporation. All rights reserved.

/out:main.exe
main.obj

Note that the first part about the compiler version is missing because, for some reason, Microsoft chose to output it to stderr instead of stdout, but that's not the issue I care about anyway.


Now, if I intentionally introduce a syntax error into main.c on line 6, I get the following instead of the above.

No redirection:

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

main.c
main.c(6) : error C2059: syntax error : ')'

With redirection, file contains:

main.c
main.c(6) : error C2059: syntax error : ')'

This is all well and good. My goal for trying to capture stdout with a C# program is simply to capture what ended up in the file in both of redirection cases above. The C# code below represents a code sample I got directly from an MSDN example of how to start a process and capture it's stdout:

Process proc = new Process();
proc.StartInfo.FileName = "cl.exe";
proc.StartInfo.Arguments = "main.c";
proc.StartInfo.CreateNoWindow = true;

proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;

proc.Start();
string output = proc.StandardOutput.ReadToEnd();
if (output != null)
{
myRichTextBox.AppendText(output);
myRichTextBox.AppendText("\n");
}
proc.WaitForExit();


If I run this code on the version of main.c that doesn't contain the syntax error, I get the following displayed in my text box, exactly as I would expect and exactly like when I did all this from the command line itself:

main.c
Microsoft (R) Incremental Linker Version 8.00.50727.762
Copyright (C) Microsoft Corporation. All rights reserved.

/out:main.exe
main.obj

However, when I do it with the version of main that contains the syntax error on line 6, I get the following in the text box:

main.c

Notice that the line actually describing the error is missing:
main.c(6) : error C2059: syntax error : ')'

So, that is my problem. Why is that line missing? Obviously this line is being output to stdout rather than stderr or it wouldn't have gotten redirected in the command line version. I'm thinking it's probably a C# or .NET bug, but I'm probably wrong. I tried moving the proc.WaitForExit before to the ReadToEnd function (even though MSDN said not to do this), but that didn't help. I also tried putting a 5-second Sleep after the Start but again, no improvement. Possibly simply redirecting stdout directly into a file from within C#, then opening that file and reading it is a work-around, but I don't know how to do that. So, what do you think?


Thanks,
Ray

AnswerRe: Capturing stdout from a processes started in C# Pin
Luc Pattyn1-Oct-07 1:11
sitebuilderLuc Pattyn1-Oct-07 1:11 
GeneralRe: Capturing stdout from a processes started in C# Pin
Ray Mitchell1-Oct-07 9:00
Ray Mitchell1-Oct-07 9:00 
GeneralRe: Capturing stdout from a processes started in C# Pin
Luc Pattyn1-Oct-07 9:26
sitebuilderLuc Pattyn1-Oct-07 9:26 
GeneralRe: Capturing stdout from a processes started in C# Pin
Ray Mitchell1-Oct-07 10:46
Ray Mitchell1-Oct-07 10:46 
GeneralRe: Capturing stdout from a processes started in C# Pin
Luc Pattyn1-Oct-07 11:18
sitebuilderLuc Pattyn1-Oct-07 11:18 
Questionhow to add grammar checker Component into C# and use it ? Pin
yashar201230-Sep-07 13:25
yashar201230-Sep-07 13:25 
AnswerRe: how to add grammar checker Component into C# and use it ? Pin
Judah Gabriel Himango30-Sep-07 13:31
sponsorJudah Gabriel Himango30-Sep-07 13:31 
GeneralRe: how to add grammar checker Component into C# and use it ? Pin
yashar201230-Sep-07 16:31
yashar201230-Sep-07 16:31 
GeneralRe: how to add grammar checker Component into C# and use it ? Pin
Paul Conrad30-Sep-07 18:19
professionalPaul Conrad30-Sep-07 18:19 
QuestionProblems with UserSettings in Vista Pin
norla30-Sep-07 11:46
norla30-Sep-07 11:46 
QuestionCustom form? Pin
MasterSharp30-Sep-07 11:38
MasterSharp30-Sep-07 11:38 
AnswerRe: Custom form? Pin
Paul Conrad30-Sep-07 11:45
professionalPaul Conrad30-Sep-07 11:45 
GeneralRe: Custom form? Pin
MasterSharp30-Sep-07 12:14
MasterSharp30-Sep-07 12:14 
GeneralRe: Custom form? Pin
Christian Graus30-Sep-07 12:25
protectorChristian Graus30-Sep-07 12:25 
GeneralRe: Custom form? Pin
MasterSharp30-Sep-07 12:34
MasterSharp30-Sep-07 12:34 
GeneralRe: Custom form? Pin
Kenny McKee30-Sep-07 12:46
Kenny McKee30-Sep-07 12:46 
GeneralRe: Custom form? Pin
MasterSharp30-Sep-07 12:56
MasterSharp30-Sep-07 12:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.