Windows CE: Save and Restore the Registry

Windows CE: Save and Restore the Registry
Created by samphung on 11/20/2010 10:54:27 PM

Save and Restore the Entire Registry   The functions RegCopyFile() and RegRestoreFi... 


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

 

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

 

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

 

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

 

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.

------------------------------------------------------------------------------------------------------------------------------

Note:  This article is written by Bruce Eitman, and is posted to the Embedded101 site with Bruce’s permission.

 

Copyright © 2010 – Bruce Eitman – All Rights Reserved

 

http://geekswithblogs.net/BruceEitman/

 

 

 

 

 

 

 

 

Embedded101 Articles

Click to Expand/Collapse Groups
Skip Navigation Links.
Collapse Windows Embedded Compact (CE)Windows Embedded Compact (CE)
Collapse Compact 7Compact 7
Collapse CE 6.0CE 6.0
Collapse CE 5.0CE 5.0
Collapse Platform Builder & OS DesignPlatform Builder & OS Design
Collapse BSP, OAL & BootloaderBSP, OAL & Bootloader
Collapse RegistryRegistry
Collapse Device DriverDevice Driver
Collapse File SystemFile System
Collapse Application DevelopmentApplication Development
IBW & ICE
Stream Driver with CEDriverWiz rev 0...
Connecting Visual Studio IDE to CE 6...
Platform Builder: Automatically Flus...
Windows CE: Save and Restore the Re...
Windows CE: Stream Interface Driver...
Windows CE: Persisting Registry Chan...
Windows CE: Enhanced BusEnum
Windows CE: Reading a String from th...
Windows CE: Displaying Disk Informa...
Windows CE: Formatting TFAT
Windows CE: C# Application to Format...
Build A Windows Network Projector wi...
Hive-Based Registry for CE 6.0
AutoLaunch for CE 6.0
Hello all
Filter Device Drivers
Asynchronous I/O request support
Configure Flash Storage to Launch Co...
CEDriverWiz Version 2.00 User Manual
CEDriverWiz V2.00: About this releas...
Installing CEDriverWiz for Visual St...
CEDriverWiz Version 2.00: An Example
CEDriverWiz Version 2.00: Using the ...
Collapse Windows Embedded StandardWindows Embedded Standard
Collapse NET Compact FrameworkNET Compact Framework
Windows Phone 7
Misc.
Turkish porno izle video site in rokettubeporno izle