|
EXAMPLE 1
Items
In this example you will learn to
Protection scheme
Flowchart
Get Registration Status
Default values of properties
Control the number of concurrent instances
Brief practice with example 1a
Run the application
Start the trial period
Deleting the existing registration
Registering the application using Keys
Control the computer system date
The example 1b
In this example you will learn to:
1. Use the component in a basic protection scheme (Scheme A).
2. Control the number of concurrent instances
3. Use two different methods to start the trial period: offline and online using the basic OLM.
4. Check and automatically correct the date of your computer.
5. Remove the local registration data and from the web server (OLM).
6. Using the KeyGen utility to generate registration keys.
Protection scheme
This example uses the scheme A
Scheme A

It is implemented as shown in the following flowchart:
Flowchart

|
Main form (unit1)
procedure TForm1.FormCreate(Sender: TObject);
begin
DoRegister(False);
end;
procedure TForm1.DoRegister(force:boolean);
var F : TRegForm;
begin
F:=TRegForm.Create(nil); //Create the registration Form
try
if AVLockS41.IsLocal and (force or (keydata.DaysLeft < 15)) then F.ShowModal;
finally
FreeAndNil(F);
end;
end;
procedure TForm1.FormPaint(Sender: TObject);
begin
if (keydata.Status <> Registered) then begin
showmessage('Not Registered');
application.Terminate;
end;
if (keydata.TooManyInstances) then begin
showmessage('Too many instances');
application.Terminate;
end;
if not AVLockS41.IsLocal and (AVLockS41.activeinstances.count < 2) then begin
showmessage('This application must be run first from the server.');
application.Terminate;
end;
end;
|
The "force" parameter into the DoRegister procedure determine its behavior:
procedure TForm1.DoRegister(force:boolean);
Also, consider the following line:
if force or (F.keydata.DaysLeft < 15) then F.ShowModal;
When you run the procedure:
1) When (force = true): the registration form is always displayed.
2) When (force = false): only shows the form if the missing days to expire
are under 15.
Let's see what happens under different circumstances;
a) when not registered: the registration form shown because (daysleft = 0).
b) the registered status is trial or temporary:
- When the remaining days are greater than or equal to 15, the form
not shown (daysleft >= 15).
- When the remaining days are under 15, the form is shown (daysleft < 15).
c) the registered status is permanent: the form is not shown because daysleft is greater than 15 (daysleft = 65535).
From FormCreate DoRegister is called with (force = false), so that the form is not displayed when not needed because the application is already registered or have much time to expiry of the period allowed, but since the [Registration Form] runs with (force = true) DoRegister (True) to force the form is always displayed regardless of registration status.
Version (1a): is the development version, with utilitarian section including buttons to start the trial period and delete the registration data in order to get back the application to its original status.
Version (1b): is the final release, where the trial period starts automatically and the utilitarian section were removed.
Get the Registration Status
The first thing into this unit are defining two compiler directives in order to configure the application:
{$DEFINE SAN} //Options are NAS, SAN, REMOV
// NAS = Network Attached Storage
// SAN = Storage Area Network
// REMOV = Removable Disk
{$DEFINE BASIC_OLM} //Options are BASIC_OLM, ADVANCED_OLM, NO_OLM
The method GetRegStatus() into the Regist unit allow to get the current registration status of the component. Below you can see the source code. The first thing you do is assign the properties of the component according with the defined directives. Properties have its own default values (see it in a table below). It would only be necessary to assign values that do not match the preset, in our case, for the most complete example are assigned all properties.
There we use the method GetKeyData() to read the local registration data and assign the 'keydata' record of TKeyData type. Then assigns the 'EdIcode' edit box with the Installcode value obtained from the machine, then in a case statement is prepared the message for the registration status to be shown in the top of the registration form and assigned to the caption of the lstatus label.
This method GetRegStatus, is called into the OnCreate event and at the end of each section of code that makes changes in the registration status, such as in BtnRegClick (), BtnRemoveClick (), BtnTrialClick () and BtnTrialOlmClick ().
procedure TRegForm.GetRegStatus;
var s:string;
begin
AVLock:= Form1.AVLockS41;
with avlock do begin
{$IFDEF NAS}
RegPath := CommonDocuments;
RegFolder := 'example1';
InstancesCtrl := False;
{$ENDIF}
{$IFDEF SAN}
RegPath := ExeDir;
RegFolder := '';
InstancesCtrl := True;
{$ENDIF}
{$IFDEF REMOV}
RegPath := ExeDir;
RegFolder := '';
InstancesCtrl := False;
RemovableDisk := True;
{$ENDIF}
EncryptionKey := 'abc123';
EncryptionKey2 := 'xyz321';
AppID := 12301;
AppName := 'MyApp';
AppVersion := '1.0.0';
WebHost := 'www.av-soft.com';
TimeHost:= 'time-a.nist.gov';
OlmPath := '/olm4';
OlmBasicScript := 'basicolm.php';
OlmAdvScript := 'advancedolm.php';
V32Compat := False;
end;
AVLock.GetKeyData(0,keydata);
EdIcode.Text := AVLock.InstallCode;
s:='';
case keydata.Status of
Unregistered: s:='Not registered';
Moved : s:='Moved to another computer';
Expired : s:='Expired';
Registered : begin
s:='Registered ';
case keydata.KeyType of
Trial : s:=s+inttostr(keydata.Days)+' days trial - '+inttostr(keydata.DaysLeft)+' days left.';
Temporal : s:=s+inttostr(keydata.Days)+' days license - '+inttostr(keydata.DaysLeft)+' days left.';
Permanent : s:=s+'(Permanent no time limit)';
end;
end;
end;
lstatus.caption:=s;
end;
Default values of properties
The component initialize its properties with the following values:
Properties
|
Default Values
|
Detail
|
EncryptionKey
|
'abc123'
|
Encryption key used for keys and other registration data.
|
EncryptionKey2
|
'xyz321'
|
Encryption key used to encrypt the data sent and received on the website.
|
AppID
|
12345
|
Application ID number.
|
RemovableDisk
|
False
|
Make True for portable applications.
|
AppName
|
'MyApp'
|
Application name.
|
AppVersion
|
'1.0.0'
|
Application version.
|
WebHost
|
'www.av-soft.com'
|
website url where you installed the OLM.
|
TimeHost
|
'time-a.nist.gov'
|
url for the time server in order to get the actual time and date.
|
InstancesCtrl
|
False
|
If True it enables to control the number of instances executed simultaneously.
|
RegPath
|
CommonDocuments
|
Location within the local disk where is saved the registration data. Set to "Other" if you want to especify your own location.
|
RegFolder
|
'avlocks3'
|
Name of the folder where is saved the registration data. Set (RegPath=Other) and (RegFolder='') in order to especify the application folder to save the registration data. This is required to control instances into the network. See example 1.
|
OlmPath
|
'/olm3'
|
Location within the hosting where you installed the OLM.
|
OlmBasicScript
|
'basicolm.php'
|
Name of the main script for the basic OLM.
|
OlmAdvScript
|
'advancedolm.php'
|
Name of the main script for the advanced OLM.
|
V32Compat
|
False
|
Compatibility with previous versions 3.x. Set it to true to update an existing application without their users have to register again. Installcode will remain the same as in the previous version.
|
|
|
|
Before using the component properties should be allocated according to their own settings. You only need to assign those who are different from those that are assigned by default, should at least assign different values for the following properties: EncryptionKey, EncryptionKey2 and AppID, and if you have installed the OLM into your own server also change the WebHost and OlmPath properties.
Control the number of concurrent instances
The Instances field determines the number of simultaneous instances allowed into the computer. To allow it you must set (InstancesControl=True). Optionally this will manage the number of instances into the network using (RegFolder='') and (RegPath=Other), so the *.avr file for the registration data and the *.avc file to control instances will be saved into the
application folder. Please remember that Windows Vista do not allow to write under the "Program Files" folder, so for this configuration you probably will need to use another location to install the application.
When the number of instances allowed is surpassed the TooManyInstances field into the TKeyData record will become True.
This example has a button [Instances running] on the main form. This call the following code:
procedure TForm1.BtnUsersClick(Sender: TObject);
var s:string;
i:integer;
begin
AVLockS41.Refresh;
s:='There are active '+inttostr(AVLockS41.activeinstances.count)+' of '+
inttostr(keydata.Instances)+' allowed instances'+#13#10
+'----------------------------------------------------------'+#13#10;
for i:=0 to AVLockS41.activeinstances.count -1
do s:=s+AVLockS41.activeinstances[i]+#13#10;
s:=s+'----------------------------------------------------------';
showmessage(s);
end;
There you will see a brief information about the intances currently running. Also consider the following code from the OnPaint event:
This code terminate the application when it is not registered.
if (keydata.Status <> Registered) then begin
showmessage('Not Registered');
application.Terminate;
end;
This code terminate the application when the current number of instances surpassed the allowed number.
if (keydata.TooManyInstances) then begin
showmessage('Too many instances');
application.Terminate;
end;
This code terminates the application when trying to launch it from a terminal as the first instance. This limitation is for security reasons.
if not AVLockS41.IsLocal and (AVLockS41.activeinstances.count < 2) then begin
showmessage('This application must be run first from the server.');
application.Terminate;
end;
Brief practice with example 1a
From the Delphi IDE open the example 1a (\Examples\1\a)

Run the application
Hit the button or click F9 to start the program. In moments you will see the registration form:

Note that the current registration status is "Not registered". This is because it is first executed the program and for "a" versions, the trial period is not started automatically.
If the status is not registered then when clicking the [Continue >>] button, the application will terminate, so the main form will be unaccessible.
Start the trial period
Related items: How to start the trial period
For now we will see how to do it manually using the buttons provided for this purpose, after with the version (b) we will see how to do it automatically when you start the application.
Click on the [Start Trial] button. The code associated with this button is shown below:
procedure TRegForm.BtnTrialClick(Sender: TObject);
var res:string;
err:integer;
begin
if (keydata.Status = Unregistered) then
begin
res:='';
{$IFDEF NO_OLM}
//(index,users,inst,startdate,days,values)
err:=AVLock.MakeTrial(0,1,2,date,30,'000');
if (err=0) then res:='00';
{$ENDIF}
{$IFDEF BASIC_OLM}
//values, kind, Index, days, inst)
res := AVLock.OnlineGetKeyB('000',0,0,30,1);
{$ENDIF}
{$IFDEF ADVANCED_OLM}
//(index,users,inst,days,values)
res := AVLock.OnlineStartTrial(0,1,1,30,'000');
{$ENDIF}
if (res='00') then begin
showmessage('Trial started or synchronized successfully.');
GetRegStatus;
end else showmessage('Failed to start the trial period. Please review your internet connection and try again.');
end else if (keydata.KeyType = Trial) then showmessage('Trial already started.')
else showmessage('Application already Registered.');
end;
We have three choices to start the trial period:
a) Offline way. Using the method MakeTrial.
b) Online way. Using the basic OLM. The example is configured to access the script basicolm.php on the site www.av-soft.com which you can use to do the practices.
c) Online way. Using the advanced OLM. The example is configured to access the script advancedolm.php on the site www.av-soft.com which you can use to do the practices.
Changing the following compiler directive
{$DEFINE BASIC_OLM} //Options are BASIC_OLM, ADVANCED_OLM, NO_OLM
you could test the three options. We suggest you that before try with a new configuration, remove all registration data with the [Remove Registration] button.
After pressing the [Start Trial] button, you will see the following message box informing the operation result, "Trial started":
You can also see current registration status has changed to show the message that you see below:

