Board index » delphi » Preventing multiple instances of an application - D3

Preventing multiple instances of an application - D3

Delphi 3
How can I prevent multiple instances of an application?

The application  in question is a screensaver  which works well apart from
the fact that after it starts and the preset screensaver time elapses again
another instance of the program is started. I need to either inform the
system that a screensaver is already running or merely ensure that only
one instance of this program can be run.

Can anyone offer any advice on this?

Thanks
--
Andi Carey

 

Re:Preventing multiple instances of an application - D3


Hello,

put this in your prog. :

function FirstInstance(MainForm : TForm) : boolean;
var
  hWin : THandle;
  hMutex : THandle;
  WinTitle : string;
begin
  hMutex:=CreateMutex(nil, false, 'DDLFirstIntance');

  result:=(hMutex <> 0) AND (GetLastError() <> ERROR_ALREADY_EXISTS);

  if not result then begin
    WinTitle:=MainForm.Caption;
    MainForm.Caption:='';
    hWin:=FindWindow(nil, PChar(WinTitle));
    if hWin<>0 then
      if IsIconic(hWin) then
        ShowWindow(hWin, SW_SHOWNORMAL)
      else
        SetForegroundWindow(hWin);

    CloseHandle(hMutex);
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  if not FirstInstance(self) then
    halt;
end;

Bye
gscare...@wanadoo.fr

Re:Preventing multiple instances of an application - D3


In article <6pnhbe$q4...@platane.wanadoo.fr>, Georges Scareder
<gscare...@wanadoo.fr> writes
Quote
>Hello,

>put this in your prog. :

(snip)
>Bye
>gscare...@wanadoo.fr

Thank you very much.
--
Andi Carey

Other Threads