Disabling the program crash dialog

Raymond Chen

If you don’t want your program to display the standard crash dialog, you can disable it by setting the SEM_NOGPFAULTERRORBOX flag in the process error mode.

The simple-minded way is just to do

SetErrorMode(SEM_NOGPFAULTERRORBOX);

but this overwrites the previous error mode rather than augmenting it. In other words, you inadvertently turned off the other error modes!

Unfortunately, there is no GetErrorMode function, so you have to do a double-shuffle.

DWORD dwMode = SetErrorMode(SEM_NOGPFAULTERRORBOX);
SetErrorMode(dwMode | SEM_NOGPFAULTERRORBOX);

This sets the new error mode (possibly disabling some other error modes that had been set) and saves the previous mode. Then it sets the error mode the right way, adding the SEM_NOGPFAULTERRORBOX flag to the existing error modes.

Merging with existing error modes is important. For example, the previous error mode may have included SEM_NOALIGNMENTFAULTEXCEPT. If you casually turned that off, then the program would not longer receive automatic alignment fault fixups and will start crashing.

(But if you read the documentation, you’ll see that SEM_NOALIGNMENTFAULTEXCEPT is special: The system won’t let you turn it off once it’s been turned on. Why? Because too many people were already making this mistake. I remember before this special rule was introduced. Programs were crashing left and right because they didn’t do the double-set shuffle above; as a result, they started crashing on alignment faults. So the special rule had to be added. Welcome to the world of compatibility, where it is the operating system’s duty to cover for other people’s mistakes. Mind you, the design of the SetErrorMode function makes this mistake a very easy one to commit.)

Note that the error mode is a process-wide setting, not a per-thread setting. This means that manipulating the process error mode is not something you should do lightly, since it may have unintended consequences for other threads (which you might not have control over). For safety’s sake, a program should set its error mode when it starts up and not mess with it thereafter.

Of course, if you disable the crash dialog, then you also miss out on the opportunity to retrieve crash reports collected by Windows Error Reporting so you can see where your program is crashing in the real world.

0 comments

Discussion is closed.

Feedback usabilla icon