软件试用期及试用次数控制(附部分关键代码)!

其实网上已经有不少现成的软件,它们可以帮你做到一些共享软件的时间控制,比如试用30天,试用100次等。前些天给台湾人写的软件现在需要要一个试用版,由于是繁体,想用网上已有的软件<<幻影加密系统>>来做,但在繁体下一运行就把机子搞死了,害得我新装了繁体2000。后来想想还是自己写算了。
对方要求试用90天,150次。本来也想用时间同步的方法来实现(以前也都实现了),但考虑到一些问题,想想没有那些必要,反正这也不是什么重要的东西,也就好弃了。我的思路是这样的,在安装软件时,取得系统时间 ,加密(我采用DES),写入注册表,写入INI文件(两手准备,加强安全)。文件和注册表都写入相同的数据,如果它们不相同,那么软件就不能运行,写入项包括安装时间,使用次数以及使用时间等。软件运行时检查时间是否全法,文件和注册表都写入的数据是否相同,如果有问题就不让再使用,并且删除ini文件和注册表的部分数据(没有全部删除,以防止再次安装)。申明本方法并不是安全的,只是为了应付一般的要求而已,如果需要高安全性,请使用其它的方法。

下面的代码简单演示了各部分的处理过程:

一、安装时写入加密数据:

(这里的加密使用了控件)

DES->GenerateKey("neowarton20030731");
TDateTime *d=new TDateTime(Date());
AnsiString times,date,filename;;
char dir[256];

times="0";
date=DateTimeToStr(*d);

times=DES->EncryptString(times);
date=DES->EncryptString(date);
//install=DES->EncryptString(date);
AnsiString s=times+date;
OutputDebugString(s.c_str());

filename=" \\mysoft.ini ";
GetSystemDirectory(dir,sizeof(dir));
filename=dir+filename;

if(FileExists(filename))
{
Application->Terminate(); //if the ini file is alread exists exit;
}
else
{
TIniFile *ini=new TIniFile(filename);
ini->WriteString("setup","times",times);
ini->WriteString("setup","date",date);
ini->WriteString("setup","install",date);

delete ini;

//write register

TRegistry *reg=new TRegistry();
reg->RootKey=HKEY_LOCAL_MACHINE;
if(reg->KeyExists(" \\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\mysoft "))
Application->Terminate();//if the key exists,terminate
reg->OpenKey(" \\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\mysoft",true );
reg->WriteString("warning","dot modify these items!Otherwise,your system will not run!neowarton2003xian");
reg->WriteString("times",times);
reg->WriteString("date",date);
reg->WriteString("install",date);
reg->CloseKey();

delete reg;
}

二、安装时在install Shield中加入以下代码:

Lauchapp(SUPPORTDIR^"test.exe","") //test为上面代码的exe文件
其它代码省略

三、在应用程序起动时:

bool rt;
TForm1 *fm1=new TForm1(NULL);//试用版弹出的窗口,点击试用返回判断结果
fm1->ShowModal();
rt=fm1->try_time;//是否已经过期
delete fm1;
if(rt)
{
MessageBox(NULL,"¥»³nÅé¸Õ¥Î´Á¤v¹L¡A§A¤£¯à¦A¨Ï¥Î¸Óª©¥»¡A½ÐÁʶR¥¿¦¡ª©¡I\n\n http://www.xxxxx.com ","¯«²þ¥D¾÷ºôµ¸¨¾¤õÀð",MB_OK+MB_ICONINFORMATION);
Application->Terminate();//Ãö³¬À³¥Îµ{§Ç
return 0;
}

四、用户点击试用按钮,返回是否过期的bool值:

//read the encrypted ini file in the system directory to read the time and date
//if filenote exist,return false,if the time and date wrong return false;

//read data and then time from the register,where the data is also encrypted

char dir[256];
AnsiString filename=" \\mysoft.ini ";
GetSystemDirectory(dir,sizeof(dir));

//the ini fils is in the system directory
filename=dir+filename;
if(!FileExists(filename))
{
Application->MessageBox("§Aªº¨t²Î¹ï¥»³n¥ó¶i¦æ¤F«Dªk­×§ï¡A¤w¸g¤£¯à¦A¨Ï¥Î¡I\n\n ","¯«²þ¥D¾÷¨¾¤õÀð",MB_ICONERROR);
HWND H;
H=FindWindow(NULL,"DNAAlarm");
::SendMessage(H,WM_CLOSE,0,0);
OutputDebugString("go here");
this->try_time=true;
Close();
return;
}

TIniFile *ini=new TIniFile(filename);
AnsiString times,datetime,installday;

times=ini->ReadString("setup","times","");
datetime=ini->ReadString("setup","date","");
installday=ini->ReadString("setup","install","");
getthepara();

AnsiString tt=times_reg+"|"+datetime_reg+"|"+datetime_reg;
AnsiString ttt=times+"|"+datetime+"|"+installday;

if((times_reg!=times)||(datetime_reg!=datetime)||(installday!=datetime_reg))
{

this->try_time=true;
}
else //------------------------------------
{

DES->GenerateKey("neowarton20030731");
times=DES->DecryptString(times);
datetime=DES->DecryptString(datetime);
installday=DES->DecryptString(installday);

times_reg=DES->DecryptString(times_reg);
datetime_reg=DES->DecryptString(datetime_reg);
installday_reg= DES->DecryptString(installday_reg);

tt=times_reg+"|"+datetime_reg+"|"+datetime_reg;
ttt=times+"|"+datetime+"|"+installday;
//¦r²Å¦êÂà´«¦¨¤é´Á
int t=StrToInt(times);
TDateTime *dt=new TDate(Date());

TDateTime *s=new TDate(StrToDate(datetime)); TDateTime *install=new TDate(StrToDate(installday));

int tmp=*dt-*s;
int tmp2=*dt-*install;
//int tmp3=*s-*dt;

if((tmp>=-1)&&(tmp<=90)&&(tmp2>=-1)&&(tmp2<=90)&&(t<150))
{
AnsiString times_tmp=DES->EncryptString(IntToStr(StrToInt(times)+1));
AnsiString datetime_tmp=DES->EncryptString(DateTimeToStr(*dt));
ini->WriteString("setup","times",times_tmp);
ini->WriteString("setup","date",datetime_tmp);
putthepara(times_tmp,datetime_tmp);
this->try_time=false;
}
else
{
//write wrong time;

AnsiString times_tmp=DES->EncryptString(IntToStr(StrToInt(times)+200));
AnsiString datetime_tmp=DES->EncryptString(DateTimeToStr(*dt+3000));
ini->WriteString("setup","times",times_tmp);
ini->WriteString("setup","date",datetime_tmp);
putthepara(times_tmp,datetime_tmp);
this->try_time=true;
}
}

delete ini;

this->Close();

五、ini文件:

[setup]

times=inAk6xcTevw=
date=XOqvDla+r+2Xtl4ZM567cQ==
install=kAP2X5LVcWaXtl4ZM567cQ==
(数据已经加密)
.....

六、注冊表內容與INI文件內容基本相同!

上面的代码只是初步的,如果你要使用,需要经过详细的修改才对。由于是繁体版,部分注释显示为乱码,我已经将注释删除了,里面的消息框里的字符串也是乱码,可以改改就行!敬請原諒!

Published At
Categories with Web编程
comments powered by Disqus