Board index » delphi » Assuring one instance of an app

Assuring one instance of an app

I've been trying to write a code block that will check for a previous
instance of the application, and if found, bring it to the front.

I suspect the proper Win32API function to use is FindWindow(), but I
haven't been successful at implementing it.  I think the problem is that I
have no idea what the two variables Window Class (P) and Window Name (Q)
are of my application.  This is what I have so far (code compiles but does
not act as intended):

program caseman;

uses
  Forms,
  main in 'main.pas' {fmMain},
  WinProcs,

{$R *.RES}

var
  P,Q : PChar;

begin
 P := 'fmMain'; {don't know if this is the correct Window class}
 Q := 'CaseManager-DS'; {don't know if this is the correct Window Name}
 {Check whether this is the first application instance}
 if FindWindow(P,Q) <> 0 then
  begin
    {If not, bring previous instance to top}
    BringWindowToTop(FindWindow(P,Q));
    exit;  {Terminate}
  end;
  Application.Initialize;
  Application.Title := 'CaseManager-DS';
  Application.CreateForm(TfmMain, fmMain);
  Application.Run;
end.

David Sweeney
Texas A&M University
adapti...@tamu.edu

 

Re:Assuring one instance of an app


Quote
David Sweeney wrote:

> I've been trying to write a code block that will check for a previous
> instance of the application, and if found, bring it to the front.

> I suspect the proper Win32API function to use is FindWindow(), but I
> haven't been successful at implementing it.  I think the problem is that I
> have no idea what the two variables Window Class (P) and Window Name (Q)
> are of my application.  This is what I have so far (code compiles but does
> not act as intended):

> program caseman;

> uses
>   Forms,
>   main in 'main.pas' {fmMain},
>   WinProcs,

> {$R *.RES}

> var
>   P,Q : PChar;

> begin
>  P := 'fmMain'; {don't know if this is the correct Window class}
>  Q := 'CaseManager-DS'; {don't know if this is the correct Window Name}
>  {Check whether this is the first application instance}
>  if FindWindow(P,Q) <> 0 then
>   begin
>     {If not, bring previous instance to top}
>     BringWindowToTop(FindWindow(P,Q));
>     exit;  {Terminate}
>   end;
>   Application.Initialize;
>   Application.Title := 'CaseManager-DS';
>   Application.CreateForm(TfmMain, fmMain);
>   Application.Run;
> end.

You probably want to check for a class of 'TApplication' and a window
name equalling Application.Title. Then, send a WM_SYSCOMMAND with wParam
= SC_RESTORE to the window, and optionally call SetForegroundWindow to
make it active. You'll probably wan't to experiment a bit with the
messages/API calls, I can at the moment not recall how to make a delphi
app activate properly using API.

If you have a look at my web-site, I've got a unit (Win32 only) which
uses a different approach to ensuring one instance only, where you don't
have to rely on FindWindow and possibly varying Application.Title.

Regards,

Erik.

--
Independent contracting - Development of applications and drivers
for DOS, Win 3.1, Win 95, Win NT.

Visit http://www.info-pro.no/sperling for some free Delphi source.

Erik Sperling Johansen <e...@info-pro.no>

Other Threads