二、實現方法
1.在Delphi5.0中新建一個單元文件,書寫代碼如下,保存:
unit RegObj;
interface
uses
Windows, Messages, SysUtils, Classes, Forms;
type
TRegObj = class
private
FSerial: string; //主板序列號
FKey: string; //密碼
FMaxTimes: Integer; //最大運行次數
FCompany: string; //公司名稱
FEmail: string; //聯系用的電子郵件
protected
procedure SetSerial; //取得主扳的序列號
procedure GetKey; //從用戶序列號文件中讀取序列號
function GetTimes: Integer; //從文件中讀取程序的運行次數
function CheckKey: Boolean; //檢查序列號和密碼是否匹配的函數
public
constructor Create;
function Execute: Boolean; //運行對象方法
published
property Company: string read FCompany write FCompany;
property MaxTimes: Integer read FMaxTimes write FMaxTimes;
property Email: string read FEmail write FEmail;
end;
implementation
//TRegObj.
constructor TRegObj.Create;
begin
inherited;
end;
function TRegObj.GetTimes: Integer;
Const
//用於存儲運行次數的文件,開發人員可自定義或使用注冊表存儲運行次數
//起此名字用於迷惑破解者,使用前不要和系統的動態鏈接庫同名
Tmp = 'ispnet.dll';
var
Ch: Char;
Dir: array [0..255] of Char;
Fn: string;
I : Integer;
List: Tstrings;
begin
//取得Windows系統的目錄
GetSystemDirectory(@Dir, 255);
For I := 0 to 255 do
begin
if Ord(Dir[I]) = 0 then Break;
Fn := Fn + Dir[I];
end;
Fn := Fn + '\' + Tmp;
try
List := TStringList.Create;
if Not FileExists(Fn) then
Ch := Chr(1)
else
begin
List.LoadFromFile(Fn);
Ch := List.Text[1];
Ch := Chr(Ord(Ch) + 1);
end;
List.Text := Ch;
//存儲運行次數
List.SaveToFile(Fn);
Result := Ord(Ch);
finally
List.Free;
end;
end;
procedure TRegObj.SetSerial;
begin
//取得主板的序列號
FSerial := String(Pchar(Ptr($FEC71)));
end;
//取得密碼
procedure TRegObj.GetKey;
const
Sn = 'Key.dat';
var
List: TStrings;
Fn, Path: string;
begin
Path := ExtractFilePath(Application.ExeName);
Fn := Path + Sn;
if Not FileExists(Fn) then
begin
FKey := '';
Exit;
end;
try
List := TStringList.Create;
List.LoadFromFile(Fn);
FKey := List.Values['Key'];
finally
List.Free;
end;
end;
function TRegObj.CheckKey: Boolean;
begin
//開發人員根據自己的需要進行修改,在這裡是為了簡單起見
Result := FKey = FSerial;
end;
function TRegObj.Execute: Boolean;
var
Msg: string;
T: Integer;
begin
T := GetTimes;
GetKey;
SetSerial;
if FKey <> FSerial then
begin
Msg := ' 您這是第' + IntToStr(T) + '次運行此程序(最大次數:' + IntToStr(FMaxTimes) + ')!';
Application.MessageBox(PChar(Msg), '用戶信息', Mb_Ok + Mb_IconWarning);
Msg := '歡迎使用' + Company + '的軟件,如果您覺得滿意的話,請注冊或購買正版軟件!';
Application.MessageBox(PChar(Msg), '建議', Mb_Ok + Mb_IconInformation);
if T > FMaxTimes then
begin
if Application.MessageBox(' 是否注冊?', '注冊', Mb_YesNo + Mb_IconQuestion) = Id_Yes then
begin
Msg := '您的注冊號是:“' + FSerial + '”' + Chr(13) + Chr(10) +
'請您將以上序列號通過電子郵件寄給以下信箱:' + FEmail;
Application.MessageBox(PChar(Msg), '軟件 注冊', Mb_Ok + Mb_Iconinformation);
end;
Application.Terminate;
end;
end;
end;
end.