可單獨或組合使用。核心機制:登錄表儲存狀態 (策略 1、2) 或單純時間比對 (策略 3、4)。
TRegistry* Reg = new TRegistry;
Reg->RootKey = HKEY_USERS; // 或 HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE
Reg->OpenKey("\\.DEFAULT\\MyProgram\\Records", true); // 不存在則建立
// 寫入初始值
Reg->WriteInteger("Count", 1);
Reg->WriteInteger("Flag", 0);
Reg->WriteDate("First", Date());
Reg->WriteDate("Last", Date());
Reg->CloseKey();
delete Reg;
鍵值說明:Count 執行次數,Flag 過期標記,First 首次執行日期,Last 上次執行日期。
TDate firstDate = Reg->ReadDate("First");
TDate currentDate = Date();
if (currentDate.Val - firstDate.Val > 30) {
Application->MessageBox("超過 30 天試用期!", "結果", MB_OK);
Reg->WriteInteger("Flag", 1);
Application->Terminate();
}
if (Reg->ReadInteger("Count") > 45) {
Application->MessageBox("超過 45 次試用次數!", "結果", MB_OK);
Reg->WriteInteger("Flag", 1);
Application->Terminate();
}
DateSeparator = '-';
ShortDateFormat = "yyyy/mm/dd";
TDate endDate = StrToDate("2001-08-31");
if (Date().Val > endDate.Val) {
Application->MessageBox("已過截止日期!", "結果", MB_OK);
Application->Terminate();
}
// 全域變數
TDateTime startDateTime, currentDateTime;
// FormCreate
startDateTime = Now();
// Timer1Timer
currentDateTime = Now();
if (currentDateTime.Val - startDateTime.Val > 50.0 / (60 * 24)) { // 50 分鐘
Timer1->Enabled = false;
Application->MessageBox("本次執行超過 50 分鐘!", "結果", MB_OK);
Close();
}
HKEY_USERS\\.DEFAULT\\{GUID}\\Data)。
TDate lastDate = Reg->ReadDate("Last");
if (Date().Val < lastDate.Val) {
Application->MessageBox("偵測到系統時間被回撥!", "結果", MB_OK);
Reg->WriteInteger("Flag", 1);
Application->Terminate();
}
Flag=1 直接終止,不再執行後續邏輯。
Reg->WriteInteger("Count", Reg->ReadInteger("Count") + 1);
Reg->WriteDate("Last", Date());