本文介绍了确定运行提升的Inno Setup安装程序的Administrator帐户是否与当前Windows登录会话的帐户相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Inno Setup脚本中使用PrivilegesRequired=lowest.如果安装程序运行提升权限,即IsAdminLoggedOn or IsPowerUserLoggedOn报告TRUE,如何确定提升的用户帐户是否与启动安装程序的帐户相同?

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.

推荐答案

您可以使用 WTSQuerySessionInformation 来检索当前Windows登录会话的帐户用户名.

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;

(该代码用于 Inno Setup的Unicode版本 – Inno Setup 6唯一的版本)

(The code is for Unicode version of Inno Setup – The only version as of Inno Setup 6).

然后可以将结果与 GetUserNameString 进行比较.

You can then compare the result against GetUserNameString.

您可能需要在比较中添加域名.

这篇关于确定运行提升的Inno Setup安装程序的Administrator帐户是否与当前Windows登录会话的帐户相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 05:44