微软在《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"这个按钮来触发一个溢出异常,让程序无法继续运行,代码如下
C/C++ code
/*---------------------------------------------- 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); }
pRecoveryCallback [in] A pointer to the recovery callback function. For more information, see ApplicationRecoveryCallback.
//崩溃恢复回调函数的指针
pvParameter [in, optional] A pointer to a variable to be passed to the callback function. Can be NULL.
//崩溃恢复回调函数的参数
dwPingInterval [in] 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.
//单位为毫秒,崩溃后多少毫秒后执行崩溃恢复回调函数
dwFlags [in] Reserved for future use. Set to zero.
//保留,为0
崩溃恢复回调函数代码如下:
C/C++ code
/*---------------------------------------------- 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按钮中,注册崩溃恢复回调函数的代码如下:
C/C++ code
/*---------------------------------------------- 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); }