Como Impedir Um Executável Ser Instânciado Mais de Uma Vez ?

Delphi

03/09/2003

Como Impedir Um Executável Ser Instânciado Mais de Uma Vez ?

Caso alguém saiba, me ajudaria muito!

Grato.

Alex. :)


Alex_

Alex_

Curtidas 0

Respostas

Cebikyn

Cebikyn

03/09/2003

Você pode usar uma das duas opções abaixo:

Opção 1:

unit Unit1;

uses
  Windows, Dialogs, Sysutils;

{....}
implementation

{....}

var
  mHandle: THandle;

initialization
  mHandle := CreateMutex(nil, True, ´XYZ´);
  if GetLastError = ERROR_ALREADY_EXISTS then
  begin
    ShowMessage(´Já há uma instância deste programa.´);
    halt;
  end;

finalization
  if mHandle <> 0 then CloseHandle(mHandle)
end.


Opção 2:

procedure TForm1.FormCreate(Sender: TObject);
var
  Sem: THandle;
begin
  Sem := CreateSemaphore(nil, 0, 1, ´NOME_DO_PROGRAMA´);
  if ((Sem <> 0) and (GetLastError = ERROR_ALREADY_EXISTS)) then
  begin
    CloseHandle(Sem);
    ShowMessage(´Já há uma instância deste programa.´);
    Halt;
  end;
end;



GOSTEI 0
POSTAR