0

Trying to load a 3rd party application in admin mode with a C# process and read in its output. However, when I try and use RedirectStandardOutput on the process, it crashes a few seconds after starting on Windows 7 PCs. I'm able to get it to work fine on Windows 10, but we also need it to work on Windows 7. I'd like to have a way for the process to be able to run properly and me to read its output into the program without it crashing when C# tries to load it.

I've isolated the process to only crashing when I redirect standard output or standard error. Setting UseShellExecute to false doesn't seem to affect it. Command Prompt similarly closes when I try and load it like this, so it doesn't seem to be a quirk of the program.

I've also tried getting around it by using FileSystemWatcher and reading logs that the program can be set to output to those and I read them in, but when I tried that, the output didn't seem to be flushing to the file at a good enough rate.

Adding a "runas" verb in startInfo doesn't seem to affect it either as this process already requires the application to be started in administrator mode.

Process p = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = filename;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
p.StartInfo = startInfo;

if(case1){
    p.StartInfo.Arguments = args;
    p.Start();
}

The expected result, which happens on my original Windows 10 development environment, is that the process would start

The actual result on Windows 7 is that the program freezes on startup and the windows 'this application is not responding' message pops up.

EventViewer seems to think the problem is with msvcr120.dll but the fact this has happened on 3 separate Windows 7 PCs indicates it's probably something else.

  • *UseShellExecute to false doesn't seem to affect it*. The only way to redirect stdOut is to set `UseShellExecute = false` (since the default is `true`). Otherwise you receive an exception. I don't see where you're redirecting stdOut and what is receiving it (`BeginOutputReadLine` or a stream that receives the output), nor any other method that let the Process wait until the program is done (`WaitForExit` or the `Exited` event) or kills it. – Jimi Jun 08 '19 at 00:55
  • Resolved with a workaround, used class described here https://stackoverflow.com/questions/9757018/how-to-monitor-textfile-and-continuously-output-content-in-a-textbox – CorvusAtrox Jun 10 '19 at 20:06

1 Answers1

1

It could be you're redirecting the output but not reading it in. The child process will hang when it fills its output buffer. Make sure you are reading it in in the C# code. Something like:

        using (Process p = new Process())
        {
            p.StartInfo.UseShellExecute = false;                
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.OutputDataReceived += (sender, args) => Debug.WriteLine("STDOUT: " + args.Data);
            p.ErrorDataReceived += (sender, args) => Debug.WriteLine("STDERR:" + args.Data);
            p.StartInfo.FileName = exePath;
            p.StartInfo.Arguments = parameters;
            p.Start();
            p.BeginOutputReadLine();
            p.BeginErrorReadLine();

            p.WaitForExit(30 * 1000);

            if (!p.HasExited)
            {
                Debug.WriteLine("Killing process");
                p.Kill();
            }

        }

At the very least maybe the child executable is writing an error to its stderr, in which case the problem will show up in your Debug output.

Nick Garyu
  • 506
  • 2
  • 5