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

Allow Only One Application Execution Per Windows Session

Creating Trial Delphi Applications

By Zarko Gajic, About.com

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}

begin
  Application.Initialize;

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

    Application.CreateForm(TFormMain, FormMain) ;
    Application.Run;
  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.

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

More from About.com

  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

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

All rights reserved.