this slowpoke moves

Check Logon for correctness

const
  USER = 'hackbard'; // Den Namen des Users eintragen
  PW   = 'Geheim';   // Das Logon Passwort eintragen
  
//

function GetCurrUserName: string;
var
  Size              : DWORD;
begin
  Size := MAX_COMPUTERNAME_LENGTH + 1;
  SetLength(Result, Size);
  if GetUserName(PChar(Result), Size) then
    SetLength(Result, Size)
  else
    Result := '';
end;

function Impersonate(const User, PW: string): Boolean;
var
  LogonType         : Integer;
  LogonProvider     : Integer;
  TokenHandle       : THandle;
  strAdminUser      : string;
  strAdminDomain    : string;
  strAdminPassword  : string;
begin
  LogonType := LOGON32_LOGON_INTERACTIVE;
  LogonProvider := LOGON32_PROVIDER_DEFAULT;
  strAdminUser := USER;
  strAdminDomain := '';
  strAdminPassword := PW;
  Result := LogonUser(PChar(strAdminUser), nil,
    PChar(strAdminPassword), LogonType, LogonProvider, TokenHandle);
  if Result then
  begin
    Result := ImpersonateLoggedOnUser(TokenHandle);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if Impersonate(USER, PW) then
  begin
    ShowMessage(GetCurrUserName);
    RevertToSelf;
    ShowMessage(GetCurrUserName);
  end
  else
    MessageBox(Handle,
               PChar(SysErrorMessage(GetLastError)),
               'Fehler',
               MB_ICONSTOP);
end;
 

Keine Kommentare:

Kommentar veröffentlichen

Beliebte Posts

Translate