user1615362
user1615362

Reputation: 3817

Disabling Windows Error Reporting(AppCrash) dialog programmatically

I am executing a Process in .NET app.

                Process process = new Process(); 

....

                process.StartInfo.UseShellExecute = false;
                process.StartInfo.ErrorDialog = false;

                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
....
                process.Start();

The problem is that the executable sometimes crashes, which is OK, but the AppCrash dialg pops up and prevents the app to continue the execution until I click on close.

I know that I can set HKLM\Software\Microsoft\Windows\Windows Error Reporting\ value Disabled to true - msdn.microsoft.com/en-us/library/bb513638%28v=vs.85%29.aspx

But is there a way that I can do this in code?

EDIT:

kmp has posted a great answer, but I am still looking how to achieve the same with native application.

Upvotes: 19

Views: 6603

Answers (2)

Ondra
Ondra

Reputation: 1647

According to this document it should be possible to inherit the SetErrorMode value to the child process. So any kind of launcher (or your main app) that would SetErrorMode to required values and then execute your native app should work. Actually this inheritace exists by default. To turn it off see flags here.

Upvotes: 5

kmp
kmp

Reputation: 10865

If you can edit the code of the crashing process then what you can do is add code like I show below to it (this article talks about it: Disabling the program crash dialog) - see SetErrorMode function for the MSDN information about this Windows API function.

If you cannot alter the crashing application's code it is more complex and you would have to inject code into it at runtime (how complex that is depends on what the process you are launching is written in - if it is a .NET process it is easier than a native application for example, so you would need to give some more information about that process).

[Flags]
internal enum ErrorModes : uint 
{
    SYSTEM_DEFAULT = 0x0,
    SEM_FAILCRITICALERRORS = 0x0001,
    SEM_NOALIGNMENTFAULTEXCEPT = 0x0004,
    SEM_NOGPFAULTERRORBOX = 0x0002,
    SEM_NOOPENFILEERRORBOX = 0x8000
}

internal static class NativeMethods
{
    [DllImport("kernel32.dll")]
    internal static extern ErrorModes SetErrorMode(ErrorModes mode);
}

// Ideally the first line of the main function...

NativeMethods.SetErrorMode(NativeMethods.SetErrorMode(0) | 
                           ErrorModes.SEM_NOGPFAULTERRORBOX | 
                           ErrorModes.SEM_FAILCRITICALERRORS | 
                           ErrorModes.SEM_NOOPENFILEERRORBOX);

Upvotes: 24

Related Questions