2

I am launching a process and attempting to read the StdOut... I am very familiar with the normal way of launching a process, redirecting, and capturing the output.

However, I have run across an app which I can launch manually at the command prompt and see output in the console, but does not provide any output on the StdOut or StdErr streams. I have attempted launching cmd.exe first and I am able to capture the Output from cmd.exe, but not this process when launched that way either.

To be more clear, when I run app.exe manually, I see this on the console:

Trying to connect to VP

Trying to connect to VP

When I launch it from System.Diagnostics.Process directly:

[blank]

When I launch it from System.Diagnostics.Process via cmd.exe:

Microsoft Windows [Version 6.1.7601]

Copyright (c) 2009 Microsoft Corporation. All rights reserved.

Again, the class I am using to do this runs many process and has only has an issue getting the output with this app.exe, so this is not a "hey maybe try redirecting the standard output" type of problem. I know that works in my implementation.

Ideas? I've simply never encountered this before.. How can a process put something on the console window when running manually, but not when running via the Process object?

Here's my code:

process = new Process();
process.StartInfo.FileName = f.FullName;
process.StartInfo.Arguments = arguments;
process.StartInfo.WorkingDirectory = workingDir;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.OutputDataReceived += p_OutputDataReceived;
process.ErrorDataReceived += p_ErrorDataReceived;
if (!process.Start())
{
    throw new Exception("Failed to start [" + f.Name + "]. Exit code " + process.ExitCode);
}
process.BeginOutputReadLine();
process.BeginErrorReadLine();

processRunning = true;

process.WaitForExit();

exitCode = process.ExitCode;
John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • 2
    Just wait longer, until the program has generated about 2 or 4 kilobytes worth of output and the stream buffer filled up. Fixing this requires the program's source code, that's usually where it ends. – Hans Passant Feb 14 '14 at 23:20
  • 1
    What is `p_OutputDataReceived` doing? Please post that. – TyCobb Feb 14 '14 at 23:35
  • Could it be that app.exe was designed to be silent in a particular execution context, like location or security? Maybe if it's run by an administrator it doesn't behave the same way? It's a long shot I know but it might be worth investigating. – Crono Feb 14 '14 at 23:49
  • at work we did face such problems but never had the patience to see it to end. we developed workarounds. in one case we created a batch file with `exe > tmp.txt 2>&1` redirection. we started the batch file and consumed the text file. it worked. in another scenario, there were spaces in the arguments and the app was behaving strangely. we did proper quoting and it worked. again there was an issue with admin-rights. the app internally called `netstat -b` and failed without admin rights. bad thing is it didnt say anything. – inquisitive Feb 15 '14 at 05:03
  • @TyCobb -- p_OutputDataReceived does nothing because it never gets called, so its content is not really part of the discussion. Hans -- I wish... no amount of waiting addresses it. Crono -- Maybe. However, I did get a peek at the source code of the offending app and it's just using what appear to be standard printf statements (so; it's a C++ app) inquisitive -- Yeah, luckily, it also has a log file and I was able to monitor that to get what I needed, but I HATE hacks like that for what should be a simple stdout capture. – user3312039 Feb 18 '14 at 21:26

0 Answers0