views:

154

answers:

3

Every now and then there's a strong need to write a program in such a way that it never (really never) shows an error message as a message box. For example it can be a program run inside a daily build - if it hangs with a message box the daily build hangs.

Unfortunately VC++ runtime has a lot of ways to trigger message boxes when indicating errors.

First of all, whenever an exception is not handled terminate() is called which calls abort() which causes "This application has requested the Runtime to terminate it in an unusual way." message box. This can be worked around by catching all exceptions and/or using set_terminate() to set a custom terminate() handler without message boxes.

Then, whenever an exception escapes any destrutor during stack unwinding terminate() is also called. set_terminate() helps here as well.

Then, there's a "pure virtual function call" message box that is shown in some hardcore cases of mismatching the number of functions expected by the caller and those implemented by the callee. _set_purecall_handler() should help here.

What else to do to a VC++ program to be absolutely positively sure it doesn't show a message box in some fatal situation?

+2  A: 

I would recommend that you use a helper program to launch it and have this helper limit the time your program can run. This is by far the safest way if you can do it, as it handles every case. Some things you cannot handle in your program directly, such as "This program is not a valid Win32 application" which might happen if you are missing a DLL.

Dark Falcon
+2  A: 

Hook the MessageBox API before daily build.

BTW, i think SetUnhandledExceptionFilter. is a relevant API.

whunmr
and _set_invalid_parameter_handler.
whunmr
+1  A: 
shoosh