samedi 28 février 2015

Console output redirect issue

I have an issue with console output redirection (wrote in C#). With "cmd.exe" and any arguments (like "dir") everything works just fine, but with "gnatmake.exe" or "gcc.exe" nothing! I don't see an output and the commands I gave to these programs aren't working (source code isn't compilling)! I've tried with and without arguments -- nothing!



console = new Process();

// The path is correct, I've checked!
console.StartInfo.FileName = @"D:\MinGW\bin\gnatmake.exe";

// cmd.exe works perfectly!
//console.StartInfo.FileName = @"C:\Windows\System32\cmd.exe";

// gnatmake.exe isn't working even without arguments
console.StartInfo.Arguments = currFile;
console.StartInfo.UseShellExecute = false;
console.StartInfo.CreateNoWindow = true;
console.StartInfo.RedirectStandardOutput = true;
console.OutputDataReceived += new DataReceivedEventHandler(ConsoleOutputHandler);
console.Start();
console.BeginOutputReadLine();

void ConsoleOutputHandler(object sendingProcess, DataReceivedEventArgs recieved)
{
if (!string.IsNullOrWhiteSpace(recieved.Data))
{
MessageBox.Show(recieved.Data);
}
}


I've tried something else:



console = new Process();
console.StartInfo.FileName = @"D:\MinGW\bin\gnatmake.exe";
console.StartInfo.UseShellExecute = false;
console.StartInfo.CreateNoWindow = true;
console.StartInfo.RedirectStandardOutput = true;
console.StartInfo.RedirectStandardInput = true;
console.Start();

StreamWriter sr = console.StandardInput;
sr.WriteLine(currFile);
sr.Close();

string str = console.StandardOutput.ReadToEnd();
console.WaitForExit();
MessageBox.Show(str);


Still doesn't work with "gnatmake.exe", but works with "cmd.exe"!


But then I've just wrote this:



Process.Start(@"D:\MinGW\bin\gnatmake.exe", currFile);


and it worked, compiled the file but with this function I can't have an output! What's wrong with "gnatmake.exe" and "gcc.exe"? How to do it properly? Thanks for answers!


Aucun commentaire:

Enregistrer un commentaire