如何从以管理员

时间:2017-07-26 05:30:28

标签: installer inno-setup pascalscript

我的设置设置为以最低权限运行

PrivilegesRequired=lowest

但我正在执行设置为管理员(右键单击>以管理员身份运行,在UAC中输入管理员凭据),并想要检查登录用户的注册表在InitializeSetup()

function InitializeSetup(): boolean;
begin
  if RegQueryStringValue(HKCU,'SOFTWARE\{some path}','Version', {some value}) then
  begin
     { do something here }
  end
end

但是这会检查管理员帐户的注册表值,不是登录用户帐户

此时是否有办法检查登录用户注册表?

1 个答案:

答案 0 :(得分:0)

首先,您不应尝试从以管理员权限运行的安装程序访问用户环境。那是错的。

有关此主题的一般性讨论,请参阅:
Installing application for currently logged in user from Inno Setup installer running as Administrator

无论如何,你可以使用下面的功能。

代码结合了以下解决方案:

function ReqQueryValueOfOriginalUser(var ResultStr: String): Boolean;
var
  Uniq: string;
  TempFileName: string;
  Cmd: string;
  Key: string;
  Value: string;
  Params: string;
  Lines: TArrayOfString;
  Buf: string;
  ResultCode: Integer;
  P: Integer;
begin
  Log('Querying registry value of original user');
  Uniq := ExtractFileName(ExpandConstant('{tmp}'));
  TempFileName :=
    ExpandConstant(Format('{commondocs}\appdata-%s.txt', [Uniq]));
  Cmd := ExpandConstant('{cmd}');
  Key := 'HKEY_CURRENT_USER\Software\{some path}';
  Value := 'Version';
  Params := Format('/C reg.exe QUERY "%s" /v "%s" > "%s"', [Key, Value, TempFileName]);
  Result := False;
  if ExecAsOriginalUser(Cmd, Params, '', SW_HIDE, ewWaitUntilTerminated, ResultCode)
     and (ResultCode = 0) then
  begin
    if LoadStringsFromFile(TempFileName, Lines) then
    begin
      if (Length(Lines[0]) > 0) or
         (Lines[1] <> Key) then
      begin
        Log(Format('Unexpected output of reg.exe QUERY: "%s" - "%s"', [
          Lines[0], Lines[1]]));
      end
        else
      begin
        Buf := Trim(Lines[2]);
        if Copy(Buf, 1, Length(Value)) <> Value then
        begin
          Log(Format('Unexpected output of value query: "%s"', [Buf]));
        end
          else
        begin
          Buf := Trim(Copy(Buf, Length(Value) + 1, Length(Buf) - Length(Value)));
          P := Pos(' ', Buf);
          if P = 0 then
          begin
            Log(Format('Cannot find type and value separator in "%s"', [Buf]));
          end
            else
          begin
            ResultStr := Trim(Copy(Buf, P + 1, Length(Buf) - P));
            Log(Format('Value is "%s"', [ResultStr]));
            Result := True;
          end;
        end;
      end;
    end
      else
    begin
      Log(Format('Error reading %s', [TempFileName]));
    end;
    DeleteFile(TempFileName);
  end
    else
  begin
    Log('Error querying registry key of original user');
  end;

  Result := True;
end;