How to use the System Restore API to save and to restore system data in Visual C++

The System Restore application programming interface (API) allows you to save system state information, to change the state, and to roll back to a previously held consistent state.

This article demonstrates how to use the System Restore API from Visual C++. In this article, you build an application that creates a restore point, that installs a dummy application, and then that finalizes the restore point. You also see how to view existing Restore Points and how to roll back using the System Restore user interface.

System state information includes the following:
  • Current configuration
  • Registry information
  • Installed applications
  • Devices
  • Critical operating system files
System state information does not include user files such as documents, spreadsheets, and so on.

You can use the System Restore API to record the current state of the operating system and the installed applications. If any subsequent changes (for example, installed applications or devices) cause instability in the system, you can revert to the last stable state. You can also use the System Restore API as part of the installation process of applications that you develop; you can undo any changes that you made if the system becomes unstable after the installation.

The System Restore API monitors selected files on a drive-by-drive basis and makes copies of these files before it changes the files during a system update. A driver in the Microsoft Windows XP operating system intercepts the operations that are performed on files. The copies of files are compressed to save space. Each set of saved files is referred to as a Restore Point. You can use the System Restore user interface to revert the system back to the state that is determined by any Restore Point that you have captured on the computer.

System Restore monitors files that have a recognized file name extension, such as .exe, .dll, and .ini, are described in the following file:
%windir%/System32/Restore/Filelist.xml

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
  • Microsoft Windows XP Home Edition or Microsoft Windows XP Professional
  • Microsoft Visual Studio .NET 2002, Visual Studio .NET 2003, Visual Studio 2005, or Visual Studio 2008
  • Microsoft Platform SDK
This article assumes that you are familiar with Visual C++ programming.

Create a Test Application

  1. Start Visual C++.
  2. On the File menu, point to New , and then click Project .
  3. In the New Project dialog box, click Visual C++ Projects under Project Types , and then click Win32 Project under Templates .

    Note In Visual Studio 2005 or in Visual Studio 2008, click Visual C++ under Project Types .
  4. In the Name text box, type Install , and then click OK .
  5. In the Win32 Application Wizard, click Application Settings .
  6. Under Application type, click Console Application , and then click Finish to create the application.
  7. In Solution Explorer , right-click the Install project, and then click Properties .
  8. In the Install.cpp Property Pages dialog box, click Configuration Properties , click Linker , click Input , add Srclient.lib to the list of Additional Dependencies, and then click OK .

    Note In Visual Studio 2005 or in Visual Studio 2008, the name of the dialog box is Install Property Pages .

Create a Restore Point

  1. Add the following #include directives to the end of stdafx.h:
    #include <windows.h>
    #include <srrestoreptapi.h>
  2. In the _tmain function in Install.cpp, create the two variables shown in the following code:
    RESTOREPOINTINFO RstPt;
    STATEMGRSTATUS MgrStat;
  3. Add the following statements that populate the RstPt variable with information that indicates that an application is about to be installed:
    <?xm-deletion_mark author="v-bobbid" time="20080319T134214-0800" data="RstPt.dwEventType = BEGIN_SYSTEM_CHANGE;
    RstPt.dwRestorePtType = APPLICATION_INSTALL;
    strcpy(RstPt.szDescription, &quot;Demonstration Restore Point&quot;);"?><?xm-insertion_mark_start author="v-bobbid" time="20080319T134222-0800"?>
    RstPt.dwEventType = BEGIN_SYSTEM_CHANGE;
    RstPt.dwRestorePtType = APPLICATION_INSTALL;
    _tcscpy(RstPt.szDescription, _T("Demonstration Restore Point"));
    <?xm-insertion_mark_end?>
  4. Create a Restore Point to mark the beginning of the installation using the SRSetRestorePoint function. In the event of a failure, report the reason for the failure and exit:
    if (!SRSetRestorePoint(&RstPt, &MgrStat))
    {
    printf("Unable to set restore point. Error %ld/n",
    MgrStat.nStatus);
    return 1;
    }
  5. Display the sequence number for the newly created Restore Point:
    printf("Restore point created. Sequence %ld/n", 
    MgrStat.llSequenceNumber);

