在Inno Setup中在线验证日期是否使安装程序过期

时间:2018-02-02 06:34:15

标签: inno-setup pascalscript

我有Inno安装程序代码,用于在安装使用Inno Setup开发的设置时显示错误消息。当到期日期发生时,将显示错误消息。

代码如下:

const MY_EXPIRY_DATE_STR = '20171112'; { Date format: yyyymmdd }

function InitializeSetup(): Boolean;
begin
  //If current date exceeds MY_EXPIRY_DATE_STR then return false and exit Installer.
  result := CompareStr(GetDateTimeString('yyyymmdd', #0,#0), MY_EXPIRY_DATE_STR) <= 0;

  if not result then
    MsgBox('Due to some problem', mbError, MB_OK);
end;

现在我的问题是我想用互联网验证日期,而不是本地系统日期。

1 个答案:

答案 0 :(得分:1)

使用一些在线服务来检索时间(或构建自己的服务)。

请参阅Free Rest API to get current time as string (timezone irrelevant)

请确保使用HTTPS,以免绕过检查。

以下示例使用TimeZoneDB service

您必须设置自己的API密钥(在免费注册后获得)。

const
  TimezoneDbApiKey = 'XXXXXXXXXXXX';

function GetOnlineTime: string;
var
  Url: string;
  XMLDocument: Variant;
  XMLNodeList: Variant;
  WinHttpReq: Variant;
  S: string;
  P: Integer;
begin
  try
    { Retrieve XML from with current time in London }
    { See https://timezonedb.com/references/get-time-zone }
    WinHttpReq := CreateOleObject('WinHttp.WinHttpRequest.5.1');
    Url :=
      'https://api.timezonedb.com/v2/get-time-zone?key=' + TimezoneDbApiKey +
      '&format=xml&by=zone&zone=Europe/London';
    WinHttpReq.Open('GET', Url, False);
    WinHttpReq.Send('');
    if WinHttpReq.Status <> 200 then
    begin
      Log('HTTP Error: ' + IntToStr(WinHttpReq.Status) + ' ' + WinHttpReq.StatusText);
    end
      else
    begin
      Log('HTTP Response: ' + WinHttpReq.ResponseText);

      { Parse the XML }
      XMLDocument := CreateOleObject('Msxml2.DOMDocument.6.0');
      XMLDocument.async := False;
      XMLDocument.loadXML(WinHttpReq.ResponseText);

      if XMLDocument.parseError.errorCode <> 0 then
      begin
        Log('The XML file could not be parsed. ' + XMLDocument.parseError.reason);
      end
        else
      begin
        XMLDocument.setProperty('SelectionLanguage', 'XPath');
        XMLNodeList := XMLDocument.selectNodes('/result/formatted');
        if XMLNodeList.length > 0 then
        begin
          S := Trim(XMLNodeList.item[0].text);
          { Remove the time portion }
          P := Pos(' ', S);
          if P > 0 then
          begin
            S := Copy(S, 1, P - 1);
            { Remove the dashes to get format yyyymmdd }
            StringChange(S, '-', '');
            if Length(S) <> 8 then
            begin
              Log('Unexpected date format: ' + S);
            end
              else
            begin
              Result := S;
            end;
          end;
        end;
      end;
    end;
  except
    Log('Error: ' + GetExceptionMessage);
  end;

  if Result = '' then
  begin
    { On any problem, fallback to local time }
    Result := GetDateTimeString('yyyymmdd', #0, #0);
  end;
end;