Run application only once per windows-session

Using this simple tip, you can block your application to be executable only once per windows sessions. To run application again, Windows must be restarted before.
We will use “atom”, which is something like global identifier. Using function GlobalFindAtom, we will try to find our application’s unique ID (we will generate a GUID using Ctrl+Shift+G shortcut). If it doesn’t exist, we will allow to run application and create “atom” using GlobalAddAtom function. And that is all.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm1 = class(TForm)
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}



procedure TForm1.FormShow(Sender: TObject);
begin
  if GlobalFindAtom('{92859177-BAA9-45B7-9A21-07F1BA3C1CA5}') = 0 then GlobalAddAtom('{92859177-BAA9-45B7-9A21-07F1BA3C1CA5}')
  else
  begin
    ShowMessage('This application can be started only once per Windows session, you need to restart system to run program again.');
    Close;
  end;
end;

end.

Leave a Reply