Board index » delphi » Checking for Previous Instance of object

Checking for Previous Instance of object

In Visual Basic you have a property .PreviousInstance which you use to see
if the App or the Form or whatever is already running somewhere else.
Useful when writing screen savers or using createform with multiple
instances.

Anyone know how to check for this in Delphi 2 under Win95?
--
Best regards
Neale Gray
gray...@patrol.i-way.co.uk

 

Re:Checking for Previous Instance of object


Checking for a previous instance, I use a Win32 API Mutex in the
following procedures:

var
  HMutex: THandle;
...
...
...
procedure CheckForPrevious;     {Call on application initialization}
begin
  HMutex := CreateMutex(nil, false,'MY APP'S NAME GOES HERE');
  if HMutex = 0 then begin
     ShowMessage('Can''t create mutex');
     Application.Terminate;
  end else if GetLastError = ERROR_ALREADY_EXISTS then begin
     ShowMessage('Application already running');
     CloseHandle(HMutex);
     Application.Terminate;
  end;
end;

procedure CheckOut;   {Call on application termination}
begin
  CloseHandle(HMutex);
end;

It's clean to destroy the Mutex upon termination (procedure CheckOut),
but Win32 will automatically take care of this for you when you don't.
If you can, issue CloseHandle(HMutex); when terminating the
application.  I don't use ParamStr(0)  (function to get app's path and
filename) for the program's name because Mutexes names don't allow
back slashes.  This all works.  Just drop into your D2 app and go...

John

Quote
"Neale Gray" <gray...@patrol.i-way.co.uk> wrote:
>In Visual Basic you have a property .PreviousInstance which you use to see
>if the App or the Form or whatever is already running somewhere else.
>Useful when writing screen savers or using createform with multiple
>instances.
>Anyone know how to check for this in Delphi 2 under Win95?
>--
>Best regards
>Neale Gray
>gray...@patrol.i-way.co.uk

---------------------------------------------------------
John Crane
Crane Consulting
 - Specializing in Delphi development -
Phone   310-433-8774
FAX     310-433-3520
E-mail  johnn...@ix.netcom.com
---------------------------------------------------------

Re:Checking for Previous Instance of object


Here is the code we use (edited down somewhat)...

//Globals used by CheckPrevInst processing

type TAppHook = class //we need this for an object binding for D2's HookMainWindow
  function WndProc(var Message: TMessage): boolean;
end;

var AppHMutex: THandle;
var AppMsgIDSetFocus: integer;
var AppHookObj: TAppHook;

//...//

{________________
 HookObj.WndProc: Application main window hook procedure to intercept our
                  Set-Focus-request message.}

function TAppHook.WndProc(var Message: TMessage): Boolean;
begin
  result := false;
  with Message do begin
    if msg = AppMsgIDSetFocus then with Application do begin
      if IsIconic(Application.Handle) then ShowWindow(Application.Handle, SW_RESTORE)
      else SetForegroundWindow(MainForm.Handle);
      WndProc := true;
    end;
  end;
end;

{______________
 CheckPrevInst: Called at startup to make sure we're the only app running.}

function CheckPrevInst: boolean;
  var Recips: DWORD;
  var Appstr: pchar;
begin

  {Get our unique Windows message ID for set-focus}

  result := false;
  Appstr := 'ESI.APP';
  AppMsgIDSetFocus := RegisterWindowMessage(pchar(Appstr + '.SETFOCUS'));

  {Try to open the mutex - if this doesn't return a mutex, no other
   inst is running, and we hook D2's app window proc to intercept our
   private Set-Focus-Request message if another one tries to start}

  AppHMutex := OpenMutex(MUTEX_ALL_ACCESS, false, Appstr);
  if AppHMutex = 0 then begin
    AppHookObj := TAppHook.Create;
    Application.HookMainWindow(AppHookObj.WndProc);
    AppHMutex := CreateMutex(nil, false, Appstr);
    result := true;
  end

  {Otherwise, another one is running. Tell it to focus up before we Kervork}

  else begin
    Recips := BSM_APPLICATIONS;
    BroadcastSystemMessage(BSF_IGNORECURRENTTASK or BSF_POSTMESSAGE,
            @Recips, AppMsgIDSetFocus, 0, 0);
    Application.ShowMainForm := false;
    Application.Terminate;
  end;
end;

=steve

In article <01bbb5c4.8d99b960$98c081c2@graynet>, gray...@patrol.i-way.co.uk says...

Quote

>In Visual Basic you have a property .PreviousInstance which you use to see
>if the App or the Form or whatever is already running somewhere else.
>Useful when writing screen savers or using createform with multiple
>instances.

>Anyone know how to check for this in Delphi 2 under Win95?
>--
>Best regards
>Neale Gray
>gray...@patrol.i-way.co.uk

Other Threads