使用Vista的崩溃恢复/重启机制优化你的软件
2013-07-03 11:13
429 查看
从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"这个按钮来触发一个溢出异常,让程序无法继续运行,代码如下
[cpp] view
plaincopy
/*----------------------------------------------
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按钮是向系统注册一个崩溃恢复回调函数
[cpp] view
plaincopy
HRESULT WINAPI RegisterApplicationRecoveryCallback(
__in APPLICATION_RECOVERY_CALLBACK pRecoveryCallback,
__in_opt PVOID pvParameter,
__in DWORD dwPingInterval,
__in DWORD dwFlags
);
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
崩溃恢复回调函数代码如下:
[cpp] view
plaincopy
/*----------------------------------------------
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按钮中,注册崩溃恢复回调函数的代码如下:
[cpp] view
plaincopy
/*----------------------------------------------
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还有很多的新特性,合理地利用他们能帮助我们开发者提高软件的性能和质量,本文仅供抛砖引玉用。
转载:http://blog.csdn.net/Tr0j4n/article/details/4633917
图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"这个按钮来触发一个溢出异常,让程序无法继续运行,代码如下
[cpp] view
plaincopy
/*----------------------------------------------
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按钮是向系统注册一个崩溃恢复回调函数
[cpp] view
plaincopy
HRESULT WINAPI RegisterApplicationRecoveryCallback(
__in APPLICATION_RECOVERY_CALLBACK pRecoveryCallback,
__in_opt PVOID pvParameter,
__in DWORD dwPingInterval,
__in DWORD dwFlags
);
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
崩溃恢复回调函数代码如下:
[cpp] view
plaincopy
/*----------------------------------------------
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按钮中,注册崩溃恢复回调函数的代码如下:
[cpp] view
plaincopy
/*----------------------------------------------
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还有很多的新特性,合理地利用他们能帮助我们开发者提高软件的性能和质量,本文仅供抛砖引玉用。
转载:http://blog.csdn.net/Tr0j4n/article/details/4633917
相关文章推荐
- 使用Vista的崩溃恢复/重启机制优化你的软件
- 记一次电脑重启后多个软件无法使用、崩溃问题
- 软件调试笔记22 - WINDOWS验证机制 :使用应用程序验证器
- 使用pg_rman软件来实现postgresql数据库的备份恢复
- 闪电恢复(系统数据快速恢复软件) 软件介绍及软件使用特性
- 在 Linux 下使用 RAID(八):当软件 RAID 故障时如何恢复和重建数据
- 模拟namenode崩溃,使用secondarynamenode恢复
- 使用innodb_force_recovery解决MySQL崩溃无法重启问题
- 【自主研发-贡献给SQL Server人员】索引诊断与优化软件使用说明(1)-总体预览
- 化零为整WCF(7) - 消息处理(使用消息传输优化机制 - MTOM)
- 使用简单工厂模式和反射机制优化MVP数据绑定流程
- 数据恢复软件如何换机使用?
- WCF中使用MTOM消息传输优化机制
- Android IPC机制—多进程的使用及优化
- 使用tar恢复ORACLE RAC环境的软件目录
- 优化Photoshop CS3软件使用环境
- 化零为整WCF(7) - 消息处理(使用消息传输优化机制 - MTOM)
- 数据恢复软件如何换机使用?
- android 使用内存缓存机制缓存图片,优化listview,recylerview
- Android Studio 调试机制及性能优化工具使用