1. Home
  2. Computing & Technology
  3. Delphi Programming

Allow Only One Application Execution Per Windows Session
Creating Trial Delphi Applications

By , About.com Guide

Windows Session Trial Delphi Application

Windows Session Trial Delphi Application

If you are developing shareware or commercial applications, you should protect your applications from unauthorized usage.

While there are ways to protect a Delphi application from misusage, there are situation when you want to have the weirdest protection available, but simple to implement.

Once Per Windows Session Run Protection

If you need to allow a user to run your application only once per Windows session, you can use the following "trick".

For the sake of simplicity I'll presume only one Form in a Delphi application, form is named "FormMain".

To create a Windows Session Trial application, you'll need to manually edit the project's source code (DPR).

Using the GlobalFindAtom and GlobalAddAtom API functions requires the "Windows" unit in the uses clause.

When a user starts the application, the code check for the existance of a "'SOME-UNIQUE-TEXT-RELATED-TO-THIS-APPLICATION'" string in the Windows global atom table. If the atom is not found, the application is started and the string is added to the atom table.

The second time a user tries to start the application, the "'Test Trial Protection'" warning is displayed, and the application is "terminated" - thus not started.

The user can start the application again, only when the Windows gets restarted (which clears the global atom table).

program OncePerSessionTrialTest;

uses
  Windows,
  Forms,
  mainUnit in 'mainUnit.pas' {FormMain};

{$R *.res}

var appAtom : THandle;
begin

  Application.Initialize;

  if 0 = GlobalFindAtom('SOME-UNIQUE-TEXT-RELATED-TO-THIS-APPLICATION') then
  begin
    appAtom := GlobalAddAtom('SOME-UNIQUE-TEXT-RELATED-TO-THIS-APPLICATION') ;

 try
     Application.CreateForm(TFormMain, FormMain) ;
     Application.Run;
 finally
   GlobalDeleteAtom(appAtom);
 end
  end
  else
  begin
    Application.MessageBox(
      'You can run the trial version '+
      'of this application '+
      'only once per Windows session!',
      'Test Trial Protection') ;
  end;
end.
Note: in Windows, an atom table is a system-defined table that stores strings and corresponding identifiers. An application places a string in an atom table and receives a 16-bit integer, called an atom, that can be used to access the string.

Note: according to the Application Singleton article by Christian Wimmer, it is better to use Mutexes.

Delphi tips navigator:
» Advanced Record Structure Type in Turbo Delphi and Delphi 2006
« How to Highlight Non-Focused DBGrid Selected Row

More Delphi Programming Quick Tips
Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. Delphi 2006 Tips
  7. Trial Delphi Programs: Allow Only One Application Execution Per Windows Session

©2009 About.com, a part of The New York Times Company.

All rights reserved.