简体   繁体   中英

Identify crash of executable run by C# Process.Start()

My code:

ProcessInfo processInfo = ...
Process proc = Process.Start(processInfo);
proc.WaitForExit();
if (proc.ExitCode != 0)
{
   // ...
}

My problem is that the process (a C++ executable) is sometime crashing due to unhandled exceptions, in unknown circumstances.

I can tell that the executable crashed, since on crash it returns a negative exit code (or non zero for that matter). However, I cannot create a process dump to investigate.

If I at least had Windows' "Program stopped working" message popped, then I could create the dump manually.

Of course I can use software like Debug Diag to monitor executables and take dump on crash, but would rather have a more generic in-house solution.

I think it's really up to the executable being called to output the error. Wether it outputs to the window or puts an entry in the event viewer etc is up to the application in question. You should be able to read the output messages.

Have you tried to capture the stdErr output in addition to stdOut?

For example:

Process installProcess = new Process 
{ 
    StartInfo = 
    {
        FileName = exeName,
        Arguments = args,
        CreateNoWindow = true,
        UseShellExecute = false,
        WindowStyle = ProcessWindowStyle.Hidden,
        RedirectStandardOutput = true,
        RedirectStandardError = true
    }
};

installProcess.Start();

string processStandardOutput = installProcess.StandardOutput.ReadToEnd();
string processStandardError = installProcess.StandardError.ReadToEnd();

// Check both strings for !IsNullOrEmpty and log something of interest

installProcess.WaitForExit();
ExitCode = installProcess.ExitCode;

// If ExitCode < 0, log the StandardError output...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM