當你辛辛苦苦用DELPHI做好了一個你認為十分不錯的程序,你是否想把它發布出去成為共享軟件呢?
做為一個共享軟件,注冊碼肯定是少不了的,你可以通過判斷程序是否注冊來進行功能,時間或一些其它限制.現在就介紹一種簡單的注冊碼制造方法.思路是這樣的:程序運行時先檢測注冊表,如果找到注冊項,則表明已經注冊,如果沒有找到注冊項,則提示要求注冊.
<注冊例程>
在DELPHI下新建一工程,放置Edit1,Edit2,Label1,Label2,Button1組件.具體代碼如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,Registry;//在此加上Registry以便調用注冊表.
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
Function Check():Boolean;
Procedure CheckReg();
Procedure CreateReg();
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
PName:string; //全局變量,存放用戶名和注冊碼.
PPass:integer;
implementation
{$R *.DFM}
Procedure TForm1.CreateReg();//創建用戶信息.
var Rego:TRegistry;
begin
Rego:=TRegistry.Create;
Rego.RootKey:=HKEY_USERS;
rego.OpenKey('.DEFAULT\Software\AngelSoft\Demo',True);//鍵名為AngelSoft\Demo,可自行修改.
Rego.WriteString('Name',PName);//寫入用戶名.
Rego.WriteInteger('Pass',PPass);//寫入注冊碼.
Rego.Free;
ShowMessage('程序已經注冊,謝謝!');
CheckReg; //刷新.
end;
Procedure TForm1.CheckReg();//檢查程序是否在注冊表中注冊.
var Rego:TRegistry;
begin
Rego:=TRegistry.Create;
Rego.RootKey:=HKEY_USERS;
IF Rego.OpenKey('.DEFAULT\Software\AngelSoft\Demo',False) then
begin
Form1.Caption:='軟件已經注冊';
Button1.Enabled:=false;
Label1.Caption:=rego.ReadString('Name');//讀用戶名.
Label2.Caption:=IntToStr(Rego.ReadInteger('Pass')); //讀注冊碼.
rego.Free;
end
else Form1.Caption:='軟件未注冊,請注冊';
end;
Function TForm1.Check():Boolean;//檢查注冊碼是否正確.
var
Temp:pchar;
Name:string;
c:char;
i,Long,Pass:integer;
begin
Pass:=0;
Name:=edit1.Text;
long:=length(Name);
for i:=1 to Long do
begin
temp:=pchar(copy(Name,i,1));
c:=temp^;
Pass:=Pass+ord(c); //將用戶名每個字符轉換為ASCII碼後相加.
end;
if StrToInt(Edit2.Text)=pass then
begin
Result:=True;
PName:=Name;
PPass:=Pass;
end
else Result:=False;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if Check then CreateReg
else ShowMessage('注冊碼不正確,無法注冊');
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
CheckReg;
end;
end.
<注冊器>
在DELPHI下新建一工程,放置Edit1,Edit2,Button1組件.具體代碼如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
Temp:pchar;
Name:string;
c:char;
i,Long,Pass:integer;
begin
Pass:=0;
Name:=edit1.Text;
long:=length(Name);
for i:=1 to Long do
begin
temp:=pchar(copy(Name,i,1));
c:=temp^;
Pass:=Pass+ord(c);
end;
edit2.text:=IntToStr(pass);
end;
end.
從<注冊器>中取得注冊碼,便可在<注冊例程>中進行注冊.原理是使用ORD函數取得用戶名每單個字符的ASCII碼值,並進行相加得到注冊碼.
當然,這十分容易並破解,你可以用XOR進行異或操作,或者倒取反值...具體的要看你怎麼實現了.總之,本文章只拋磚引玉罷了.