Visual studioc ++ new features-MFC Wang return (3) New MFC function-Windows 7-based restart management disaster recovery

Source: Internet
Author: User
Tags visual studio 2010

To improve user experience and make users feel better, Microsoft first introduced the restart manager in Windows Vista ).

Similar to the automatic saving function of office2007, once a fault occurs, you can restore your work results so that your work results will not be lost due to a fault.

It can help applicationsProgramMaintain the current running status of the Software. After the software is updated, it needs to be restarted, or after a very serious problem crashes, the software can be restarted and restored to the current working status of the software. More importantly, it can restore the state of automatically stored software data and ensure the security of user data as much as possible. With the restart manager, the software can be quickly recovered from a disaster to achieve rapid "post-disaster reconstruction ".

Jim Allchin, president of Microsoft platform products and services, told EWeek that if an application or operating system needs to be updated, the installer will call a restart manager. Restart manager is responsible for checking whether the relevant parts of the operating system can be cleared to update it. If you can, the system does not have to be restarted.
support for restart manager is added to Visual C ++ 2010, which is about to return to the king.
the main applications of the restart manager are as follows:
◆ Software Update
in many cases, after the software or operating system is upgraded, a restart is required to take effect. In this case, we can use the restart manager to automatically shut down the software that runs and update it. After the update is complete, the software is automatically restarted and restored to the current working state of the software. This will make software updates smoother and smarter.
◆ software disaster recovery
when the software encounters a serious error and the process crashes, you can restart the software on the restart manager to restore the data automatically maintained by the software, this allows the software to quickly recover from a disaster.
to support the restart manager, Microsoft provides a restart Manassas API function. These functions are defined in the header file. If your application wants to use this API, You need to reference rstrtmgr. lib and rstrtmgr. dll. Among them, our common functions are:
◆ rmstartsession
Create a New Restart task.
◆ rmgetlist
this function can be used by the installer to obtain all affected applications and their current status.
◆ rmregisterresources
register the resources of the restart task, such as the file name, service, or rm_unique_process struct.
◆ rmrestart
restart the applications or services shut down by rmshutdown. Of course, these applications or services must be registered through registerapplicationrestart in advance.
◆ rmshutdown
close the application or service.
◆ rmendsession
end the restart task.
added support for the restart manager
Although Windows Vista or Windows 7 provides support for the restart manager, but for the application itself, it also requires some additional work to support the restart manager.

The restart Manager option is available on the advanced features Tab Of the MFC Application Wizard. If you only select the "support restart manager" option, your application only supports restart. In other words, your application can be restarted after upgrade or crash, but it cannot automatically open unclosed documents and cannot recover data.
If the "Reopen previusly open documents" option is selected at the same time, it indicates that your application can re-open the document that was previously opened after the restart, that is, automatically restore to the current working state.
If the "support application recover" option is selected, it indicates that after your application restarts, you can not only re-open the previously opened documents, but also try to restore automatically saved documents. It will pop up a task dialog box (UNICODE version) or message box (non-Unicode version), asking the user whether to restore the automatic maintenance documentation. If you select Yes, The automatically maintained document will be opened as the current document. If the user selects "no", the last saved document will be opened as the current document, and the automatically saved document will be deleted.
Note that only document view applications support the "Reopen previusly open documents" option and "support application recover" option, dialog Box applications only support the support restart Manager option.
For the newly created MFC Application in Visual Studio 2010, you can set it in the Application Wizard to add support for the restart manager. What should I do if I want to get support from the restart manager for many existing MFC applications? In fact, for existing MFC applications, it is very easy to get support for the restart manager. In the new version of MFC, The cwinapp class adds a new member variable to control the application's support for the restart manager. We only need to add several lines in the application's constructor. Code And initialize it as needed.
It is consistent with the options in the "MFC Application Wizard" described above. If you select "support restart manager", you can add the following code to the initialization function:
M_dwrestartmanagersupportflags =
Afx_restart_manager_support_restart;

 

If you want to select "Reopen previusly open documents", you can add the following code:
M_dwrestartmanagersupportflags =
Afx_restart_manager_support_restart_aspects;

If you want to select "support application recover", you can add the following line of code:
M_dwrestartmanagersupportflags =
Afx_restart_manager_support_all_aspects;


For example, if I only want to support the restart manager so that the software can be restarted after the update, You can initialize the application as follows:
Crestartmanagerdemoapp: crestartmanagerdemoapp ()
{
M_bhicoloricons = true;
// Support restart Manager
M_dwrestartmanagersuppflflags = afx_restart_manager_support_restart;
// Todo: Add construction code here,
// Place all significant initialization in initinstance
}

 

 

 

 

 

 

First, start Visual Studio 2010 CTP and create a single-document application csdnrestartmanager. According to the previous introduction, in the "MFC Application Wizard", select the "support restart manager" and "Reopen previusly open documents" options, to support application restart and document re-opening. Select editview and Document Manager.
To verify that the restart manager reopens the document, we add some data in the document, which will be automatically loaded after the program restarts.

First, the serialization of documents allows us to save and reload document data. If we don't know how to serialize documents, we can go to Google!

That is to say, we first use MFC to implement a Notebook function. This network is flooded, and this expert is not paying much attention here,

 

 

 

 

Use restart Manassas API to test the restart Manager
Next, we can write a test program and use the restart Manassas API to simulate software updates and then restart to verify that the restart Manager works properly.
Use Visual Studio 2010 RC to create a console application test, and then implement the following:

 

 

# Include "stdafx. h "</P> <p> int _ tmain (INT argc, _ tchar * argv []) <br/>{< br/> DWORD dwsessionhandle = 0; <br/> wchar wszsessionkey [cch_rm_session_key + 1]; <br/> // set the resource to be restarted <br/> lpcwstr pwzresourcestorestart [] = {L "C: /users/tfssetup/documents/Visual Studio 10/projects/csdnrestartmanager/debug/csdnrestartmanager.exe "}; <br/> // create a restart task <br/> If (rmstartsession (& dwsessionhandle, 0, wszsessionkey) = error_success) <br/> {// register resources <br/> If (rmregisterresources (dwsessionhandle, 1, pwzresourcestorestart, 0, null, 0, null) = error_success) <br/> {<br/> // close the application <br/> If (rmshutdown (dwsessionhandle, rmshutdownonlyregistered, null) = error_success) <br/> {// restart the application <br/> If (rmrestart (dwsessionhandle, 0, null) = error_success) <br/>{< br/> return 0; <br/>}< br/> return 0; <br/>}

We first run csdnrestartmanager, and input data to the document via a keyboard in the video. Then, we keep the document test.txt.

Now we can run test to restart the application. After running test, we can see that csdnrestartmanager is disabled and then re-opened. At the same time, the test.txt file we opened earlier was reloaded, and the entire application quickly recovered to our previous working state.

 

 

 

 

 The author's monograph visual c ++ 2010 Development authoritative guide is coming soon. Stay tuned for the latest technologies in Visual C ++ 2010 and Windows 7!

 

 

 

 

 

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.