LimeLM
wyBuild
Support forum
wyDay blog
wyDay Home

Using TurboFloat with Delphi

DelphiThis article will give step-by-step instructions on how to add floating-licensing to your Delphi app. There's also a full example app that you can download and play with without needing to add licensing to your app.

By the end of this article you'll have working floating-licensing integrated with your application, and thus the ability to sell individual copies of your software.

This article shows you how to add floating-licensing to your Delphi app using TurboFloat. To add hardware-locked licensing to your app see the "Using TurboActivate with Delphi" article.

Sign up or login and download the native TurboFloat library

Before you can do anything, you need to login to your LimeLM account (or sign up). Then download TurboFloat for Windows or macOS. It contains the native library and source code examples needed to integrate floating-licensing in your app:

Adding floating licensing to your appTurboActivate.dat and Version GUID

After you've created a new product, go to the version page of the product you will be adding licensing to. You will need to do 2 things:

  1. Download the TurboActivate.dat file for the product version.
  2. Make a note of the Version GUID.

You'll be including the TurboActivate.dat file in the same folder as the TurboFloat.dll file and you'll use the Version GUID in your code as part of integrating TurboFloat within your app.

Example project

Included in TurboFloat Library package is a simple example project. It works with Delphi 7 and newer. The TurboFloatUnit.pas and TurboFloatTypesUnit.pas files contain all the functions you'll be using to add the floating licenses functionality to your app.

Fix Delphi's exception settings

Delphi XE (released 2011) and all newer versions are misconfigured. In Delphi, you need to go to "Tools" menu, click "Options", and scroll down to the "Debugger Options -> Embarcadero Debuggers -> Native OS Exceptions" and change 2 settings. First, under the "32-bit Windows OS Exceptions", change the "Privileged Instruction" to be handled by "User program". Next, under the "64-bit Windows OS Exceptions", again, change the "Privileged Instruction" to be handled by "User program":

Fixing Delphi's misconfiguration of Privileged Instructions

If you don't do this, then when you run your app under the debugger you'll get exceptions that will crash your app. Why? Because the way TurboFloat works is that it throws exceptions and then catches them internally. However, with Delphi's default value of the "Debugger" handling the "Privileged Instruction", the Delphi debugger interrupts TurboFloat's default behavior with no real benefit other than to annoy and confuse you. This is a poor design choice on Embarcadero's part.

Step-by-step walkthrough

We're going to walk you through adding floating licensing to your app by using our Delphi example application. If you haven't downloaded it already you can get the example app inside the TurboFloat Library package.

Step 1. Start the TurboFloat Server

Before you can continue, you need to start a TurboFloat Server instance. We recommend spinning-up a TurboFloat Server instance on our infrastructure using LicenseChest. Alternatively, your customers can host the TurboFloat Server on their own infrastructure by activating and installing the TurboFloat Server locally.

We recommend using our hosted TurboFloat Server option because it's fast, stable, up-to-date, and incredibly easy to deploy.

Step 2. Request "license lease"

In the example "Text Editor" Delphi application the "frmMain.pas" file is the main UI for the app. That is, it's the entry point for the application. And because it's the entry point of the application it's where we'll be requesting the "license lease" from the TurboFloat Server.

First, create the TurboFloat instance as a private member in the class and add the close event handler for the "Lease Expired" form:

type
  TForm1 = class(TForm)
    // ...
    procedure FrmLeaseExp_FormClose(Sender: TObject; var Action: TCloseAction);
	// ...
var
  Form1: TForm1;
  tf: TurboFloat;
  frmLeaseExp : TLeaseExpiredFrm;

Then, in the constructor for the form, you'll actually create the new instance of the TurboFloat class. Paste the Version GUID you copied from earlier:

procedure TForm1.FormCreate(Sender: TObject);
begin
   Try
       //TODO: goto the version page at LimeLM and paste this GUID here
       tf := TurboFloat.Create('PASTE-VERSION-GUID-HERE');
   except

     on Ex : ETurboFloatException do
     begin
         ShowMessage('Failed to get a lease from the floating license server: ' + Ex.Message);

         // Exit the app, and exit the function immediately
         Application.Terminate;
         exit;
     end;
   end;

