Verify permissions to run Inno Setup installer online

社会主义新天地 提交于 2019-11-30 17:03:55

Use InitializeSetup event function to trigger your check using HTTP request.

[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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!