其实网上已经有不少现成的软件,它们可以帮你做到一些共享软件的时间控制,比如试用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文件內容基本相同!
上面的代码只是初步的,如果你要使用,需要经过详细的修改才对。由于是繁体版,部分注释显示为乱码,我已经将注释删除了,里面的消息框里的字符串也是乱码,可以改改就行!敬請原諒!
(注,本文只是推荐一种简单的方法,以满足在些朋友简单的要求,代码刚实现功能 ,存在很多问题,我才准备改改,有好的方法可以提出来!)歡迎大家提示好的方法。
(剑灵时空-www.SwordElf.com)
原作者: 不详
来自于: 不详
共有120位读者阅读过此文
内容:
软件试用次数的限制有很多种方法,其关键是把次数这个值存储在哪里。本文介绍一种方法,通过改变文件 的时间来存储试用次数,从而限制软 件的试用。
一、通过改变文件时间来限制软件的试用次数
在MS-DOS中,文件的时间是 通过一个16位的值来保存的,位 15~11用来保存小时数,位10~5用 来保存分钟数,位4~0用来保存秒钟 数。这个16位的值正好可以被我们用来保存试用次数。例如,在下面的示例 程序中,笔者就使用了保存分钟数的 位来保存试用次数。运行一下示例程序,我们可以看到,只是文件时间的分 钟数加了1,一般人很难察觉,这种方 法有比较好的隐蔽性。
二、示例程序中所涉及的中断
1.中断21H的功能3DH
用途:打开文件用于读。写或补 充。
调用寄存器:AH=3DH,AL=00H,DS:DX=文件名;
返回寄存器:CF 复位--成功,AX=文件把柄。
CF 置位--出错,AX=错误代码。
2.中断21H的功能3EH
用途:强迫清除所有缓冲区,更新目录,使文件把 柄无效。
调用寄存器:AH=3EH,BX=文件把柄;
返回寄存器:CF 复位--成功,AX被破坏,
CF 置位--出错,AX=错误代码。
3.中断21H的功能57H、子功能00H
用途:读出文件被最后修改的日期和时间。
调用寄存器:AX=5700H,BX=文件把柄;
返回寄存器:CF 复位--成功,CX=文件时间, DX=文件日期,
CF 置位--出错,AX=错误代码。
4。中断21H的功能57H、子功能01H
用途:将文件最后被修改过的日期和时间信息变 成指定值。
调用寄存器:AX=5701H,BX=文件把柄,CX= 新时间,DX=新日期;
返回寄存器:CF 复位--成功,
CF 置位--出错,AX=错误代码。
三、示例程序
#include
#include
#include
int count( )
{
union REGS inregs,outregs;
strut SREGS segs;
unsigned handle;
unsigned times;
/ * 打开文件 * /
inregs.h.ah =0x3d;
inregs.h.al =0;
scgread(& segs);
inregs.x.dx ="test.exe";
intdosx(& inregs, & outregs, & segs);
if(outregs.x.cflag) return(0);
else
{
/ * 读取文件日期时间 * /
handle = Outregs.x.ax;
inregs.x.ax = 0x5700;
inregs.x.bx = handle;
intdosx(& inregs, & outregs, &segs);
times = (outregs.x.cx &0x7e0)> > 5;
if(times <40)/ * 试用次数可以通过改变此处的'40'来实现 * /
{
inregs.x.ax = 0x5701;
inregs.x.bx = handle;
inregs.x.cx = outregs.x.cx + 0x20; / * 试用次数计数 * /
inregs.x.dx = outregs.x.dx;
intdosx(& inregs, & outregs, & segs); / * 重置日期时间 * /
return(2);
}
else return(1);
/ * 关闭文件 * /
inregs.h.ah = 0x3e;
inregs.x.bx = handle;
intdosx(& inregs, & outregs, & segs);
}
}
main()
{
int flag;
flag = count();
if(flag==0) printf("此软件为试用版,请勿任意修改!/ n");
if(flag==1) printf("此软件为试用版,您的试用次数已满!/n");
if(flag==2) printf("此软件为试用版,欢迎试用!/n");
}