简体   繁体   中英

Programmatically restart application when it crashes

I have a Windows application written in C++ that sometimes crashes. Is there a way to programmatically ignore the modal dialog box and automatically relaunch the application?

Something i've learned from viruses: use a service to check that your application is running. If the service detects that the application is not running, start the application. Implement the service however you like.

您可以看看RegisterApplicationRestart函数(最低Windows Vista / 2008)

in vc++:

//-- a Top-level Exception Handler in process level 
LONG WINAPI TopLevelExceptionHandler(PEXCEPTION_POINTERS pExceptionInfo)
{

// find app name to re execute it.
TCHAR szPath[_MAX_PATH];
VERIFY(::GetModuleFileName(AfxGetApp()->m_hInstance, szPath, _MAX_PATH));
CString csPath(szPath);

// re execute.
ShellExecute( NULL, "open",csPath , NULL, NULL, 1 );

//
exit(-1);
return EXCEPTION_CONTINUE_SEARCH;
}


main()
{
   //set a Top-level Exception Handler in process level to re execute app. 
   SetUnhandledExceptionFilter(TopLevelExceptionHandler);
   .
   .
   .
}

Write an unhandled exception handler that relaunches. If you need to be sure you have the memory to do so, preallocate.

If you make recovering from faults too much a guaranteed thing, you risk never fixing the faults.

Plus, it's better to notify someone that it faulted and shutdown. What if you faulted on a problem that will reoccur every time the program starts. Then it looks like your program is always running, but it isn't doing anything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM