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\Borland\Locales';
var
Filename:
string;
ErrorCode: DWORD;
begin
SetLength(Filename, MAX_PATH + 1);
SetLength(Filename, GetTempPath(MAX_PATH, PChar(Filename)));
Filename := Filename + '
Locales.dat';
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;
DeleteFile(Filename);
end;