How to read registry HKCU for logged In user from

2019-06-10 01:37发布

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

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?

1条回答
Juvenile、少年°
2楼-- · 2019-06-10 01:47

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.


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;
查看更多
登录 后发表回答