Now if you click on [Continue>>], you will go to the main application form:

This is the [Registration Form] button, which allows you to access the registration form. This form is not displayed automatically on startup, so you should use this button to display it. Close the application and open it again and you'll see.
Deleting the existing registration
The registration data are always saved in the computer which has the application (local data), then if the user has internet connection may also be stored on the website (Online Data). By using the [Start Trial] button, having defined the {$DEFINE NO_OLM} directive the registration data will be stored only on the computer, otherwise if you have defined {$DEFINE BASIC_OLM} or {$DEFINE ADVANCED_OLM}, the registration data will be stored in both places, the computer and on the website. See below the code executed for each case depending of the defined directive:
procedure TRegForm.BtnRemoveLocalClick(Sender: TObject);
var ok:boolean;
s,msg:string;
begin
if AVLock.EraseReg
then msg:= 'Local registration data removed'+#13#10
else msg:= 'Could not remove Local registration data'+#13#10;
ok:=False;
{$IFDEF BASIC_OLM}
ok:= AVLock.OnlineRemoveKey(0); //remove regitration data from the Basic OLM
{$ENDIF}
{$IFDEF ADVANCED_OLM}
s:= AVLock.OnlineRemoveReg(0); //remove regitration data from the Advanced OLM
ok:= ((s='00') or (s='11'));
{$ENDIF}
if ok then msg:=msg+'Online registration data removed'
else msg:=msg+'Could not remove Online registration data';
showmessage(msg);
GetRegStatus;
end;
Go to the registration form with [Registration Form] and delete the registration with [Remove Local Registration Data]. You will see a message informing you of the result of the operation:

Now when you click on [Continue>>] the application stops the execution. The program returned to its initial status "unregistered".
If you started the trial period having defined the {$DEFINE BASIC_OLM} directive, apparently the result is the same as when it was with the {$DEFINE NO_OLM} directive defined, only apparently, since if you leave pass some days, then erase the local registration data with the EraseReg method and restarting the trial period with the [Start Trial] button, the trial period will not be restarted but will be restored to the previous situation. For example, if before to restart you have 22 days to expire, like in the below image:

