Board index » delphi » Getting the handle of another application

Getting the handle of another application

I'm trying to find a way to detect whether another application is
running and have started with code from the WinAPI FAQs onthe Inprise
website (under the same heading as my subject).

I've copied the code almost verbatim (see below) , but cannot get it
to work. A few tests show that its enumerating the windows OK and
finding the one I'm looking for, but the Button1Click procedure always
gives"Window not found".

I have no experience of pointers and very little of the Win API so am
not entirely clear as to what is going on in the code. I would realy
appreciate some help.

Many thanks.

Alan Hale
(Code begins here)

unit Enumf11;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }

  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

 type
              PFindWindowStruct = ^TFindWindowStruct;
              TFindWindowStruct = record
                Caption : string;
                ClassName : string;
                WindowHandle : THandle;
              end;

            function EnumWindowsProc(hWindow : hWnd;
                                     lParam  : LongInt) : Bool
            {$IFDEF Win32} stdcall; {$ELSE} ; export; {$ENDIF}
            var
              lpBuffer : PChar;
              WindowCaptionFound : bool;
              ClassNameFound : bool;

            begin
              GetMem(lpBuffer, 255);
              Result := True;
              WindowCaptionFound := False;
              ClassNameFound := False;

              try
                if GetWindowText(hWindow, lpBuffer, 255) > 0 then
                  if Pos(PFindWindowStruct(lParam).Caption,
StrPas(lpBuffer)) > 0
                   then   WindowCaptionFound := true;

                if PFindWindowStruct(lParam).ClassName = '' then
                  ClassNameFound := True else
                    if GetClassName(hWindow, lpBuffer, 255) > 0 then
                      if Pos(PFindWindowStruct(lParam).ClassName,
StrPas(lpBuffer))
                       > 0 then ClassNameFound := True;

                if (WindowCaptionFound and ClassNameFound) then begin
                  PFindWindowStruct(lParam).WindowHandle := hWindow;
                  Result := False;
                end;

              finally
                FreeMem(lpBuffer, sizeof(lpBuffer^));
              end;
            end;

            function FindAWindow(Caption : string;
                                 ClassName : string) : THandle;
            var
              WindowInfo : TFindWindowStruct;

            begin
              with WindowInfo do begin
                Caption := Caption;
                ClassName := ClassName;
                WindowHandle := 0;
                EnumWindows(@EnumWindowsProc, LongInt(@WindowInfo));
                FindAWindow := WindowHandle;
              end;
            end;

            procedure TForm1.Button1Click(Sender: TObject);
            var
              TheWindowHandle : THandle;
            begin
              TheWindowHandle := FindAWindow('Sidekick', '');
              if TheWindowHandle = 0 then
                ShowMessage('Window Not Found!') else
                BringWindowToTop(TheWindowHandle);
            end;

end.

 

Re:Getting the handle of another application


if you know the title of the main window of the program looked for, use the
FindWindow function.

Denis

Re:Getting the handle of another application


Yes, but I only know *part* of the title, which is why I have to use
EnumWindows.

On Mon, 14 Dec 1998 18:59:01 +0100, "Denis Jeanroy" <denj...@hrnet.fr>
wrote:

Quote
>if you know the title of the main window of the program looked for, use the
>FindWindow function.

>Denis

Re:Getting the handle of another application


Alan Hale schrieb in Nachricht <367438b9.18292...@news.clara.net>...

Quote
>I'm trying to find a way to detect whether another application is
>running and have started with code from the WinAPI FAQs onthe Inprise
>website (under the same heading as my subject).

>I've copied the code almost verbatim (see below) , but cannot get it
>to work. A few tests show that its enumerating the windows OK and
>finding the one I'm looking for, but the Button1Click procedure always
>gives"Window not found".

>I have no experience of pointers and very little of the Win API so am
>not entirely clear as to what is going on in the code. I would realy
>appreciate some help.

>Many thanks.

>Alan Hale
>(Code begins here)
> ...

It's just an idea, but what about using the GetNextWindow function?

For example:

procedure TForm1.Button1Click(Sender: TObject);
var cName:array[0..63] of char;
    cClass:array[0..63] of char;
    AHandle:THandle;
begin
     AHandle:=GetNextWindow(Self.Handle,gw_HWndNext);
     while AHandle>0 do begin
           GetWindowText(AHandle, cName,64);
           GetClassName(AHandle, cClass,64);
    if (StrComp('Sidekick',cName)=0) then begin
               BringWindowToTop(AHandle);
    end;
           AHandle:=GetNextWindow(AHandle,gw_HWndNext);
     end;
end;

Hth,
Matthias.

Re:Getting the handle of another application


On Tue, 15 Dec 1998 09:31:49 +0100, "Matthias Thiel" <ma...@sj.com>
wrote:

Quote

>It's just an idea, but what about using the GetNextWindow function?

Yes, this works (with the small change that I substitute the Pos
function for StrComp since I will only know part of the title string.)
Thank you very much!  I wonder why the solution on the Inprise WinAPI
FAQs is so relatively complicated.

I still have one problem though - I wish to use the code as part of a
function in a DLL which will return true if a matching window is
found. There will be no form in the DLL, so
AHandle:=GetNextWindow(Self.handle,gw_HWndNext);
will not compile - the Self.Handle is not recognised.

Any suggestions please?

Many thanks

Alan Hale

Re:Getting the handle of another application


Alan Hale schrieb in Nachricht <3676c86c.5611...@news.clara.net>...

Quote
>On Tue, 15 Dec 1998 09:31:49 +0100, "Matthias Thiel" <ma...@sj.com>
>wrote:
>I still have one problem though - I wish to use the code as part of a
>function in a DLL which will return true if a matching window is
>found. There will be no form in the DLL, so
>AHandle:=GetNextWindow(Self.handle,gw_HWndNext);
>will not compile - the Self.Handle is not recognised.

Maybe you can add the handle of the calling application as a parameter to
your function.
Such as
function FindAWindow(Caption : string; ClassName : string;
StartHandle:THandle) : THandle;
and use the StartHandle.

Or you try to get the handle of any window,
e.g. "GetDesktopWindow" which returns the handle of the desktop
or "GetActiveWindow" which returns the handle of the active window,
because all you need is just any handle of a existing window.

Hth,
Matthias.

Other Threads