When using SaveKey method to export a registry key, it outputs a 0 byte file.
I have tried several approaches from the internet but w/o success..
Does any one has a correct working example for Delphi X5?
Thank you.
Reynaldo wrote:
> Now to test the saveKey using restoreKey I get reg.lasterror = 5.
That is ERROR_ACCESS_DENIED.
> Where can I find the meaning of 5?
MSDN documentation.
RegRestoreKey function
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724915.aspx
{quote}
If the function fails, the return value is a nonzero error code defined in
Winerror.h.
{quote}
System Error Codes
http://msdn.microsoft.com/en-us/library/windows/desktop/ms681381.aspx
{quote}
ERROR_ACCESS_DENIED5 (0x5)
Access is denied.
{quote}
> The procedure to Restore would just replace SeBackupPrivilege by
> SeRestorePrivilege for the privilege
Actually, RestoreKey needs both privileges, per the documentation:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724915.aspx
{quote}
The calling process must have the SE_RESTORE_NAME and SE_BACKUP_NAME privileges
on the computer in which the registry resides.
{quote}
--
Remy Lebeau (TeamB)
> {quote:title=Reynaldo Mola wrote:}{quote}
> Now to test the saveKey using restoreKey I get reg.lasterror = 5. Where can I find the meaning of 5?
> The procedure to Restore would just replace SeBackupPrivilege by SeRestorePrivilege for the privilege and reg.SaveKey by reg.RestoreKey.
> I'm missing something?
>
> begin
> if not NTSetPrivilege('SeRestorePrivilege',true) then
> exit;
>
> reg := TRegistry.Create;
> try
> reg.RootKey := HKEY_CURRENT_USER;
>
> if reg.OpenKey('software\dbadmintools\db2gdba\databases',false) then
> begin
> reg.CloseKey;
> reg.Access:=KEY_ALL_ACCESS;
> if not reg.RestoreKey('software\dbadmintools\db2gdba\databases', sKeyFileName) then
> ShowMessage('Error Restoring ' + intToStr(reg.LastError));
> end
> finally
> reg.Free;
> NTSetPrivilege('SeRestorePrivilege',false)
> end;
I found it: 5-- access denied. Any idea why saveKey works and RestoreKey doesn't?
Now to test the saveKey using restoreKey I get reg.lasterror = 5. Where can I find the meaning of 5?
The procedure to Restore would just replace SeBackupPrivilege by SeRestorePrivilege for the privilege and reg.SaveKey by reg.RestoreKey.
I'm missing something?
begin
if not NTSetPrivilege('SeRestorePrivilege',true) then
exit;
reg := TRegistry.Create;
try
reg.RootKey := HKEY_CURRENT_USER;
if reg.OpenKey('software\dbadmintools\db2gdba\databases',false) then
begin
reg.CloseKey;
reg.Access:=KEY_ALL_ACCESS;
if not reg.RestoreKey('software\dbadmintools\db2gdba\databases', sKeyFileName) then
ShowMessage('Error Restoring ' + intToStr(reg.LastError));
end
finally
reg.Free;
NTSetPrivilege('SeRestorePrivilege',false)
end;
> {quote:title=Remy Lebeau (TeamB) wrote:}{quote}
> karl wrote:
>
> > True, but it does letyou narrow down the errors when debugging - does
> > the key exist etc
>
> That is overkill when you can just use the TRegistry.LastError property to
> know why TRegistry.SaveKey() fails.
>
> --
> Remy Lebeau (TeamB)
I so don't care, I even explicitly said I write verbose code when playing with test code
Back on the plonk list you go... again
karl wrote:
> True, but it does letyou narrow down the errors when debugging - does
> the key exist etc
That is overkill when you can just use the TRegistry.LastError property to
know why TRegistry.SaveKey() fails.
--
Remy Lebeau (TeamB)
Reynaldo wrote:
> It worked for me. However I couldn't use the SaveKey function as is.
> I had to modify the Karl's code to use the win api instead.
That would translate to the following TRegistry code:
{code}
procedure TForm1.Button2Click(Sender: TObject);
var
Reg: TRegistry;
RootKey,phKey: hKey;
handle: Hwnd;
sKeyFileName: String;
begin
if not NTSetPrivilege('SeBackupPrivilege',true) then
Exit;
sKeyFileName := 'C:\temp\tempReg';
if FileExists(sKeyFileName) then
DeleteFile(sKeyFileName);
Reg := TRegistry.Create(KEY_ALL_ACCESS);
try
Reg.RootKey := HKEY_CURRENT_USER;
if Reg.SaveKey('software\dbadmintools\db2gdba\databases', sKeyFileName)
then
ShowMessage('BACKUP OK!')
else
ShowMessage('BACKUP ERROR: ' + IntToStr(Reg.LastError) + '!');
finally
Reg.Free;
end;
end;
{code}
--
Remy Lebeau (TeamB)
Hello Reynaldo,
> Well actually it worked using savekey, but I needed to request
> reg.Access:= KEY_ALL_ACCESS; If I remember though, embarcadero
> help says this function would open the key with all accesses.
Yes, and KEY_ALL_ACCESS requires admin rights, so you have to run your app
as an elevated admin if UAC is enabled.
--
Remy Lebeau (TeamB)
> {quote:title=Tom Brunberg wrote:}{quote}
> Reynaldo Mola wrote:
>
> > Well actually it worked using savekey, but I needed to request reg.Access:= KEY_ALL_ACCESS;
> > If I remember though, embarcadero help says this function would open the key with all accesses.
>
> I saw that both Karl and you called reg.OpenKey and RegOpenKey respectively. Using Delphis
> TRegistry that shouldn't be required at all. Didn't check the API docs. reg.SaveKey opens and if
> successful also closes the key.
> And yes, Delphis docs says it opens with KEY_ALL_ACCESS and that is my experience also.
> I simply used:
> {code}
> reg := TRegistry.Create;
> try
> result := reg.SaveKey(RegKey, FileName);
> finally
> reg.Free;
> end;
> {code}
>
> --
> Tom Brunberg
> firstname.lastname@welho.com
True, but it does letyou narrow down the errors when debugging - does the key exist etc
I usually write verbosely when doing test code so it's easy to step through and double check things