I have a process which I start with CreateProcess, then I wait for it to finish and check its exit code. I do this in batch mode and I don’t want any message boxes to show up if the process crashes. It’s enough to just return a nonzero exit code which would indicate failure. So far I’ve tried using
LONG WINAPI MyUnhandledExceptionFilter(_EXCEPTION_POINTERS *lpTopLevelExceptionFilter)
{
ExitProcess(-1);
return EXCEPTION_EXECUTE_HANDLER;
}
BOOL CMyApp::InitInstance()
{
AfxEnableControlContainer();
SetUnhandledExceptionFilter(MyUnhandledExceptionFilter);
throw std::runtime_error("test");
}
But this isn’t always silent. Sometimes it displays a dialog:
---------------------------
Microsoft Visual C++ Runtime Library
---------------------------
Runtime Error!
Program: C:\Workspace\MyApp\Release\MyApp.exe
abnormal program termination
---------------------------
OK
---------------------------
You want to suppress two things:
The first you do with
SetErrorModerequestingSEM_FAILCRITICALERRORSandSEM_NOGPFAULTERRORBOX. The second can be suppressed by altering CRT behavior with_set_abort_behavior.You don’t actually need an empty handler with
SetUnhandledExceptionFilter– you are not doing anything important there. Proving no handler would crash the process andSetErrorMode(see above) will suppress the unwanted OS notification.Sample code to do it: