Hi there... I was working on a similar unit lately, so maybe I can help
you

    See comments embedded

-----Original Message-----
From: [email protected] [mailto:[EMAIL PROTECTED] Behalf
Of fishsqzr
Sent: Thursday, August 18, 2005 3:32 PM
To: [email protected]
Subject: [delphi-en] Single instance of an application

    My case was a bit different in the way that I allow running two
different instances of the same application if each one is connected to a
different data, hence the parameter aData, you might want to ignore that

procedure BroadCastFocusMessage(const aData: String);
var
  BSMRecipients: DWORD;
begin
     raise ExceptionRuntimeFmt(Application,'An Instance is already running
on "%s"',[aDataPath]);
    Application.ShowMainForm := false;
     BSMRecipients                 := BSM_APPLICATIONS;
     BroadCastSystemMessage(BSF_IGNORECURRENTTASK or
BSF_POSTMESSAGE,@BSMRecipients,MessageID,0,0);
end;

    What I do is the following: When first the instance is running try to
open a mutex with a constant key value, if you could find this mutex, means
no instance is running then create a mutex with the same value. If value was
found, means already an instance is running, then you will call the
procedure written above.


I am trying to ensure that only one instance of an application will
run.  I have using the uDHSOneInstanceApp utility by Clément Dos.
While the utility indeed prevents multiple instances of the app, it
does not restore the current instance or make it the active app.  A
code snippet follows.  Any idea why the calls to API don't work?

  FMutex := CreateMutex (nil, FALSE, FAppTitle );

  if WaitForSingleObject (FMutex, 0) = wait_TimeOut then begin
     // Found another instance
     SetWindowText(Application.Handle,''); // We must not found this
one again ;)

     // Retrieving handle of the first instance;
     hRunningApp := FindWindow(nil,FAppTitle);
     if hRunningApp<>0 then begin
        //***FOLLOWING CODE DOES NOT SEEM TO WORK - Window not
restored or set to focus
             BroadCastFocusMessage;
         if IsIconic(hRunningApp) then
          // First instance is minimized.. Restore it
          ShowWindow(hRunningApp,SW_RESTORE);

        BringWindowToTop(hRunningApp);
        SetForegroundWindow(hRunningApp);
     end;

    In case it does not work, contact me again, I'll provide more details

Good luck
Vahan




-----------------------------------------------------
Home page: http://groups.yahoo.com/group/delphi-en/
To unsubscribe: [EMAIL PROTECTED]




----------------------------------------------------------------------------
----
YAHOO! GROUPS LINKS

  a..  Visit your group "delphi-en" on the web.

  b..  To unsubscribe from this group, send an email to:
   [EMAIL PROTECTED]

  c..  Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.


----------------------------------------------------------------------------
----





[Non-text portions of this message have been removed]



------------------------ Yahoo! Groups Sponsor --------------------~--> 
<font face=arial size=-1><a 
href="http://us.ard.yahoo.com/SIG=12hnkiarv/M=362131.6882499.7825260.1510227/D=groups/S=1705115362:TM/Y=YAHOO/EXP=1124381318/A=2889191/R=0/SIG=10r90krvo/*http://www.thebeehive.org
">Get Bzzzy! (real tools to help you find a job) Welcome to the Sweet Life 
- brought to you by One Economy</a>.</font>
--------------------------------------------------------------------~-> 

-----------------------------------------------------
Home page: http://groups.yahoo.com/group/delphi-en/
To unsubscribe: [EMAIL PROTECTED] 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/delphi-en/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


Reply via email to