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

时间:2018-08-30 18:42:03

标签: inno-setup pascalscript

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

  • 如果文件的值为“ False”,则安装程序必须取消安装并将该值保存在注册表文件中,以在无法使用Internet连接时始终取消该安装。
  • 如果文件的值为“ True”,则安装程序将继续安装,并且将删除注册表文件值(如果存在)。
  • 如果没有Internet并且注册表值不存在,则安装程序将继续安装。

1 个答案:

答案 0 :(得分:0)

使用InitializeSetup event function来使用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;