I have experienced erratic results with this component on a couple of
programs. I use the following unit modified from one I downloaded from the
DSP.
unit previnst;
//Based on PrevCode by Brendan Delumpa
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
function DoIExist(WndTitle : String) : Boolean;
implementation
{===========================================================================
========
This is a different twist on finding a previous instance of an application
in a
32-bit environment. It uses a semaphore (although you could also use a
mutex object)
instead of performing an EnumWindows to find a previous instance, like you
would
have done in a 16-bit environment. This is more in line with multi-threaded
app
design.
============================================================================
=======}
function DoIExist(WndTitle : String) : Boolean;
var
hSem : THandle;
hWndMe,
hWndPrev : HWnd;
semNm,
wTtl : Array[0..256] of Char;
begin
Result := False;
//Initialize arrays
StrPCopy(semNm, 'SemaphoreName');
StrPCopy(wTtl, WndTitle);
//Create a Semaphore in memory - If this is the first instance, then
//it should be 0.
hSem := CreateSemaphore(nil, 0, 1, semNm);
//Now, check to see if the semaphore exists
if ((hSem <> 0) AND (GetLastError() = ERROR_ALREADY_EXISTS)) then begin
CloseHandle(hSem);
//We'll first get the currently executing window's handle then change
its title
//so we can look for the other instance
hWndPrev := FindWindow(nil, wTtl);
SetWindowText(hWndPrev, 'zzzzzzz');
//What we want to do now is search for the other instance of this window
//then bring it to the top of the Z-order stack.
hWndMe := FindWindow(nil, wTtl);
SetWindowText(hWndPrev, wTtl);
if (hWndMe <> 0)
then
begin
Application.ProcessMessages;
if IsIconic(hWndMe) then
ShowWindow(hWndMe, SW_SHOWNORMAL)
else
SetForegroundWindow(hWndMe);
end;
//Begin block Patrick Dunford 8-Dec-98
if hWndMe <> 0
then Result := True //Original code line - Result:=true
else Result:=false;
//End block
//Could put the Halt here, instead of in the FormCreate method,
//unless you want to do some extra processing.
//Halt;
end;
end;
end.
USEAGE:
As the first line of FormCreate procedure in the main form, place this call:
if DoIExist(Self.Caption)
then Halt;
============================================
Views contained in this post are my personal opinions and unless otherwise
stated not those of any organisation I am associated with.
----------------------------------------------------------------------------
----------
Patrick Dunford, ChristChurch, NZ
http://patrick.dunford.com/
> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On
> Behalf Of Jeremy Coulter
> Sent: Sunday, 24 January 1999 14:44
> To: Multiple recipients of list delphi
> Subject: Re: [DUG]: Determining instance already running
>
>
> FTR (for the record) Just one also runs fine under D2 and D3...I cant say
> about D4 as I am still waiting for my saterday delivery of D4 that someone
> at Sealcorp is meant to have sent !
---------------------------------------------------------------------------
New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
Website: http://www.delphi.org.nz