我们经常看到很多网上下载的试用版软件,都有使用时间的限制,就其商业角度而言也是处于软件效益保护的一种措施,可以让用户免费试用一段时间,若满意就可以购买商业软件。本文所述实例代码功能就是如何为Delphi所编写的程序添加使用时间的限制功能,这里默认的时限为30天。
主要代码如下:
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);varregisterTemp : TRegistry;curDate : TDateTime;beginregisterTemp := TRegistry.Create;with registerTemp dobeginRootKey := HKEY_LOCAL_MACHINE;//判断是否初次运行程序if OpenKey("SoftwareMySoftware",True) thenbeginif ReadBool("Runned") then//不是第一次运行begincurDate := Date;if (curDate-ReadTime("LastRunTime"))>=ReadInteger("Duration") thenbegin//当前的系统时间超出了使用期限ShowMessage("试用版已到期");exit;endelsebeginDeleteKey("LastRunTime");WriteTime("LastRunTime",Date);end;endelsebegin//初次运行程序DeleteKey("Runned");WriteBool("Runned",True);//设置试用期限30天WriteInteger("Duration",30);//写入当前运行时间WriteTime("LastRunTime",Date);end;endelsebeginShowMessage("Fails!");end;CloseKey;end;end;end.