When you erases the registration and restart it using the [Start Trial] the registration status will remain the same, (22 days to finish). This occurs because the registration data have not been erased on the web server, only local data were deleted and are reinstated with the same values previously obtained from the website. As you can see this method is really sure.
The only way to reset the trial period is deleting the corresponding file on the server. The methods OnlineRemoveKey and OnlineRemoveReg removes the registration data on the website, so you should not allow the user to access these utilities on the final version of your application if you are using in your application a panel of utilities like this example, remove it from the final version as we do in version 1b.
Do not worry, thinking that perhaps an user can download the package AVLock SIMPLE and taking the source code of the examples may come to use these methods to pass over your protection, this is not possible because to do so should know the AppID and encryption keys that you are using.
See below how the registration data is saved into the web site using the basic OLM.
Also in version 3.5 Added OnlineRemoveKey() method that lets you delete the corresponding file on the web server, the button [Remove Online Registration Data] uses this method. Try it and verify that this way restart the trial period to its initial values. This possibility does not imply a risk for you since to erase this data is necessary to configure the component with the AppID and EncriptionKey that only you know. Besides the utilitarian panel will be removed in the final version of the application.
Registering the application using Keys
Every time you started the trial period was generated internally a registration key. Normally the trial period is started with the two methods already seen, with MakeTrial() or with the OLM, but can also be done using a registration key.
Let's assume you do not want the trial period starts automatically and prompts users to send you InstallCode code to generate a key which would allow the trial period. This method is very safe and does not require that you use the OLM.
To generate the key we use the keygen.exe utility
Generate an 50-day trial key as shown below:

Enter the generated key into the registration form and click on [Register].

if no mistakes should receive a message like the one shown below, with the state of registration to 50 days.

You can generate different types of keys:
Permanent: Never expire.
Temporary: Same as the trial keys but used to rent your application by month, year, etc.
Trial: Used for the initial trial period.
Unregister Key: Deletes local registration data corresponding to the Index used to generate the key.
Unregister All: Deletes all local registration data.
Control the computer system date
During the trial period and periods of temporary keys there is the potential risk that the user attempts to set back the date of the computer to use the application for more time than the authorized period. To avoid this we added the following methods:
GetOnlineDate(): Gets the current date from the internet.
FixSystemDate(): Changes the computer system date using the date given as parameter.
OnlineCheckDate(): Internally uses the two above methods to obtain the correct date and assign it to the computer. Also saves the latest date reached by the computer (LastDate) along with the registration data.
Then, each time you get the registration status with GetKeyData() also reads the LastDate field and compares it with the date of the computer and if LastDate is higher than the computer date it assumes that the user turned back the computer date, then the DateBacked field within the key data is changed to True.
With the button [Check System Date] is used the GetOnlineDate() method to get the date from Internet and then compares it to the local date, if not matches then ask the user permission to change it. If he agree then uses FixSystemDate() to fix the computer date.
With the button [Check and Fix System Date] is used OnlineCheckDate() to do the same but without giving the user the ability to approve the transaction. This method takes a Boolean parameter "fix", if true then the date is fixed otherwise it is not changed. This option can only be applied if exists the local registration data from where LastDate could reads the date saved, so in the example executes only if the application is already registered.
The example 1b
Example 1b is the definitive version of the program. The utilitarian section was eliminated as these are tools that serve the programmer to see that the registration mechanism is working properly but should not be in the final version. The following procedures were added:
procedure TRegForm.StartTrial;
begin
if (keydata.Status = Unregistered) then begin
{$IFDEF NO_OLM}
//(index,users,inst,startdate,days,values)
AVLock.MakeTrial(0,1,2,date,30,'007');
{$ENDIF}
{$IFDEF BASIC_OLM}
//values, kind, Index, days, inst)
AVLock.OnlineGetKeyB('007',0,0,30,1);
{$ENDIF}
{$IFDEF ADVANCED_OLM}
//(index,users,inst,days,values)
AVLock.OnlineStartTrial(0,1,1,30,'007');
{$ENDIF}
GetRegStatus;
end;
end;
procedure TRegForm.FormCreate(Sender: TObject);
begin
GetRegStatus;
StartTrial;
//check and fix system date automatically
if (keydata.KeyType in [Temporal, Trial]) then AVLock.OnlineCheckDate(true);
GetRegStatus;
end;
In the event FormCreate were added two lines to automatically create the trial period and to check whether the computer date is correct. In this last line calls the method OnlineCheckDate(True) and only does so if the registration type is temporary or trial, otherwise this control does not make sense. The True parameter automatically adjusts the computer's date.
Another possibility for the latter line would be:
if (keydata.KeyType in [Temporal, Trial]) and not AVLockS31.OnlineCheckDate(false)
then begin
showmessage('Your system date seems to be incorrect. Please fix it and try again');
application.terminate;
end;
In this case the procedure do not changes the date but the program ends by suggesting the user to make the correction manually.
|