Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I supress all error dialogs when a process crashes (I only want it to crash silently)

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   
---------------------------
like image 598
sashoalm Avatar asked Mar 15 '12 11:03

sashoalm


2 Answers

You want to suppress two things:

  1. OS modal dialog notifying user on crash
  2. Your own dialogs popped up by runtime

The first you do with SetErrorMode requesting SEM_FAILCRITICALERRORS and SEM_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 and SetErrorMode (see above) will suppress the unwanted OS notification.

Sample code to do it:

SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
_set_abort_behavior(0,_WRITE_ABORT_MSG);
like image 137
Roman R. Avatar answered Oct 13 '22 08:10

Roman R.


You're dealing with three kinds of abnormal termination:

  1. Uncaught C++ exception
  2. C++ library exiting
  3. Win32 uncaught excecption.

On Windows, C++ exceptions typically reuse parts of Win32 exception handling. Therefore your method addresses 1 and 3, but misses 2. That's why the message is coming from "Microsoft Visual C++ Runtime Library". You'll need _set_abort_behavior(0, _WRITE_ABORT_MSG) to suppress that.

like image 25
MSalters Avatar answered Oct 13 '22 09:10

MSalters