We often see that many trial versions of software downloaded online have usage time limits. From a commercial perspective, it is also a measure to protect software benefits. It allows users to try it for free for a period of time. If they are satisfied, they can purchase commercial software. . The example code function described in this article is how to add a time limit function to the program written in Delphi. The default time limit here is 30 days.
The main code is as follows:
unit Unit1;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Registry, Dialogs;type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.DFM}procedure TForm1.FormCreate(Sender: TObject);var registerTemp : TRegistry; curDate : TDateTime;begin registerTemp := TRegistry.Create; with registerTemp do begin RootKey := HKEY_LOCAL_MACHINE; //Determine whether to run the program for the first time if OpenKey('Software/MySoftware' ,True) then begin if ReadBool('Runned') then //This is not the first time to run begin curDate := Date; if (curDate-ReadTime('LastRunTime'))>=ReadInteger('Duration') then begin //The current system time exceeds the usage period ShowMessage('Trial version has expired'); exit; end else begin DeleteKey('LastRunTime'); WriteTime('LastRunTime',Date); end; end else begin //Run the program for the first time DeleteKey('Runned'); WriteBool('Runned',True); //Set the trial period for 30 days WriteInteger('Duration',30); //Write the current running time WriteTime('LastRunTime',Date); end; end else begin ShowMessage('Fails !'); end; CloseKey; end;end;end.