Install an Application

  1. Add the following statements that simulate some of the operations that are performed when an application is installed. The application creates a subdirectory called A New Application and adds three files to this subdirectory. The file extensions used (.exe, .dll, and .ini) are all examples of monitored extensions.
                  <?xm-deletion_mark author="v-bobbid" time="20080319T134254-0800" data=" // Begin application installation code

    CreateDirectory(&quot;C://A New Application&quot;, NULL);
    CreateFile(&quot;C://A New Application//Application File.exe&quot;,
    GENERIC_READ, 0, NULL, CREATE_ALWAYS, 0, NULL);
    CreateFile(&quot;C://A New Application//Application File.dll&quot;,
    GENERIC_READ, 0, NULL, CREATE_ALWAYS, 0, NULL);
    CreateFile(&quot;C://A New Application//Application File.ini&quot;,
    GENERIC_READ, 0, NULL, CREATE_ALWAYS, 0, NULL);

    // End application installation code"?><?xm-insertion_mark_start author="v-bobbid" time="20080319T134258-0800"?>
    // Begin application installation code

    CreateDirectory(_T("C://A New Application"), NULL);
    CreateFile(_T("C://A New Application//Application File.exe"),
    GENERIC_READ, 0, NULL, CREATE_ALWAYS, 0, NULL);
    CreateFile(_T("C://A New Application//Application File.dll"),
    GENERIC_READ, 0, NULL, CREATE_ALWAYS, 0, NULL);
    CreateFile(_T("C://A New Application//Application File.ini"),
    GENERIC_READ, 0, NULL, CREATE_ALWAYS, 0, NULL);

    // End application installation code
    <?xm-insertion_mark_end?>

Complete the Restore Point

  1. Populate the Restore Point structure with the data that you need to indicate that the installation has completed and that the Restore Point should be saved and recorded. The sequence number used should be the same sequence number that was returned when the Restore Point was created:
    RstPt.dwEventType = END_SYSTEM_CHANGE;
    RstPt.llSequenceNumber = MgrStat.llSequenceNumber;
  2. Call SRSetRestorePoint again to complete the Restore Point. If an error occurs, delete the application files, and remove the Restore Point using the SRRemoveRestorePoint function:
    <?xm-deletion_mark author="v-bobbid" time="20080319T134704-0800" data="if (!SRSetRestorePoint(&amp;amp;RstPt, &amp;amp;MgrStat))
    {
    printf(&quot;Unable to set end of restore point. Error %ld/n&quot;,
    MgrStat.nStatus);

    // Delete application files and remove restore point
    DeleteFile(&quot;C://A New Application//Application File.exe&quot;);
    DeleteFile(&quot;C://A New Application//Application File.dll&quot;);
    DeleteFile(&quot;C://A New Application//Application File.ini&quot;);
    RemoveDirectory(&quot;C://A New Application&quot;);
    SRRemoveRestorePoint((DWORD)MgrStat.llSequenceNumber);
    printf(&quot;Restore point removed/n&quot;);
    return 1;
    }
    printf(&quot;Restore point completed/n&quot;);
    return 0;"?><?xm-insertion_mark_start author="v-bobbid" time="20080319T134715-0800"?>
    if (!SRSetRestorePoint(&RstPt, &MgrStat))
    {
    printf("Unable to set end of restore point. Error %ld/n",
    MgrStat.nStatus);

    // Delete application files and remove restore point
    DeleteFile(_T("C://A New Application//Application File.exe"));
    DeleteFile(_T("C://A New Application//Application File.dll"));
    DeleteFile(_T("C://A New Application//Application File.ini"));
    RemoveDirectory(_T("C://A New Application"));
    SRRemoveRestorePoint((DWORD)MgrStat.llSequenceNumber);
    printf("Restore point removed/n");
    return 1;
    }
    printf("Restore point completed/n");

    return 0;
    <?xm-insertion_mark_end?>

Verify the Restore Point

  1. Build and run the Install application. If the installation is successful, the following messages appear in the Console window (the sequence number may vary):
    Restore Point created. Sequence 24
    Restore Point completed
  2. Using Windows Explorer, navigate to the root directory of the C drive. Find a new folder that is named A New Application . This folder contains three files called Application_File.exe, Application_File.dll, and Application_File.ini.

View the Restore Point

  1. Execute the System Restore user interface application, Rstrui.exe, that is located in the following directory:
    %windir%/system32/Restore
  2. In the Welcome to System Restore dialog box, verify that the Restore my computer to an earlier time radio button is selected, and then click Next .
  3. In the Select a Restore Point dialog box, you see a Restore Point that is labeled Demonstration Restore Point in the list. This is the Restore Point that you created during the Install application. Any other Restore Points for the current day, if you have installed other applications or executed Install more than once, are listed also. You can click dates on the calendar to see the Restore Points for other days.

Rollback the Application Installation

  1. Verify that the Demonstration Restore Point is selected, and then click Next .
  2. In the Confirm Restore Point Selection dialog box, click Next .
    Visual C++ closes all applications that are running. You can see the System Restore window while the system settings are restored and the application is uninstalled. The computer shuts down and restarts.
  3. When Windows XP has restarted, log in. In the Restoration Complete dialog box, click OK .
  4. Use Windows Explorer to view the root directory of the C drive. The directory A New Application is now visible.

For more information about how to use the System Restore API, including the System Restore WMI Classes, see "System Restore" in the Platform SDK.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值