Главная страница | назад





Article #10348: Using TRegisty::SaveKey and TRegistry::LoadKey

Question: How do I use the TRegistry::SaveKey and TRegistry::LoadKey functions to backup registry keys?
Solution: This is an example of how to use TRegisty::LoadKey and TRegistry::SaveKey for making backups of your registry. These functions store the data in what is called a hive file. Once saved the Key can be loaded (along with all its sub_keys back into the registry. This function has very limited uses because of the following:

a) A key that has been saved and then reloaded with load can not be deleted, it can only be programatically removed using UnLoadKey.

b) Save file can only save to files that don't have an extenstion, and the file can not exist.

c) LoadKey can only work when it is loading to a key that does not already exist.

Code: /*Function SaveKey: [in] String sFileName = file to save to

[in] String sKeyName = key to save

*/

void SaveKey (String sFileName, String sKeyName){

//You can only use HKEY_LOCAL_MACHINE and HKEY_USERS as

//root when using the TRegistry::SaveKey & TRegistry::LoadKey methods

//reg->RootKey = HKEY_LOCAL_MACHINE;

//you cannot save a Key to a file that exists. So, here I am

//checking to see if the file "reg" exists

//note: you cannot save to a file with an extention!!

//see if file reg exists, if exists ask if

//it shouold be deleted

if (access (sFileName.c_str (), 00) == 0){

int r = Application->MessageBox (

"File already exists. Should it"

" be deleted?","File Exists",

MB_YESNO);

//if it should be deleted,delete it

if (r == IDYES){

//reg is created by

//call to

//TRegistry::SaveKey

//so its attributes

//are set to readonly

//and hidden. Before I

//can delete i need to

//change these

//attributes

if (SetFileAttributes (sFileName.c_str (), FILE_ATTRIBUTE_NORMAL)== 0){

ShowMessage ("Unable to set modes on" + sFileName);

}

//delete the file

if(!DeleteFile(sFileName))

ShowMessage ("DeleteFile failed");

}

else

ShowMessage ("Key " + sKeyName + " cannot be saved while " + sFileName + "file exists");

}

//if you are on NT Make sure that

//you have the privleges to do this

OSVERSIONINFO osv;

::GetVersionEx (&osv);

if (osv.dwPlatformId == VER_PLATFORM_WIN32_NT){

TOKEN_PRIVILEGES tp;

HANDLE hToken;

LUID luid;

if (!::OpenProcessToken (::GetCurrentProcess (), TOKEN_ADJUST_PRIVILEGES, &hToken)){

ShowMessage ("OpenProcessToken: Failure");

Application->Terminate ();

}

if(!LookupPrivilegeValue(NULL, SE_BACKUP_NAME, luid)){

ShowMessage ("LookupPrivilegeValue: Failure");

Application->Terminate ();

}

tp.PrivilegeCount = 1;

tp.Privileges[0].Luid =luid;

tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

::AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES),NULL, NULL );

if (::GetLastError() != ERROR_SUCCESS){

ShowMessage ("AdjustTokenPrivileges: Failure");

Application->Terminate ();

}

}

//Save the HKEY_LOCAL_MACHINE&#092Software&#092Foo keys to the file .&#092reg

if (reg->SaveKey (sKeyName, sFileName))

ShowMessage ("Save Complete");

else{

ShowMessage ("Save Incomplete");

}

}

/* Function LoadKey: [in] sFileName = File to load from

[in] sKeyName = key to load to */

void LoadKey (String sFileName, String sKeyName){

if (reg->LoadKey (sKeyName.c_str (), sFileName.c_str()))

ShowMessage ("Key " + sKeyName + " is now reloaded. If you want to&#092nremove the key you have to use TRegistry::UnLoadKey do it.&#092nOne of the problems with TRegisty::SaveKey and LoadKey&#092nis that you cannot unload the key then unless you do it&#092nprogramatically." );

else

ShowMessage ("RegLoadKey: Failure");

}

Last Modified: 05-OCT-99