Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling Windows Error Reporting(AppCrash) dialog programmatically

Tags:

.net

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.

like image 220
user1615362 Avatar asked Aug 24 '12 04:08

user1615362


1 Answers

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);
like image 138
kmp Avatar answered Oct 25 '22 00:10

kmp