我正在使用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;
我在堆栈溢出中看到了类似的example。他们使用常数比较日期时间。相反,我要在保存的安装日期时间中增加90天。
任何帮助将不胜感激。
答案 0 :(得分: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;