Determine if Administrator account that runs eleva

2019-04-12 23:59发布

I am using PrivilegesRequired=lowest in my Inno Setup script. If setup is running elevated, i.e. IsAdminLoggedOn or IsPowerUserLoggedOn reports TRUE, how can I determine if the elevated user account is the same account from which setup was launched?

My script can do different things accordingly.

标签: inno-setup
1条回答
我命由我不由天
2楼-- · 2019-04-13 00:28

You can use WTSQuerySessionInformation to retrieve an account username for the current Windows logon session.

function WTSQuerySessionInformation(
  hServer: THandle; SessionId: Cardinal; WTSInfoClass: Integer; var pBuffer: DWord;
  var BytesReturned: DWord): Boolean;
  external 'WTSQuerySessionInformationW@wtsapi32.dll stdcall';

procedure WTSFreeMemory(pMemory: DWord);
  external 'WTSFreeMemory@wtsapi32.dll stdcall';

procedure RtlMoveMemoryAsString(Dest: string; Source: DWord; Len: Integer);
  external 'RtlMoveMemory@kernel32.dll stdcall';

const
  WTS_CURRENT_SERVER_HANDLE = 0;
  WTS_CURRENT_SESSION = -1;
  WTSUserName = 5;

function GetCurrentSessionUserName: string;
var
  Buffer: DWord;
  BytesReturned: DWord;
  QueryResult: Boolean;
begin
  QueryResult :=
    WTSQuerySessionInformation(
      WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSUserName, Buffer,
      BytesReturned);

  if not QueryResult then
  begin
    Log('Failed to retrieve username');
    Result := '';
  end
    else
  begin
    SetLength(Result, (BytesReturned div 2) - 1);
    RtlMoveMemoryAsString(Result, Buffer, BytesReturned);
    WTSFreeMemory(Buffer);
    Log(Format('Retrieved username "%s"', [Result]));
  end;
end;

(The code is for Unicode version of Inno Setup).


You can then compare the result against GetUserNameString.


You may need to add a domain name into the comparison.

查看更多
登录 后发表回答