// ...

Next, we'll actually request the lease from the TurboFloat Server:

Try
    //TODO: goto the version page at LimeLM and paste this GUID here
    tf := TurboFloat.Create('PASTE-VERSION-GUID-HERE');

    // actually try to request the lease
    tf.RequestLease();
except

Step 3. Handle the "LeaseChange" event

The native TurboFloat library handles all the details about renewing leases, retrying, etc. All you have to do is handle the cases where TurboFloat talks to your app and tells it something has changed (license lease failing to be renewed or new license field data). To do this you need to handle the LeaseChange event. Here we add a new event handler before we request a lease:

Try
    //TODO: goto the version page at LimeLM and paste this GUID here
    tf := TurboFloat.Create('PASTE-VERSION-GUID-HERE');

    // set the lease changed handler
    tf.LeaseChange := TurboFloat_LeaseChange;

    // actually try to request the lease
    tf.RequestLease();
except

Then, add the "TurboFloat_LeaseChange()" function to handle notifications from the TurboFloat library:

procedure TForm1.TurboFloat_LeaseChange(Sender: TObject; status: LongWord);
begin

    if status = TF_CB_FEATURES_CHANGED then
    begin
        //TODO: if you're using feature values in your app you might want
        //      to reload them now.
        ShowMessage('TODO: Reload custom license fields in your app.');
    end
    else if status = TF_CB_LEASE_REGAINED then
    begin

        // we got the lease back, close the lease expired dialog
        // and resume the application
        if frmLeaseExp <> nil then
        begin
            frmLeaseExp.ModalResult := mrOK;
            frmLeaseExp.Close();
        end;
    end
    else begin // TF_CB_EXPIRED, TF_CB_EXPIRED_INET, and everything else

        // Immediately disable the app and/or show a modal form that
        // gives the user the option to Save / Save As the data (if
        // applicable) and let the user re-try connecting to the server.

        self.Enabled := False;

        //Note: Don't just close your app. Your users will be rightfully
        //      upset if you do something like that.

        // Instead, we're showing a modal dialog that lets the user try
        // to get a new lease from the server. Or, if they can't, then
        // save their data.


        frmLeaseExp := TLeaseExpiredFrm.Create(self, tf);
        frmLeaseExp.OnClose := FrmLeaseExp_FormClose;
        frmLeaseExp.Show();
    end;
end;

procedure TForm1.FrmLeaseExp_FormClose(Sender: TObject; var Action: TCloseAction);
begin
    if frmLeaseExp.ModalResult = mrCancel then
    begin
        frmLeaseExp := nil;

        // Exit the app, and exit the function immediately
        Application.Terminate;
        exit;
    end;

    frmLeaseExp := nil;

    // We've gotten a new lease, so re-enable the app.
    self.Enabled := True;
end;

Step 4. Finishing touches

Included in the example Delphi app are 3 forms that you might want to copy to your own application:

  1. LeaseExpired.pas: a form to show if the license lease has expired.

    Lease expired

  2. InternetErr.pas: a form to show if there's an internet error (that is, a server is specified, but your app couldn't connect to the server).

    Internet error

  3. ServerConfig.pas: a form to allow the end-user to enter details about where their TurboFloat Server is located.

    Config TurboFloat Server location

    This simple dialog gives your customers easy entry of the details needed to connect to their TurboFloat Server instance, whether they host it on our infrastructure using LicenseChest or they host it locally.

You've already seen example usage of the LeaseExpired form in the TurboFloat_LeaseChange() function. Here's an example usage of the ServerConfig and InternetErr forms. Its logic is fairly simple. First it tries to get a lease, and if that fails then it prompts the user for action:

procedure TForm1.FormCreate(Sender: TObject);
var
   srvConf : TServerConfigFrm;
   inetErr : TInternetErrFrm;
