1
votes

I have Inno Setup code to show error message when installing a setup developed using Inno Setup. The error message will shown when date of expire happens.

The code is as follows:

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;

Now my question is that I want verify the date using internet, not by local system date.

1

1 Answers

1
votes

Use some online service to retrieve the time (or build your own service).

See Free Rest API to get current time as string (timezone irrelevant).

Make sure, you use HTTPS, so that it not easy to bypass the check.

The following example uses TimeZoneDB service.

You have to set your own API key (that you get after a free registration).

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;