简体   繁体   English

如何从以管理员身份运行的Inno Setup安装程序中读取登录用户的注册表HKCU

[英]How to read registry HKCU for logged In user from Inno Setup installer running as administrator

My setup is set to run with lowest privileges 我的设置设置为以最低特权运行

PrivilegesRequired=lowest

But I'm executing setup as Admin (right click-> run as admin, enter Admin credential in UAC), and want to check Logged In User's registry in InitializeSetup() 但是我以管理员身份执行设置(右键单击->以管理员身份运行,在UAC中输入管理员凭据),并想在InitializeSetup()检查“ 登录用户”的注册表

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

But this checks the registry value for the Admin Account , not for Logged In User Account 但这会检查管理员帐户的注册表值, 而不是登录用户帐户的注册表值

Is there a way to check the logged in users registry at this point? 现在有没有办法检查登录的用户注册表?

First, you should not try to access a user environment from an installer running with Administrator privileges. 首先,您不应尝试从以管理员权限运行的安装程序访问用户环境。 That's just wrong. 那是错误的。

For a general discussion on this topic, see: 有关此主题的一般讨论,请参见:
Installing application for currently logged in user from Inno Setup installer running as Administrator . 从以管理员身份运行的Inno Setup安装程序为当前登录用户安装应用程序


Anyway, you can use a function below. 无论如何,您可以使用下面的功能。

The code combines these solutions: 该代码结合了以下解决方案:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM