Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delphi Registry Functions

Status
Not open for further replies.

WizardSuns

Programmer
Jan 27, 2004
1
SI
Hi!


My code - Saving registry key into a separate file:

This code does not work in Win9xME. The problem is that the registry key is not saved into a file. What could be the problem?

...
var
phKey: hKey;
FileName: array [0..255] of Char;
ErrorMsg: Integer;
begin
// adjust registry privileges only if OS is WinNT2kXP
if (CheckOSVersion = VER_PLATFORM_WIN32_NT) then SetRegistryPrivilege ('SeBackupPrivilege');
// show save as dialog box
if (Dialog_Save_As.Execute) then
begin
RegOpenKeyEx (ROOT_KEY, PChar (SAVE_LOAD_KEY), 0, KEY_ALL_ACCESS, phKey);
StrPCopy (FileName, Dialog_Save_As.FileName);
ErrorMsg := RegSaveKey (phKey, FileName, nil);
if (ErrorMsg = 0) then
begin
ShowMessage ('Hive file named '''+ FileName +''' was successfully saved.');
end
else
begin
if (ErrorMsg = 1314) then MessageDlg ('You do not have the privileges to save a hive file.', mtError, [mbOk], 0)
else if (ErrorMsg = 1016) then MessageDlg ('An I/O operation initiated by the registry failed unrecoverably. File path might be too long or contains unusual characters.', mtError, [mbOk], 0)
else if (ErrorMsg = 183) then MessageDlg ('File already exists. Please, write a new name for a hive file.', mtError, [mbOk], 0)
else MessageDlg ('Error occured while saving a hive file. Error: '+ IntToStr (ErrorMsg) +'.', mtError, [mbOk], 0);
end;
RegCloseKey (phKey);
end;
...

My code - Loading registry keys from a file into registry:


Problem is here in the RegRestoreKey which is not supported in Win9xME, but RegRestoreKey and RegLoadKey are not working too.
What is the problem, I do not know. Anyone knows the solution??

...
var
phKey: hKey;
FileName: array [0..255] of char;
ErrorMsg: Integer;
begin
// adjust registry privileges only if OS is WinNT2kXP
if (CheckOSVersion = VER_PLATFORM_WIN32_NT) then SetRegistryPrivilege ('SeRestorePrivilege');
// show load dialog box
if (Dialog_Load.Execute) then
begin
RegOpenKeyEx (ROOT_KEY, PChar (SAVE_LOAD_KEY), 0, KEY_ALL_ACCESS, phKey);
StrPCopy (FileName, Dialog_Load.FileName);
ErrorMsg := RegRestoreKey (phKey, FileName, 0);
if (ErrorMsg = 0) then
begin
ShowMessage ('Hive file named '''+ FileName +''' was successfully loaded.');
// update main form
Dialog_MKH.FormCreate (Sender);
end
else
begin
if (ErrorMsg = 1314) then MessageDlg ('You do not have the privileges to load a hive file into registry.', mtError, [mbOk], 0)
else if (ErrorMsg = 1017) then MessageDlg ('File '''+ FileName +''' is not a hive file.', mtError, [mbOk], 0)
else if (ErrorMsg = 1010) then MessageDlg ('Configuration registry key in a hive file is invalid.', mtError, [mbOk], 0)
else if (ErrorMsg = 120) then MessageDlg ('Function is not supported on this system.', mtError, [mbOk], 0)
else if (ErrorMsg = 5) then MessageDlg ('Access to the registry is denied. Close regedit or any other registry editing tool.', mtError, [mbOk], 0)
else MessageDlg ('Error occured while loading a hive file into registry. Error: '+ IntToStr (ErrorMsg) +'.', mtError, [mbOk], 0);
end;
RegCloseKey (phKey);
end;
...

Please note, that this code is perfectly working on WinNT2kXP.

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top