使用Restart Manassas API测试重启管理器
接下来,我们可以编写一个测试程序,使用Restart Manassas API模拟软件的更新后重启,以验证其重启管理器是否正常工作。
用Visual Studio 2010 CTP创建一个控制台应用程序TestRM,然后将其实现如下:
// TestRM.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <restartmanager.h>
int _tmain(int argc, _TCHAR* argv[])
{
DWORD dwSessionHandle = 0;
WCHAR wszSessionKey[CCH_RM_SESSION_KEY+1];
// 设定需要重启的资源
LPCWSTR pwzResourcesToRestart[] =
{L"C:\\Users\\TFSSETUP\\Documents\\Visual Studio 10\\Projects\\
RestartManagerDemo\\Debug\\RestartManagerDemo.exe" };
// 创建一个重启任务
if (RmStartSession(&dwSessionHandle, 0, wszSessionKey) == ERROR_SUCCESS)
{
// 注册资源
if (RmRegisterResources(dwSessionHandle, 1,
pwzResourcesToRestart, 0, NULL, 0, NULL) == ERROR_SUCCESS)
{
// 关闭应用程序
if (RmShutdown(dwSessionHandle,
RmShutdownOnlyRegistered, NULL) == ERROR_SUCCESS)
{
// 重新启动应用程序
if (RmRestart(dwSessionHandle, 0, NULL) == ERROR_SUCCESS)
{
return 0;
}
}
}
}
return 0;
}
//
#include "stdafx.h"
#include <windows.h>
#include <restartmanager.h>
int _tmain(int argc, _TCHAR* argv[])
{
DWORD dwSessionHandle = 0;
WCHAR wszSessionKey[CCH_RM_SESSION_KEY+1];
// 设定需要重启的资源
LPCWSTR pwzResourcesToRestart[] =
{L"C:\\Users\\TFSSETUP\\Documents\\Visual Studio 10\\Projects\\
RestartManagerDemo\\Debug\\RestartManagerDemo.exe" };
// 创建一个重启任务
if (RmStartSession(&dwSessionHandle, 0, wszSessionKey) == ERROR_SUCCESS)
{
// 注册资源
if (RmRegisterResources(dwSessionHandle, 1,
pwzResourcesToRestart, 0, NULL, 0, NULL) == ERROR_SUCCESS)
{
// 关闭应用程序
if (RmShutdown(dwSessionHandle,
RmShutdownOnlyRegistered, NULL) == ERROR_SUCCESS)
{
// 重新启动应用程序
if (RmRestart(dwSessionHandle, 0, NULL) == ERROR_SUCCESS)
{
return 0;
}
}
}
}
return 0;
}
我们首先运行RestartManagerDemo,在视图中用鼠标点击向文档中添加数据,然后保持文档为demo.bub。
图3 支持重启管理器的MFC应用程序
现在,我们就可以运行TestRM重启这个应用程序了。运行TestRM后,我们会看到RestartManagerDemo会被关闭然后重新打开。同时,我们之前打开的文档demo.bub也被重新加载,整个应用程序很快恢复到了我们之前的工作状态。
“自从有了重启管理器,更新也简单了,崩溃也不怕了,连加班都不用了!”