如何从以管理员身份运行的Inno Setup安装程序中读取登录用户的注册表HKCU [英] How to read registry HKCU for logged In user from Inno Setup installer running as administrator

查看:170
本文介绍了如何从以管理员身份运行的Inno Setup安装程序中读取登录用户的注册表HKCU的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My setup is set to run with lowest privileges

PrivilegesRequired=lowest

但是我正在以 Admin (右键单击->以admin身份运行,在UAC中输入Admin凭据)执行安装程序,并想在其中检查已登录用户的注册表InitializeSetup()

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()

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.

有关此主题的一般讨论,请参见:
从以管理员身份运行的Inno Setup安装程序中为当前登录的用户安装应用程序.

For a general discussion on this topic, see:
Installing application for currently logged in user from Inno Setup installer running as Administrator.

无论如何,您可以使用下面的功能.

Anyway, you can use a function below.

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

The code combines these solutions:

  • Inno Setup Creating registry key for logged in user (not admin user)
  • Inno Setup always installs into admin's AppData directory
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;

这篇关于如何从以管理员身份运行的Inno Setup安装程序中读取登录用户的注册表HKCU的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