/*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\Software\Foo keys
to the file .\reg
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\nremove the key you
have to use TRegistry::UnLoadKey do it.\nOne
of the problems with TRegisty::SaveKey and
LoadKey\nis that you cannot unload the key
then unless you do it\nprogramatically."
);
else
ShowMessage
("RegLoadKey: Failure");
}
|