[分享]使用Vista的崩溃恢复/重启机制优化你的软件
本文地址:http://blog.csdn.net/Tr0j4n/archive/2009/10/05/4633917.aspx
从Vista开始,微软便引进了软件恢复/重启机制,由软件先向系统注册一个回调函数,当软件发生collapse的时候,系统可以帮你做一些事情,比如写错误日志,重启程序自身等等。
图1
微软在《Application Recovery and Restart Reference》中介绍了这些新增的API,
主要的是这么几个API:
ApplicationRecoveryFinished
Indicates that the calling application has completed its data recovery.
ApplicationRecoveryInProgress
Indicates that the calling application is continuing to recover data.
GetApplicationRecoveryCallback
Retrieves a pointer to the recovery callback routine registered for the specified process.
GetApplicationRestartSettings
Retrieves the restart information registered for the specified process.
RegisterApplicationRecoveryCallback
Registers the active instance of an application for recovery.
RegisterApplicationRestart
Registers the active instance of an application for restart.
UnregisterApplicationRecoveryCallback
Removes the active instance of an application from the recovery list.
UnregisterApplicationRestart
Removes the active instance of an application from the restart list.
笔者对他们进行了探索,现在将其中的崩溃恢复心得和大家共享,程序重启和其类似,请读者自己举一反三
首先,打开VC2008,创建一个基于对话框的MFC项目,在上面放3个按钮
图2
接下来,我们写代码
我们需要用"Raise a CRASH"这个按钮来触发一个溢出异常,让程序无法继续运行,代码如下
/*---------------------------------------------- Author:tr0j4n Blog:http://blog.csdn.net/Tr0j4n Function:Raise a Overflow Exception Show the software recovery Mechanism of Vista ---------------------------------------------------*/ void CCrashDlg::OnBnClickedButton1() { // TODO: 在此添加控件通知处理程序代码 RaiseException(EXCEPTION_FLT_OVERFLOW, EXCEPTION_NONCONTINUABLE_EXCEPTION,NULL,NULL); }
HRESULT WINAPI RegisterApplicationRecoveryCallback( __in APPLICATION_RECOVERY_CALLBACK pRecoveryCallback, __in_opt PVOID pvParameter, __in DWORD dwPingInterval, __in DWORD dwFlags );
/*---------------------------------------------- Author:tr0j4n Blog:http://blog.csdn.net/Tr0j4n Function:RecoveryCallback Function Show the software recovery Mechanism of Vista ---------------------------------------------------*/ DWORD WINAPI ApplicationRecoveryCallback( PVOID pvParameter ) { //Write a crash log LPTSTR curDirectory=new TCHAR[256]; GetCurrentDirectory(256,curDirectory); CString szLogFilePath; szLogFilePath.Format(_T("%s\\CrashLog.txt"),curDirectory); CStdioFile logFile(szLogFilePath,CFile::modeCreate | CFile::modeWrite | CFile::typeText); logFile.WriteString(_T("I was crashed----------------Tr0j4n")); //Restart myself LPTSTR lpFilename=new TCHAR[256]; GetModuleFileName(NULL,lpFilename,256); ShellExecute(NULL,_T("open"),lpFilename,NULL,NULL,SW_SHOW); //Recovery Finished ApplicationRecoveryFinished(TRUE); return 0;
/*---------------------------------------------- Author:tr0j4n Blog:http://blog.csdn.net/Tr0j4n Function:Register the Recovery Callback Function PS:UnregisterApplicationRecoveryCallback can unregister the callback function Show the software recovery Mechanism of Vista ---------------------------------------------------*/ void CCrashDlg::OnBnClickedOk() { // TODO: 在此添加控件通知处理程序代码 HRESULT hr=RegisterApplicationRecoveryCallback(ApplicationRecoveryCallback, NULL,25000,0); if( S_OK !=hr) MessageBox(_T("RegisterApplicationRecoveryCallback Error"), _T("Warn!"),MB_OK|MB_ICONERROR); }