this slowpoke moves

Export Registry Key

Beispiel 1 :
uses Registry, ShellApi

//

function SetTokenPrivilege(const APrivilege: string; const AEnable: Boolean): Boolean;
var
  LToken: THandle;
  LTokenPriv: TOKEN_PRIVILEGES;
  LPrevTokenPriv: TOKEN_PRIVILEGES;
  LLength: Cardinal;
  LErrval: Cardinal;
begin
  Result := False;
  if OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, LToken) then
  try
    // Get the locally unique identifier (LUID) .
    if LookupPrivilegeValue(nil, PChar(APrivilege), LTokenPriv.Privileges[0].Luid) then
    begin
      LTokenPriv.PrivilegeCount := 1; // one privilege to set
      case AEnable of
        True: LTokenPriv.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
        False: LTokenPriv.Privileges[0].Attributes := 0;
      end;
      LPrevTokenPriv := LTokenPriv;
      // Enable or disable the privilege
      Result := AdjustTokenPrivileges(LToken, False, LTokenPriv, SizeOf(LPrevTokenPriv), LPrevTokenPriv, LLength);
    end;
  finally
    CloseHandle(LToken);
  end;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  LReg: TRegistry;
begin
  if not SetTokenPrivilege('SeBackupPrivilege', True) then
    Exit;
  LReg := TRegistry.Create(KEY_ALL_ACCESS);
  try
    LReg.RootKey := HKEY_CURRENT_USER;
    if not LReg.SaveKey('Software\Microsoft\Notepad', 'C:\Notepad.reg') then
      ShowMessage('Could not save');
  finally
    LReg.Free;
  end;
end;
Beispiel 2 :
uses Registry

//

function SetPrivilege(Privilege: PChar; EnablePrivilege: Boolean; out PreviousState: Boolean): DWORD;
var
  Token: THandle;
  NewState: TTokenPrivileges;
  Luid: TLargeInteger;
  PrevState: TTokenPrivileges;
  Return: DWORD;
begin
  PreviousState := True;
  if (GetVersion() > $80000000) then
    // Win9x
    Result := ERROR_SUCCESS
  else
  begin
    // WinNT
    if not OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED, Token) then
      Result := GetLastError()
    else
      try
        if not LookupPrivilegeValue(nil, Privilege, Luid) then
          Result := GetLastError()
        else
        begin
          NewState.PrivilegeCount := 1;
          NewState.Privileges[0].Luid := Luid;
          if EnablePrivilege then
            NewState.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED
          else
            NewState.Privileges[0].Attributes := 0;
          if not AdjustTokenPrivileges(Token, False, NewState, SizeOf(TTokenPrivileges), PrevState, Return) then
            Result := GetLastError()
          else
          begin
            Result := ERROR_SUCCESS;
            PreviousState := (PrevState.Privileges[0].Attributes and SE_PRIVILEGE_ENABLED <> 0);
          end;
        end;
      finally
        CloseHandle(Token);
      end;
  end;
end;

function RegSaveKeyToFile(Key: HKEY; const SubKey, FileName: string): DWORD;
const
  SE_BACKUP_NAME = 'SeBackupPrivilege';
var
  PreviousState: Boolean;
  KeyHandle: HKEY;
begin
  Result := SetPrivilege(SE_BACKUP_NAME, True, PreviousState);
  if (Result = ERROR_SUCCESS) then
    try
      KeyHandle := 0;
      Result := RegOpenKeyEx(Key, PChar(SubKey), 0, MAXIMUM_ALLOWED, KeyHandle);
      if (Result = ERROR_SUCCESS) then
        try
          // FIXME: Short Filename on Win9x!
          Result := RegSaveKey(KeyHandle, PChar(FileName), nil);
        finally
          RegCloseKey(KeyHandle);
        end;
    finally
      if (not PreviousState) then
        SetPrivilege(SE_BACKUP_NAME, PreviousState, PreviousState);
    end;
end;

function RegLoadKeyFromFile(Key: HKEY; const SubKey, FileName: string): DWORD;
const
  SE_BACKUP_NAME = 'SeBackupPrivilege';
  SE_RESTORE_NAME = 'SeRestorePrivilege';
var
  PrevBackup: Boolean;
  PrevRestore: Boolean;
  KeyHandle: HKEY;
  ShortName: array [0 .. MAX_PATH] of Char;
begin
  Result := SetPrivilege(SE_BACKUP_NAME, True, PrevBackup);
  if (Result = ERROR_SUCCESS) then
    try
      Result := SetPrivilege(SE_RESTORE_NAME, True, PrevRestore);
      if (Result = ERROR_SUCCESS) then
        try
          if (GetVersion() > $80000000) then
          begin // Win9x (FIXME: Test it! - and see RegReplaceKey)
            if (GetShortPathName(PChar(FileName), ShortName, MAX_PATH) = 0) then
              Result := GetLastError()
            else
              Result := RegLoadKey(Key, PChar(SubKey), ShortName);
          end
          else
          begin // WinNT (FIXME: Load RegRestoreKey dynamically!)
            KeyHandle := 0;
            Result := RegOpenKeyEx(Key, PChar(SubKey), 0, MAXIMUM_ALLOWED, KeyHandle);
            if (Result = ERROR_SUCCESS) then
              try
                Result := RegRestoreKey(KeyHandle, PChar(FileName), 0);
              finally
                RegCloseKey(KeyHandle);
              end;
          end;
        finally
          if (not PrevRestore) then
            SetPrivilege(SE_RESTORE_NAME, PrevRestore, PrevRestore);
        end;
    finally
      if (not PrevBackup) then
        SetPrivilege(SE_BACKUP_NAME, PrevBackup, PrevBackup);
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
const
  Key = HKEY(HKEY_LOCAL_MACHINE);
  SubKey = 'Software\Microsoft\Notepad';
var
  FileName: string;
  ErrorCode: DWORD;
begin
  SetLength(FileName, MAX_PATH + 1);
  SetLength(FileName, GetTempPath(MAX_PATH, PChar(FileName)));
  FileName := 'C:\notepad.reg';
  ErrorCode := RegSaveKeyToFile(Key, SubKey, FileName);
  if (ErrorCode <> ERROR_SUCCESS) then
    ShowMessage('Save: ' + SysErrorMessage(ErrorCode))
  else
  begin
    ErrorCode := RegLoadKeyFromFile(Key, SubKey, FileName);
    if (ErrorCode <> ERROR_SUCCESS) then
      ShowMessage('Load: ' + SysErrorMessage(ErrorCode))
    else
      ShowMessage(IntToStr(42));
  end;
end;

Keine Kommentare:

Kommentar veröffentlichen

Beliebte Posts

Translate