Title: Trial Delphi Programs: Allow Only One Application Execution Per Windows Session
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.