In other posts, I have discussed persistent registry (Windows CE: Persisting Registry Changes from an Application) but what if we want to set the registry back to some known point? There are some functions to save the registry, or parts of the registry, and the restore them later.
Save and Restore the Entire Registry
The functions RegCopyFile() and RegRestoreFile() can be used to save the registry to a file and then restore the registry with that file later. The following are two very simple applications to that use these functions to save and restore the registry.
SaveReg simply saves the registry to a file on a Storage Card. The saved registry is a binary file, so not a .REG file. I used the extension SRG for Saved ReGistry.
#include <Windows.h>
 
int WINAPI WinMain(     HINSTANCE hInstance,
                                                                                HINSTANCE hPrevInstance,
                                                                                LPTSTR    lpCmdLine,
                                                                                int       nCmdShow)
{
                RegCopyFile( TEXT("\\Storage Card\\SaveReg.srg") );
 
}
Then to restore the registry with the file I created RestoreReg. RestoreReg calls RegRestoreFile() which sets up the system to restore the registry after a soft reset, then uses KernelIoControl to perform the soft reset (For more see Windows CE: Soft Reset.)
#include <Windows.h>
 
int WINAPI WinMain(     HINSTANCE hInstance,
                                                                                HINSTANCE hPrevInstance,
                                                                                LPTSTR    lpCmdLine,
                                                                                int       nCmdShow)
{
                RegRestoreFile( TEXT("\\Storage Card\\SaveReg.srg") );
                KernelIoControl(IOCTL_HAL_REBOOT, NULL, 0, NULL, 0, NULL);
}
When the system restarts the registry will be replaced with the registry from the saved file.
Save and Restore Root Keys
The functions RegSaveKey() and RegReplaceKey() can be used with hive based registry only. These functions can save and restore the root keys; HKEY_LOCAL_MACHINE, HKEY_CLASSES_ROOT and HKEY_USER. The documentation for these functions is a little vague and the function signatures are deceiving so you will see that I have added some basic error handling.
In this instance of SaveReg, the app saves the root key HKEY_LOCAL_MACHINE:
#include <Windows.h>
 
int WINAPI WinMain(     HINSTANCE hInstance,
                                                                                HINSTANCE hPrevInstance,
                                                                                LPTSTR    lpCmdLine,
                                                                                int       nCmdShow)
{
                if( ERROR_SUCCESS != RegSaveKey( HKEY_LOCAL_MACHINE, TEXT("\\Storage Card\\SaveReg.srg"), NULL ))
                                RETAILMSG( 1, (TEXT("RegSaveKey failed (%d)\n"), GetLastError() ));
}
I had to add the error handling because the file is created as a System Hidden file. So at first it looked like the function had failed, then it actually did fail because the file already existed from the first time that I ran the application. RegSaveFile does not save the file as System or Hidden.
In this instance of RestoreReg, the app restores HKEY_LOCAL_MACHINE.
#include <Windows.h>
 
int WINAPI WinMain(     HINSTANCE hInstance,
                                                                                HINSTANCE hPrevInstance,
                                                                                LPTSTR    lpCmdLine,
                                                                                int       nCmdShow)
{
                if( ERROR_SUCCESS != RegReplaceKey( HKEY_LOCAL_MACHINE, NULL, TEXT("\\Storage Card\\SaveReg.srg"), NULL ))
                                RETAILMSG( 1, (TEXT("RegReplaceKey failed (%d)\n"), GetLastError() ));
                else
                {
                                KernelIoControl(IOCTL_HAL_REBOOT, NULL, 0, NULL, 0, NULL);
                }
}
For this application, I need to add the error handling because my first attempt included a subkey string, which is not valid.
So now we have two ways to save and restore the registry to a know version.
You may also want to look at Summary of Registry Posts
Tags: 
Copyright © 2008 – Bruce Eitman
All Rights Reserved