fishsqzr wrote:
> 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
It's not the *ownership* of the mutex that detects a previous instance.
It's the mere *existence* that tells you whether another instance is around.
Check the return value of CreateMutex. If it's 0, then an error occured.
Check GetLastError to get the details. If CreateMutex does not return 0,
then you have a valid mutex handle, but if you weren't the one who
created it, then there was already another instance running. To detect
that, call GetLastError. If it returns Error_Already_Exists, then you
were not the first to create it; there is another instance somewhere.
Your DPR code should go like this:
FMutex := CreateMutex(nil, False, FAppTitle);
Win32Check(FMutex <> 0);
case GetLastError of
Error_Already_Exists: begin
// This is not the first instance
FindWindow, ShowWindow, BringWindowToTop, SetForegroundWindow, etc.
exit;
end;
Error_Success: ; // this is the first instance. Do nothing.
else begin
// unexpected error. panic.
exit;
end;
end;
Application.CreateForm(...);
Application.Run;
> 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
> if IsIconic(hRunningApp) then
> // First instance is minimized.. Restore it
> ShowWindow(hRunningApp,SW_RESTORE);
The title of your application window and the title of your main form are
probably the same. You need to make sure that FindWindow finds the
application window, *not* your main form. The class name of the
application window is "TApplication"; provide that as the first
parameter of FindWindow.
--
Rob
------------------------ Yahoo! Groups Sponsor --------------------~-->
<font face=arial size=-1><a
href="http://us.ard.yahoo.com/SIG=12hgnms92/M=362131.6882499.7825260.1510227/D=groups/S=1705115362:TM/Y=YAHOO/EXP=1124387651/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/