Windows CE: Save and Restore the Registry
2009-11-19 17:09
741 查看
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.
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.
相关文章推荐
- How to use the System Restore API to save and to restore system data in Visual C++
- The Real Best Practices to Save/Restore Activity's and Fragment's state.
- 安装2:cannot save the license information because access to the registry is denied......
- How to resize a *.jpg image and save the result to a file
- Permissions (Permission) for the Event log and registry
- [CE编程]第7章 Files, Databases, and the Registry (二)CE中的数据库API
- Dubbo Failed to save registry store file, cause: Can not lock the registry cache file
- LR安装遇到 Cannot save the license information because access to the registry is denied
- A quick complete tutorial to save and restore Tensorflow models
- 保存自定义对象的数组 save and restore an array of custom objects
- iOS-Restore the connection to "“XXX”的 iPho" and run "XXX" again, or if "XXX" is still running, you c
- tensorflow model save and restore
- NHibernate Cascades: the different between all, all-delete-orphans and save-update
- SimpleCV install and "You need the python image library to save by filehandle"
- a survey about how to make a file dynamically and offer it to download,but still not save the file!
- Dubbo Failed to save registry store file, cause: Can not lock the registry cache file
- vim save and restore session
- Which two are the uses of the ASM metadata backup and restore (AMBR) feature?
- View.onSaveInstanceState() and View.onRestoreInstanceState()
- Java04 save and read the .txt