其實網上已經有不少現成的軟件,它們可以幫你做到一些共享軟件的時間控制,比如試用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文件內容基本相同!
上面的代碼只是初步的,如果你要使用,需要經過詳細的修改纔對。由於是繁體版,部分註釋顯示爲亂碼,我已經將註釋刪除了,裏面的消息框裏的字符串也是亂碼,可以改改就行!敬請原諒!
(注,本文只是推薦一種簡單的方法,以滿足在些朋友簡單的要求,代碼剛實現功能 ,存在很多問題,我才準備改改,有好的方法可以提出來!)歡迎大家提示好的方法。