Example2 |
Top Previous Next |
EXAMPLE 2
Items covered in this topic
This is an example that uses only the advanced OLM. We will see how to handle user data in addition to the registration data. Below we see the forms and units that are part of this example:
The UserData form was added to handle the user data. Form1 is the main form of the application and Regist is the registration form.
The Main Form "Form1"
As we can see in the picture below, we have 12 buttons for access to 12 different application modules. This the the maximum number of modules that can handle a single registration key in AVLock Simple. Each of these modules is to access a special application functionality that can be controlled by AVLock.
In the previous version we had developed most of the functions and procedures in the main form of the application, but since you has probably a lot of specific functions of your application there, we placed in the main unit only the essential features. To do so, in this example we have added the avlockunit.pas unit where we moved most of the functions that had previously located on the main unit, so that now for Example 2 we only have the SetModules procedure, the BtnReg button ("Registration form") to access the registration form and two lines of code in the OnCreate event handler. Below is the code from the main unit:
In the SetModules procedure for each button we call the ModuleOk function using as parameter the module number. The ModuleOk function is implemented in the avlockunit.pas unit.
From the BtnReg button we call inconditionally the registration form with the parameter (mode=True). Now the DoRegister procedure is implemented in avlockunit.pas.
From the OnCreate event handler we invoke the registration form at conditional way with the line
DoRegister(False)
Then we call SetModules to enable/disable the appropriate buttons.
The avlockunit.pas unit
Most of the functions and procedures associated with AVLock are now in this unit. Below you can see the interface section of this unit:
In the uses clause we include a reference to AVLockS6 component. This is enough to have AVLock ready to work. Also have the procedures TestData, EnterUserData, GetRegData, GetRegStatus, StartTrial, DoRegister and ModuleOk.
The variables AVLock, keydata, msg1, msg2 and barpos are declared as global because we will also use them in the registration form. In these variables we hold data and messages related with the registration status to be shown in the registration form later.
We begin by explaining the statement AVLock:
The AVLock variable
AVLock : TAVlockS6;
This variable is a reference to the AVLock component that we instantiate as object with the following line:
AVLock:=TAVLockS6.Create(nil);
After instantiating the component, it is configured by assigning the properties that we consider suitable for the case. This is done in the initialization section. Below you can see the code for initialization and finalization sections:
As in this case Example2 is a different application to Ejemplo1, we use a different AppID = '12302'. For more infgormation about these properties see the Example1.
The TestData procedure
Here is the code for this procedure:
Receive the name and email parameters and returns True if both are not empty. It is used to verify that the user enter at least the essential data the name and email. You could improve this feature to suit your own needs.
The EnterUserData function
Here is the code for this function:
This function takes the "msg" parameter which is the text to be displayed at the top of the data entry form. Below we see the form displaying the message "In order to continue You should complete the following form":
Into a while loop checks the entered data until at least the username and email are entered. If the "Save Data" button is pressed without completing these data the message "You should At Least enter the User Name and Email" is displayed. If you entered the data correctly, it is saved in the OLM and locally with the OnlineSaveUserData function. If "Cancel" is pressed the loop is exited without saving data but verifying that the existing user data within the component AVLock are acceptable. This function is used to enter user data when registering for the first time and also when the user wants to modify their data, so for both situations, when leaving the form, the entered data or existing ones should meet the requirements, returning True if met and False otherwise.
The GetRegData function
This is the code of this function:
This function accesses the advanced OLM and gets the data from the proper record with OnlineSynch0(0) which returns a string with the data or an error code. If the length of string returned is greater than 2, it is the data, otherwise it is an error code with two digits, in most cases is '66' indicating that the record does not exist in the OLM. If there is data, these are extracted from the string returned by the function explod thus obtaining values for a registration key "key" user name "uname" Company "comp" the email "eml" and other "other" . Then verifies that the key has the correct length and properties of the component are assigned. The user data is locally stored with WriteAppData and the registration key with WriteKeyData. If the operation was correct the code '00' is returned.
The GetRegStatus procedure
Here is the code for this procedure:
The first line invokes the GetkeyData method which reads the registration data of the application from the *.avr file then assigns them to the fields of the KeyData variable.
AVLock.GetKeyData(0,KeyData);
GetKeyData takes two parameters (0, KeyData). The first is the index that match with the module in the registration data to be read, normally only is used (index=0) as in our case, except on rare occasions, when for example you want to control more than 12 modules in the implementation would need to record two index, 0 and 1 and also would take two registration keys. In our examples we will use only (index=0).
After KeyData fields are analyzed and prepared text messages with details on the registration status to be shown on the registration form. The msg1 and msg2 messages, barpos and KeyData.StrStatus are assigned with a description of the state of registration.
The StartTrial procedure
Here is the source code for this procedure:
In this case the trial period is started through the advanced olm with the StartTrialAdv method.
The parameters are (index=0), (users=1), (instances=1), (days=30), (values='EA3').
Here we see that the users parameter is only used with the advanced OLM and the Values parameter are managed at different way than the example1.
The users parameter. (users=1): indicates how many computers can be registered with the same license key. There is always a parent record in the OLM that corresponds to the computer that is first registered. For example if we have (users = 3) may register 3 computers of which the first will be the primary registration and the remaining 2 generate secondary records. In the example we will use only (users=1) that will generate us a single record in the OLM.
The values parameter. (values='EA3'): In the example1 we had used '101' to indicate the active modules, but this way we can only handle a maximum of 3 modules in the application as the values field has a maximum of 3 characters. In example2 we have to handle 12 modules, so we must use another method. As the values field allows to enter hexadecimal numbers, this allows us to handle up to 4 modules for each position. Each hexadecimal character is called a nibble in computer jargon, see http://es.wikipedia.org/wiki/Nibble.
To better understand this let's use the Windows calculator in programmer mode, we select Hex and write EA3 as you can see in the picture below:
Then we select Bin and see the hex number EA3 converted to binary:
the idea is to use each of the 12 bits to control the modules. If we run the example2 the trial period will start and see the main form with the active modules in accordance with (values = 'EA3').
Below we can see the correlation between 'EA3' and the active modules:
The KeyGen utility and DevPanel Allows you to set the key to be applied according to this concept.
In KeyGen:
In DevPanel
The ModuleOk function
Here is the code of this function:
This take as parameter a module number and returns a boolean value: True/False that determines whether the module should be active or not. This function is used in the main form for the SetModules procedure to set the active modules.
The DoRegister procedure
This is its source code:
This procedure allows the user access to the registration form to see the current status and proceed to the registration of the application.
The condition not AVLock.IsLocal avoid the form were accessed remotely.
In addition to the existing functionality in the example1 here we added code to handle the user data.
It may be that we want to present this form automatically when the app starts in the case the application is in a state Non-Registered or the trial period is about to expire or has expired. But we would also like to invoke unconditionally, for example from a button. So we've placed the force parameter that allows us to do. If we have (force=True) the registration form is displayed directly without seeing the state of registration, but if (force=False) the registration form will be displayed only if the condition satisfies the following line:
if (force or (keydata.DaysLeft < 15)) then
The condition (keydata.DaysLeft <15) makes the form is displayed when there are less than 15 days to expire, also if the application is not registered or expired, the remaining days will be zero and the condition will be fulfilled.
After finished the trial period
The user .
|