Only one instance of running application

function Tform1.Am_I_loaded_Already:boolean;
var H:Thandle;
    p:Pchar;
begin
  H:=GetWindow(handle,GW_HWNDFIRST);
  getmem(P,255);
  repeat
    GetWindowText(H,p,255);
    if ((H<>application.Handle) and (string(p)=application.title)) then
    begin
      result:=true;
      exit;
    end;
    H:=GetNextWindow(H,GW_HWNDNEXT);
  until H=0;
  result:=false;
  freemem(P);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  if Am_I_Loaded_Already then
  begin
    messagedlg('Only one instance of this application is allowed. Godbye',
               mtError,[mbOk],0);
    application.terminate;
  end;
end;

this works for me under delphi3 + winnt4
REMEMBER : it will not work properly in delphi-ide.
you must start the app from explorer (or whatever)
before you can see it works.
 

OR

  CreateMutex(nil, false, 'Your Apps NAME without .exe');
  if GetLastError = ERROR_ALREADY_EXISTS then begin
      SendMessage(HWND_BROADCAST,RegisterWindowMessage('Your Apps NAME without
.exe'),0,0);
      Halt(0);
    end;

----------

For delphi 1, Open your Project Source file and enter the following:

if HPrevInst <> 0 then begin
    MessageDlg('This application is already running', mtError, [mbOK],
0);
    EXIT;
end;

You may need to add Dialogs to your Uses list.
 

--------
Subject:  Re: "Only one" Comments: I mean shutdown the prior one but remainthe posterior one alive!

If you have the handle to the prior app all you need to do is :
SendMessage(PriorAppHandle,WM_QUIT,0,0);

This will shut down the app in question.