Search code examples
inno-setuppascalscript

Incrementing date to implement trial period using Inno Setup


I am using Inno Setup compiler to create an installer set up for my software. The installer adds a timestamp to the Windows registry during the first installation. When the software is reinstalled, it checks the saved timestamp in Windows registry and if it is more than 90 days from the current date then it should stop the installation? so I am forcing the user to use the software only for 90 days.

I am trying to add 90 days to the current datetime for comparison. There is no option to do this in the datatype TSystemTime. I can add days to a TDateTime variable, but I can't use that variable in the Inno Setup script.

This is my code

function InitializeSetup(): Boolean;
var
  InstallDatetime: string;
begin
  if RegQueryStringValue(HKLM, 'Software\Company\Player\Settings', 'DateTimeInstall', InstallDatetime) then
    { I want to add 90 days before comparison }
    Result := CompareStr(GetDateTimeString('yyyymmdd', #0,#0), InstallDatetime) <= 0;
  if not result then
    MsgBox('This Software trial period is over. The Program will not install.', mbError, MB_OK);
    Result := True;
end;

I have seen a similar example on Stack Overflow. They have used a constant to compare the datetime. Instead, I am adding 90 days to my saved installation datetime.

Any help will be much appreciated.


Solution

  • To increment TSystemTime, check performing Arithmetic on SYSTEMTIME.

    Though it would probably be difficult to implement 128-bit arithmetic in Inno Setup.


    Alternatively, you can implement it on your own:

    procedure IncDay(var Year, Month, Day: Integer);
    var
      DIM: Integer;
    begin
      Inc(Day);
      case Month of
        1, 3, 5, 7, 8, 10, 12: DIM := 31;
        2: if (Year mod 4 = 0) and ((Year mod 100 <> 0) or (Year mod 400 = 0)) then
             DIM := 29
           else
             DIM := 28;
        4, 6, 9, 11: DIM := 30;
      end;
      if Day > DIM then
      begin
        Inc(Month);
        Day := 1;
        if Month > 12 then
        begin
          Inc(Year);
          Month := 1;
        end;
      end;
    end;
    
    procedure IncDays(var Year, Month, Day: Integer; Days: Integer);
    begin
      while Days > 0 do
      begin
        IncDay(Year, Month, Day);
        Dec(Days);
      end;
    end;
    
    function IncDaysStr(S: string; Days: Integer): string;
    var
      Year, Month, Day: Integer;
    begin
      Year := StrToInt(Copy(S, 1, 4));
      Month := StrToInt(Copy(S, 5, 2));
      Day := StrToInt(Copy(S, 7, 2));
      IncDays(Year, Month, Day, Days);
      Result := Format('%.4d%.2d%.2d', [Year, Month, Day]);
    end;
    

    Use it like:

    S1 := GetDateTimeString('yyyymmdd', #0, #0);
    S2 := IncDaysStr(InstallDatetime, 90);
    Result := CompareStr(S1, S2) <= 0;