📄 unit_demoform.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
//#define MAXI 50
#include "Unit_DemoForm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmDemo *frmDemo;
//---------------------------------------------------------------------------
TDateTime dtInstallDate;
const AnsiString strLicFileName = ".\\Liscens";
const AnsiString strRegKeyName = "\\Software\\MediaPlayer";
int iFileHandle;
int iFileLength;
BYTE FKey[FKEY_SIZE] = {0x01, 0x02, 0x03, 0x04};
//---------------------------------------------------------------------------
DWORD TfrmDemo::BTOD( const BYTE* pByte ) //BYTE转DWORD操作
{
DWORD dDword;
if( !pByte )
return 0;
if( !(memcpy(&dDword, pByte, sizeof(DWORD))) )
return 0;
return dDword;
}
//---------------------------------------------------------------------------
BYTE* TfrmDemo::DTOB( DWORD &dDWord ) //DWORD转BYTE操作
{
BYTE* pByte = new BYTE[sizeof(DWORD)];
if( !(memcpy(pByte, &dDWord, sizeof(DWORD))) )
return 0;
return &pByte[0];
}
//---------------------------------------------------------------------------
__fastcall TfrmDemo::TfrmDemo(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TfrmDemo::FormCreate(TObject *Sender)
{
reg = new TRegistry;
reg->RootKey = HKEY_CURRENT_USER;
try
{
if(reg->OpenKey(strRegKeyName, false) == false)
{ //没有注册
frmInput = new TfrmInput(NULL);
if(frmInput->ShowModal() == mrOk )
{
if(!FileExists(strLicFileName))
{ //是否存在LIC文件
if(dtInstallDate == Date())
{ //判断当前日期是否等于输入的日期
BYTE *Input = DTOB((int)dtInstallDate);
iFileHandle = FileCreate(strLicFileName);
GetMac1(FKey, 1, Input, sizeof(DWORD), NULL, false, FMac);
FileWrite(iFileHandle, (BYTE*)FMac, FMAC_SIZE);
FileClose(iFileHandle);
}else{
delete frmInput;
frmInput = NULL;
MessageBox(NULL, "正在使用非法软件,造成不良后果自负!","警告",MB_OK);
//Application->Terminate();
abort();
} //if(dtInstallDate == Date())
}else{
iFileHandle = FileOpen(strLicFileName, fmOpenRead);
iFileLength = FileSeek(iFileHandle,0,2);
FileSeek(iFileHandle,0,0);
BYTE *pszBuffer = new BYTE[iFileLength];
FileRead(iFileHandle, pszBuffer, iFileLength);
FileClose(iFileHandle);
DWORD dwLicDate = BTOD(pszBuffer);
DWORD dwInstallDate = (int)dtInstallDate;
BYTE* Input = DTOB(dwInstallDate);
GetMac1(FKey, 1, Input, sizeof(DWORD), NULL, false, FMac);
if(dwLicDate != BTOD(FMac))
{
delete frmInput;
delete []pszBuffer;
frmInput = NULL;
pszBuffer = NULL;
MessageBox(NULL, "正在使用非法软件,造成不良后果自负!","警告",MB_OK);
//Application->Terminate();
abort();
}
}
}else{
delete frmInput;
frmInput = NULL;
MessageBox(NULL, "正在使用非法软件,造成不良后果自负!","警告",MB_OK);
//Application->Terminate();
abort();
}
reg->CreateKey(strRegKeyName);
reg->OpenKey(strRegKeyName, true);
reg->WriteInteger("Value", 0); //使用了的天数
reg->WriteInteger("Dates", (int)dtInstallDate); //安装软件的日期
reg->WriteInteger("Today", (int)Date());
reg->CloseKey();
}else{
reg->OpenKey(strRegKeyName, true);
try
{
int value = reg->ReadInteger("Value");
int dates = reg->ReadInteger("Dates");
int today = reg->ReadInteger("Today");
if(value > USEDATES || !FileExists(strLicFileName))
{
MessageBox(NULL, "正在使用非法软件,造成不良后果自负!","警告",MB_OK);
//Application->Terminate();
abort();
}
iFileHandle = FileOpen(strLicFileName, fmOpenRead); //读入LicDates
iFileLength = FileSeek(iFileHandle,0,2);
FileSeek(iFileHandle,0,0);
char *pszBuffer = new char[iFileLength];
FileRead(iFileHandle, pszBuffer, iFileLength);
FileClose(iFileHandle);
DWORD dwLicDate = BTOD(pszBuffer);
BYTE* Input = DTOB(dates);
GetMac1(FKey, 1, Input, sizeof(DWORD), NULL, false, FMac);
DWORD dwRegDate = BTOD(FMac);
if(dwLicDate != dwRegDate) //比较LicDates 与 RegDates
{
delete []pszBuffer;
pszBuffer = NULL;
MessageBox(NULL, "正在使用非法软件,造成不良后果自负!","警告",MB_OK);
//Application->Terminate();
abort();
}else{
if(today < Date()) //判断是否是当天运行软件
{
++value;
reg->WriteInteger("Value", value);
reg->WriteInteger("Today", Date());
reg->CloseKey();
}
if((int)Date() - value != dates) //比较当天日期减去使用天数是否等于安装日期
{
delete []pszBuffer;
pszBuffer = NULL;
MessageBox(NULL, "正在使用非法软件,造成不良后果自负!","警告",MB_OK);
//Application->Terminate();
abort();
}
}
}catch(...){
MessageBox(NULL, "正在使用非法软件,造成不良后果自负!","警告",MB_OK);
//Application->Terminate();
abort();
}
}
}catch(...){
MessageBox(NULL, "正在使用非法软件,造成不良后果自负!","警告",MB_OK);
//Application->Terminate();
abort();
}
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -