[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 181
  • Last Modified:

Registry Backup on win9x

Hi experts,

I have a problem, I found folow function to backup registry but doesn't work on Win9x this work fine on
win2k/winxp, the function is:

procedure TForm1.SaveKeyToFile(Key, _File: Pchar; _default:boolean);
const
  SE_BACKUP_NAME = 'SeBackupPrivilege';
  SE_RESTORE_NAME = 'SeRestorePrivilege';

var
  MainKey: HKey;
  TP, Prev: TTokenPrivileges;
  RetLength: DWORD;
  Token: THandle;
  LUID: TLargeInteger;
  x: integer;
  i: PDWORD;
begin

   x:=RegOpenKeyEx(HKEY_LOCAL_MACHINE, Key, 0, KEY_QUERY_VALUE, MainKey);
  if x = ERROR_SUCCESS then
  begin
    try
      // under WinNT/Win2K you need SE_BACKUP_NAME privilege to save and restore registry keys
      SetLastError(0);
      // enable backup privilege
      if OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, Token) then
      begin
        if LookupPrivilegeValue(nil, SE_BACKUP_NAME, LUID) then
        begin
          TP.PrivilegeCount := 1;
          TP.Privileges[0].Luid := LUID;
          TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
          if not AdjustTokenPrivileges(Token, False, TP, SizeOf(TTokenPrivileges), Prev, RetLength) then RaiseLastWin32Error;
          if FileExists(_File) then DeleteFile(_File);
          IF RegSaveKey(MainKey,_File, nil)= 0 THEN
             showmessage('OK ')
          else
             showmessage('Fallo backup. '+SystemErrorMessage(RegSaveKey(MainKey,_File, nil)));
          // Revoke all privileges this process holds (including backup)
          AdjustTokenPrivileges(Token, True, TP, 0, Prev, RetLength);

         // close handle to process token
         CloseHandle(Token);
        end;
      end
    finally
      RegCloseKey(MainKey);
    end;
  end


end;

procedure TForm1.Button1Click(Sender: TObject);
begin
         SaveKeyToFile(pchar('SOFTWARE'),pchar('C:\SOFTWARE'),false);
end;

Can any body helpme with howto backup registry on win9x with same function ?

Thanks in advance.

0
MauricioGaviria
Asked:
MauricioGaviria
  • 2
2 Solutions
 
Wim ten BrinkCommented:
In Win9x you don't need to use any security garbage, so you could discard the privileges-related stuff... This might be enough: (I can't test it though.)

procedure TForm1.SaveKeyToFile(Key, _File: Pchar; _default:boolean);
var
  MainKey: HKey;
  RetLength: DWORD;
  LUID: TLargeInteger;
  x: integer;
  i: PDWORD;
begin
   x:=RegOpenKeyEx(HKEY_LOCAL_MACHINE, Key, 0, KEY_QUERY_VALUE, MainKey);
  if x = ERROR_SUCCESS then
  begin
    try
      if FileExists(_File) then DeleteFile(_File);
      IF RegSaveKey(MainKey,_File, nil)= 0 THEN
         showmessage('OK ')
      else
         showmessage('Fallo backup. '+SystemErrorMessage(RegSaveKey(MainKey,_File, nil)));
    finally
      RegCloseKey(MainKey);
    end;
  end
end;
0
 
Wim ten BrinkCommented:
Should be:

var
  MainKey: HKey;
  x: integer;

The other variables aren't required. Still, I don't have any Win9x so I can't test this code...
0
 
MadshiCommented:
Alternatively in win9x you can simply copy the system.dat and user.dat files by using CopyFile, I believe.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now