begin
   Try
       //TODO: goto the version page at LimeLM and paste this GUID here
       tf := TurboFloat.Create('PASTE-VERSION-GUID-HERE');

       // set the lease changed handler
       tf.LeaseChange := TurboFloat_LeaseChange;

       // actually try to request the lease
       tf.RequestLease();
   except

     on ServEx : EServerException do
     begin
         // no server configuration found -- prompt for it
         srvConf := TServerConfigFrm.Create(self, tf);

         if not (srvConf.ShowModal = mrOk) then
         begin
             ShowMessage('You must specify the location of the floating license server to run YourApp.');

             // Exit the app, and exit the function immediately
             Application.Terminate;
             exit;
         end;
     end;

     on Ex : ETurboFloatException do
     begin

       // Give the user an option to try another server if they
       // couldn't connect to the first one, or if the first one
       // is for a different product, of if the username is not
       // allowed to request leases from that particular server.
       if (Ex is EInternetException)
           Or (Ex is EWrongServerProductException)
           Or (Ex is EServerUUIDMismatchException)
           Or (Ex is EUsernameNotAllowedException) Then
       begin
           inetErr := TInternetErrFrm.Create(self, tf);

           if not (inetErr.ShowModal = mrOk) then
           begin
               // exit the app on failure
               Application.Terminate;
               exit;
           end;
       end else
       begin
           ShowMessage('Failed to get a lease from the floating license server: ' + Ex.Message);

           // Exit the app, and exit the function immediately
           Application.Terminate;
           exit;
       end;
     end;
   end;
end;

Step 5. Dropping the lease when your app closes

After you've successfully requested a lease from the TurboFloat Server, the TurboFloat library integrated in your app takes care of renewing the leases automatically and silently. You'll only ever get a notification of something going wrong in the handler for the LeaseChange event that we covered in Step 3.

When your app is closing you should "drop" the lease using the DropLease() method, and cleanup the memory using the Cleanup() method:

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
    Try
        // Drop the lease
        if tf.HasLease() Then
        begin
            tf.DropLease();
        end;

        // Cleanup the memory used by the TurboFloat library
        TurboFloat.Cleanup();
    except

        on Ex : ETurboFloatException do
        begin
          // we're exiting anyway, just let the lease be a zombie.
        end;
    end;
end;

What this does is tell the TurboFloat Server that you're through using the lease in this instance of your app, and another instance of your app on another computer or another session can now use that "free slot".

If you can't drop the lease (because your app can't connect to the internet, or for any other reason), and you choose to exit your app anyway, then the "lease" on the TurboFloat Server will be a "zombie". The lease will expire eventually on the TurboFloat Server, and thus the free slot will open back up.

Step 6: Testing the lease change event

Testing requesting leases, dropping them, and everything else in the TurboFloat Library is intuitive: just call the function and it does the thing you want it to do. Testing the lease change event is, however, slightly less intuitive. Here's how you can test the lease change event:

  1. If your app is open and has a lease, close your app and make sure it drops the license lease from the TurboFloat Server.

  2. Stop the TurboFloat Server instance.

  3. Open the TurboFloat Server config file, and edit <lease .../> element and set it to "30". This will set the lease length to 30 seconds.

  4. Save the changes you made to the configuration file.

  5. Start your TurboFloat Server instance again.

  6. Start your app again, and make sure it successfully gets a license lease from the TurboFloat Server.

  7. Now, stop the TurboFloat Server instance, but leave your app running.

  8. Within the next 30 seconds the lease change event will be called because the TurboFloat Library was not able to renew the license lease automatically.

You should also test the "TF_CB_LEASE_DROPPED_SLEEP" and "TF_CB_LEASE_REGAINED" callback types. These callbacks are raised when your app has a lease and the device your app is running on goes to sleep and then eventually wakes up. To test these scenarios:

  1. Run a TurboFloat Server on a separate computer than the one you're running the test from (or spin up a TurboFloat Server instance on our infrastructure).

  2. Start your app and acquire a lease from that TFS instance.

  3. Put the computer which your app is running on to sleep.

  4. After the computer has gone to sleep, wake it up.

  5. Your app should handle both of those events seamlessly. Namely, prompting the user to reconnect when the computer goes to sleep, and if the lease is regained automatically, hiding that prompt from the user.