一、程序启动时,通过搜索注册表,判断是否第一次运行和是否注册,来确定程序是否运行procedure TForm1.FormCreate(Sender: TObject);
var
Reg:Tregistry; KeyName,TempCode,TempName,TempStr:string;
Same:Integer;
FirstDate,NowDate:Tdatetime;
NumberOfDays:real;
begin
NoRegistry:=true;
NowDate:=date; //取得运行时系统日期
try
//创建注册表,有该键则读取,无则创建
Reg:=Tregistry.Create ;
Reg.RootKey :=hkey_local_machine;
KeyName:='Softwarejsjbxample';
if Reg.OpenKey(KeyName,true) then
begin
TempName:=Reg.ReadString('UsrName');
TempCode:=Reg.ReadString('Passwd');
//读取用户名,注册号
try
FirstDate:=Reg.ReadDate('Date');
//非第一次则读入第一次运行时间
except
Reg.WriteDate('Date',NowDate);
// 若为第一次运行,则写入系统日期
FirstDate:=NowDate;
end;
end ;
reg.CloseKey ;
finally
reg.Free ;
end;
TempStr:=Encrypt(TempName);
//通过自定义函数Encrypt()来获取密码
Same:=CompareText(TempStr,TempCode);
//比较密码
if TempName<>'' then
if Same=0 then
NoRegistry:=false;
//验证密码,通过NoRegistry为false
if NoRegistry then
begin //若未注册...
NumberOfDays:=Nowdate-FirstDate;
SpareDays:=round(31-NumberOfDays);
Label1.Caption :=FloatToStr(SpareDays);
if((NumberOfDays>31) or (NumberOfDays<-1)) then
begin
showmessage('程序未注册超过试用期或更改系统时间,将终止运行!');
application.Terminate;//超过30天,则禁止运行
end;
end;
end;
|