{-----------------------------------------------------------------------------
Project : -
Description : Enhancement of TRegistry for impersonating the registry
Author : Michael Puff [http//michael-puff.de]
Date : 2012-01-05
-----------------------------------------------------------------------------}
unit RegistryEx;
interface
uses
Windows, SysUtils, Registry;
type
TRegistryEx =
class(TRegistry)
private
FUser:
string;
FPassword:
String;
function Impersonate: Boolean;
public
destructor Destroy;
override;
procedure ImpersonateUser(UserName:
string; Password:
string);
end;
implementation
{ TRegistryEx }
destructor TRegistryEx.Destroy;
begin
if not RevertToSelf
then
Halt;
// if revert fails kill process for safty reason
inherited;
end;
procedure TRegistryEx.ImpersonateUser(UserName:
string; Password:
string);
begin
FUser := UserName;
FPassword := Password;
if not Impersonate
then
RaiseLastOSError;
end;
function TRegistryEx.Impersonate: Boolean;
var
LogonType : Integer;
LogonProvider : Integer;
TokenHandle : THandle;
begin
LogonType := LOGON32_LOGON_INTERACTIVE;
LogonProvider := LOGON32_PROVIDER_DEFAULT
or LOGON32_PROVIDER_WINNT50;
Result := LogonUser(PChar(FUser),
nil, PChar(FPassword), LogonType, LogonProvider, TokenHandle);
if Result
then
begin
Result := ImpersonateLoggedOnUser(TokenHandle);
CloseHandle(TokenHandle);
end;
end;
end.