Board index » delphi » FindWindow API call

FindWindow API call

Hi,

I'm trying to write a launcher program which will detect if there is
already a copy of the to be launched program running and disallow a second
copy.  I think I should use the FindWindow API call to do this, but I'm not
quite sure how... Can somebody give me an example, or another means to my
end?

-Benjamin

 

Re:FindWindow API call


"Randy R. Johnson" <rrj...@earthlink.net> wrote:

Quote
>I'm trying to write a launcher program which will detect if there is
>already a copy of the to be launched program running and disallow a second
>copy.  I think I should use the FindWindow API call to do this, but I'm not
>quite sure how... Can somebody give me an example, or another means to my
>end?

FindWindow needs two parameters: a window class name and a window
caption. If you leave either parameter as nil, it means that it is a
"wildcard" ie. anything goes.

Delphi forms use the name of the form type ("TMainForm" for example)
as the class name. So you could use this information in the FW call.
Window caption is the other parameter, but the captions can change.

You could also use the TApplication class (corresponds to the
Application variable) and the title of your application - this changes
less often that the main window caption.

Example:

  if (FindWindow('TApplication','Your_Title_Here') <> 0) then
    ShowMessage('My app is already running!')
  else { do whatever } ;

HTH.

Regards,

Jani J?rvinen
a.k.a SilverStream Software
Helsinki Finland

Tools, information, tips, reviews & bug lists
for professional Delphi, Win32 and WinHelp developers.

mailto:ja...@dystopia.fi
http://www.dystopia.fi/~janij/

Re:FindWindow API call


rrj...@earthlink.net (Randy R. Johnson)  wrote

Quote
> I'm trying to write a launcher program which will detect if there is
> already a copy of the to be launched program running and disallow a second
> copy.  I think I should use the FindWindow API call to do this, but I'm not
> quite sure how... Can somebody give me an example, or another means to my
> end?

FindWindow(Classname, WindowName: PChar)

If you can uniquely identify your program by a classname or a window  
caption then FindWindow might be a way. Don't forget to set a parameter  
you don't use to Nil.

Michael
--

Michael Suedkamp                                      m...@suedkm.franken.de

Re:FindWindow API call


Quote
> rrj...@earthlink.net (Randy R. Johnson)  wrote
> > I'm trying to write a launcher program which will detect if there is
> > already a copy of the to be launched program running and disallow a second
> > copy.  I think I should use the FindWindow API call to do this, but I'm not
> > quite sure how... Can somebody give me an example, or another means to my
> > end?
> FindWindow(Classname, WindowName: PChar)

> If you can uniquely identify your program by a classname or a window
> caption then FindWindow might be a way. Don't forget to set a parameter
> you don't use to Nil.

This code might help to explain it as well. Start up Calculator and then
minimise it. Put a button on a delphi form and add the following code.
It works, I'll leave it up to others to clean up the code.

procedure TForm1.Button1Click(Sender: TObject);
var  hwnd : variant;
     text1, text2, text3 : pchar;
     rc : boolean;

begin
     text1 := 'Calculator';                                             // Window Name to Search for
     text2 := nil;                                                      // Class name to search for
     text3 := '                    ';                                   // blank buffer (for class
name)
     hwnd := findwindow(text2, text1);                                  // search for window

     if hwnd > 0 then                                                        // if found
     begin
        getClassName(hwnd, text3, 20);                                  // get class name
        rc := showwindow(hwnd, 1);                                      // show window (in normal mode)
        Messagedlg('Class is :' + text3, mtinformation, [mbok],0);      //
show class name
     end
     else                                                               // otherwise
        messagedlg('Program not found', mtinformation, [mbok],0);       //
report not found

end;

---------------------------------------
John Greening
Gloucestershire UK
---------------------------------------

Re:FindWindow API call


Quote
Randy R. Johnson wrote:

> Hi,

> I'm trying to write a launcher program which will detect if there is
> already a copy of the to be launched program running and disallow a second
> copy.  I think I should use the FindWindow API call to do this, but I'm not
> quite sure how... Can somebody give me an example, or another means to my
> end?

> -Benjamin

try this, create a blank form, add a button and test with say
"Calculator"

procedure TForm1.Button1Click(Sender: TObject);
var  hwnd : variant;
     text1, text2, text3 : pchar;
     rc : boolean;

begin
     text1 := 'Calculator';
     text2 := nil;
     text3 := '                    ';
     hwnd := findwindow(text2, text1);

     if hwnd > 0 then
     begin
        getClassName(hwnd, text3, 20);
        rc := showwindow(hwnd, 1);
        Messagedlg('Class is :' + text3, mtinformation, [mbok],0);
     end
     else
        messagedlg('Program not found', mtinformation, [mbok],0);

end;

end.
--
---------------------------------------
John Greening
Gloucestershire UK
---------------------------------------

Re:FindWindow API call


Another way to do this is to add a string to the global atom table when
the app is started.  Then when trying to run another instance the app
looks to see if the string exists in the table - if it does, disallow
its startup and place focus on the running copy.

There is an article in a recent PC Magazine by Jeff Prosise that
discusses some other methods as well.  Send me email if you want the
reference.

Mark

Quote
John wrote:

> Randy R. Johnson wrote:

> > Hi,

> > I'm trying to write a launcher program which will detect if there is
> > already a copy of the to be launched program running and disallow a second
> > copy.  I think I should use the FindWindow API call to do this, but I'm not
> > quite sure how... Can somebody give me an example, or another means to my
> > end?

> > -Benjamin

> try this, create a blank form, add a button and test with say
> "Calculator"
> ...

Other Threads