在线验证 运行 Inno Setup 安装程序的权限

Verify permissions to run Inno Setup installer online

我正在寻找 Inno Setup 的代码,我可以使用它来让我的安装程序验证我是否有权安装该程序。此代码必须检查 Web 服务器上的文本文件。

使用InitializeSetup event function to trigger your check using .

[Code]

const
  SubkeyName = 'Software\My Program';
  AllowInstallationValue = 'Allow Installation';

function IsInstallationAllowed: Boolean;
var
  Url: string;
  WinHttpReq: Variant;
  S: string;
  ResultDWord: Cardinal;
begin
  try
    WinHttpReq := CreateOleObject('WinHttp.WinHttpRequest.5.1');
    Url := 'https://www.example.com/can_install.txt';
    WinHttpReq.Open('GET', Url, False);
    WinHttpReq.Send('');
    if WinHttpReq.Status <> 200 then
    begin
      RaiseException(
        'HTTP Error: ' + IntToStr(WinHttpReq.Status) + ' ' + WinHttpReq.StatusText);
    end
      else
    begin
      S := Trim(WinHttpReq.ResponseText);
      Log('HTTP Response: ' + S);

      Result := (CompareText(S, 'true') = 0);

      if RegWriteDWordValue(
           HKLM, SubkeyName, AllowInstallationValue, Integer(Result)) then
        Log('Cached response to registry')
      else
        Log('Error caching response to registry');
    end;
  except
    Log('Error: ' + GetExceptionMessage);
    if RegQueryDWordValue(HKLM, SubkeyName, AllowInstallationValue, ResultDWord) then
    begin
      Log('Online check failed, using cached result');
      Result := (ResultDWord <> 0);
    end
      else
    begin
      Log('Online check failed, no cached result, allowing installation by default');
      Result := True;
    end;
  end;

  if Result then Log('Can install')
    else Log('Cannot install');
end;

function InitializeSetup(): Boolean;
begin
  Result := True;
  if not IsInstallationAllowed then
  begin
    MsgBox('You cannot install this', mbError, MB_OK);
    Result := False;
  end;
end;