1、利用注册表啊
第一次运行程序时,在注册表里创建初始信息。以后每次运行程序时,在程序中从注册表里读,并判断使用时间,超过时间就Application.Terminate就行了2、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; //判断是否初次运行程序 if OpenKey('Software\MySoftware',True) then begin if ReadBool('Runned') then //不是第一次运行 begin curDate := Date; if (curDate-ReadTime('LastRunTime'))>=ReadInteger('Duration') then begin //当前的系统时间超出了使用期限 ShowMessage('试用版已到期'); exit; end else begin DeleteKey('LastRunTime'); WriteTime('LastRunTime',Date); end; end else begin //初次运行程序 DeleteKey('Runned'); WriteBool('Runned',True); //设置试用期限30天 WriteInteger('Duration',30); //写入当前运行时间 WriteTime('LastRunTime',Date); end; end else begin ShowMessage('Fails!'); end; CloseKey; end;end;end.3、uses
Registry;const CKeyName = 'EncryptReg';function TMainForm.InstallRegInfo: Boolean;var Reg: TRegistry; G: TGuid; sGuid: string;begin Result := False; Reg := TRegistry.Create; try with Reg do begin RootKey := HKEY_CLASSES_ROOT; if KeyExists(CKeyName) then begin // 检测是否已经存在键名了 Result := False; Exit; end else begin if not CreateKey(CKeyName) then begin // 创建主键名——相当于文件夹 Result := False; Exit; end else begin if not OpenKey(CKeyName, False) then begin // 创建保存使用次数的键名 Result := False; Exit; end else begin // 创建GUID并写入键值 CreateGuid(G); sGuid := GuidToString(G); // 注意:sGuid中包括'{}'符号 sGuid[8] := '0'; sGuid[9] := '0'; // 从左到右,第8和9位作为计数器,将他们置零 WriteString(GuidToString(G), sGuid); Result := True; end; end; end; end; finally FreeAndNil(Reg); end;end;procedure TMainForm.UninstallRegInfo;var Reg: TRegistry;begin Reg := TRegistry.Create; try with Reg do begin RootKey := HKEY_CLASSES_ROOT; if not DeleteKey(CKeyName) then raise Exception.Create('Can''t delete registry info'); end; finally FreeAndNil(Reg); end;end;procedure TMainForm.IncreaseUsedTime;var Reg: TRegistry; sTime: string; iTime: Integer; sKeyName: string; KeyNameList: TStringList;begin Reg := TRegistry.Create; KeyNameList := TStringList.Create; try with Reg do begin RootKey := HKEY_CLASSES_ROOT; if not KeyExists(CKeyName) then raise Exception.Create('Missing registry info, program can''t run!') else begin if not OpenKey(CKeyName, False) then raise Exception.Create('Can''t read registry info, program can''t run!') else begin GetValueNames(KeyNameList); // 读取GUID键名,因为GUID是随机生成的。 if KeyNameList.Count <= 0 then raise Exception.Create('Not found regsitry key!') else begin sKeyName := KeyNameList.Strings[0]; // 因为只有一个键,所有读取索引为0的字符串就是我们想要的那个键值。 sTime := ReadString(sKeyName); if Length(Trim(sTime)) <> 38 then raise Exception.Create('Registry value error!') else begin iTime := HexToInt(sTime[8])*16 + HexToInt(sTime[9]); if iTime > 30 then raise Exception.Create('Thirty times is used. Please register!') else begin Inc(iTime); sTime[8] := IntToHex(iTime div 16); sTime[9] := IntToHex(iTime mod 16); WriteString(sKeyName, sTime); end; end; end; CloseKey; end; end; end; finally FreeAndNil(KeyNameList); FreeAndNil(Reg); end; end;function TMainForm.HexToInt(const cHex: Char): Byte;begin case cHex of '0'..'9': Result := StrToInt(cHex); 'a', 'A': Result := 10; 'b', 'B': Result := 11; 'c', 'C': Result := 12; 'd', 'D': Result := 13; 'e', 'E': Result := 14; 'f', 'F': Result := 15; else Result := 0; end;end;function TMainForm.IntToHex(const iInt: Integer): Char;begin case iInt of 0: Result := '0'; 1: Result := '1'; 2: Result := '2'; 3: Result := '3'; 4: Result := '4'; 5: Result := '5'; 6: Result := '6'; 7: Result := '7'; 8: Result := '8'; 9: Result := '9'; 10: Result := 'A'; 11: Result := 'B'; 12: Result := 'C'; 13: Result := 'D'; 14: Result := 'E'; 15: Result := 'F'; else Result := ' '; end;end;