【问题标题】:Incrementing date to implement trial period using Inno Setup使用 Inno Setup 增加日期以实施试用期
【发布时间】:2019-01-03 14:33:04
【问题描述】:

我正在使用Inno Setup 编译器为我的软件创建安装程序。安装程序会在首次安装期间向 Windows 注册表添加时间戳。重新安装软件时,它会检查 Windows 注册表中保存的时间戳,如果距当前日期超过 90 天,是否应该停止安装?所以我强迫用户只使用该软件 90 天。

我正在尝试将 90 天添加到当前日期时间以进行比较。在数据类型TSystemTime 中没有执行此操作的选项。我可以将天数添加到 TDateTime 变量,但我不能在 Inno Setup 脚本中使用该变量。

这是我的代码

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;

我在 Stack Overflow 上看到过类似的 example。他们使用一个常数来比较日期时间。相反,我将 90 天添加到我保存的安装日期时间。

任何帮助将不胜感激。

【问题讨论】:

    标签: inno-setup pascalscript


    【解决方案1】:

    要增加TSystemTime,请检查performing Arithmetic on SYSTEMTIME

    虽然在 Inno Setup 中实现 128 位算术可能会很困难。


    或者,您可以自己实现它:

    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;
    

    像这样使用它:

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

    【讨论】:

    • 我收到以下错误 'Unknown identifier 'Inc'
    • 确保您使用的是 Unicode 版本的 Inno Setup。或将Inc(X) 替换为X := X + 1。尽管无论如何你都应该使用 Unicode 版本。
    • 我已经使用了 Unicode 版本的 Inno Setup 并且它现在可以工作了。
    猜你喜欢
    • 2023-03-21
    • 2017-08-15
    • 1970-01-01
    • 2010-12-01
    • 2013-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多