从Vista开始,微软便引进了软件恢复/重启机制,由软件先向系统注册一个回调函数,当软件发生collapse的时候,系统可以帮你做一些事情,比如写错误日志,重启程序自身等等。
![](https://p-blog.csdn.net/images/p_blog_csdn_net/tr0j4n/EntryImages/20091005/crash1.jpg)
图1
微软在《Application Recovery and Restart Reference》中介绍了这些新增的API,
主要的是这么几个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.
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个按钮
![](https://p-blog.csdn.net/images/p_blog_csdn_net/tr0j4n/EntryImages/20091005/crash2.jpg)
图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);
- }
OK按钮是向系统注册一个崩溃恢复回调函数
- HRESULT WINAPI RegisterApplicationRecoveryCallback(
- __in APPLICATION_RECOVERY_CALLBACK pRecoveryCallback,
- __in_opt PVOID pvParameter,
- __in DWORD dwPingInterval,
- __in DWORD dwFlags
- );
pRecoveryCallback [in]
pvParameter [in, optional]
dwPingInterval [in]
A pointer to the recovery callback function. For more information, see ApplicationRecoveryCallback.
//崩溃恢复回调函数的指针
A pointer to a variable to be passed to the callback function. Can be NULL.
//崩溃恢复回调函数的参数
The recovery ping interval, in milliseconds. By default, the interval is 5 seconds (RECOVERY_DEFAULT_PING_INTERVAL). The maximum interval is 5 minutes. If you specify zero, the default interval is used.
//单位为毫秒,崩溃后多少毫秒后执行崩溃恢复回调函数
Reserved for future use. Set to zero.
//保留,为0
崩溃恢复回调函数代码如下:
- /*----------------------------------------------
- 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;
- }
OK按钮中,注册崩溃恢复回调函数的代码如下:
- /*----------------------------------------------
- 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);
- }
我在崩溃恢复回调函数中让其写入错误日志,并在25秒后重新启动自身。由于我是简单的测试,所以我的回调函数写的比较简单,大家可以更加灵活的运用,比如给自己的程序加上参数支持,比如-c就是从崩溃之后启动的,启动的时候提示用户崩溃过。在ApplicationRecoveryCallback()中的ShellExcute中加入参数支持。
最后,测试。我们先点击OK向系统注册崩溃恢复回调函数,接下来点击"Raise a CRASH"来触发一个崩溃,弹出了图1的样子,接下来程序就退出了。25秒后,程序果然重启后,当前目录下也生成了崩溃日志。
Vista API还有很多的新特性,合理地利用他们能帮助我们开发者提高软件的性能和质量,本文仅供抛砖引玉用。