2007. december 17., hétfő

Make a program only run once per Windows session


Problem/Question/Abstract:

How can I prevent the user from running my program twice during the same Windows session? I want to force the user to log into Windows again before my application can be started a second time.

Answer:

A way to make a program only be able to run once in every session is to create a unique global atom string on first run. Then on the following run, check if the string exists, and not run the program if the string atom is present. For example:

procedure TForm1.FormShow(Sender: TObject);
var
  atom: Integer;
begin
  if (GlobalFindAtom('This_is_some_unique_text') = 0) then
    atom := GlobalAddAtom('This_is_some_unique_text')
  else
  begin
    ShowMessage('This application can only be run once for every Windows Session.');
    Close;
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése