使用Vista的崩溃恢复/重启机制优化你的软件

从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"这个按钮来触发一个溢出异常,让程序无法继续运行,代码如下
 
 
OK按钮是向系统注册一个崩溃恢复回调函数
 
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

 

 
崩溃恢复回调函数代码如下:
 

OK按钮中,注册崩溃恢复回调函数的代码如下:
 
 
      我在崩溃恢复回调函数中让其写入错误日志,并在25秒后重新启动自身。由于我是简单的测试,所以我的回调函数写的比较简单,大家可以更加灵活的运用,比如给自己的程序加上参数支持,比如-c就是从崩溃之后启动的,启动的时候提示用户崩溃过。在ApplicationRecoveryCallback()中的ShellExcute中加入参数支持。
      最后,测试。我们先点击OK向系统注册崩溃恢复回调函数,接下来点击"Raise a CRASH"来触发一个崩溃,弹出了图1的样子,接下来程序就退出了。25秒后,程序果然重启后,当前目录下也生成了崩溃日志。
       Vista API还有很多的新特性,合理地利用他们能帮助我们开发者提高软件的性能和质量,本文仅供抛砖引玉用。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值