Link to home
Start Free TrialLog in
Avatar of wackyteseo
wackyteseo

asked on

Detect the handle of child window of another application

Hi have one solution in this discussion :
https://www.experts-exchange.com/questions/26910900/Delphi-Trace-all-Windows-create-and-destroyed.html?cid=239&anchorAnswerId=35226401#a35226401
it's ok , now , i have one apllication .exe that when start create 5 box , i have only one .exe but five box and each box have one handle , i need of number of handle because i have to write in to this windows .
Anf after i have one window pop up , i have try with winspector spy to trace and in the Window Log i have : "Window 00100B6E, class #32770, created" , how can i intercept like the method of the other post ? but this time by type of class #32770 ? and detect child of .exe ?
Avatar of systan
systan
Flag of Philippines image

SendMessage(FindWindowEx(FindWindow('TForm1', nil), 0, 'TListBox', nil), LB_INSERTSTRING, 0, Integer(PChar(getcommandline + ' CREATED with HANDLE ' + IntToStr(wp) )));
>>but this time by type of class #32770 ? and detect child of .exe ?
Oh, we'll try, later.
Avatar of jimyX
jimyX

There are many methods to enumerate processes and get their handles, check method V at the link below.
function EnumProc(wnd: HWND; Lines: TStrings): BOOL; stdcall;
var
  buf, Caption: array[0..255] of char;
begin
  Result := True;
  GetClassName(wnd, buf, SizeOf(buf) - 1);
  SendMessage(wnd, WM_GETTEXT, 256, Integer(@Caption));
  Lines.Add(Format('ID: %d, ClassName: %s, Caption: %s',
           [GetDlgCtrlID(wnd), buf, Caption]));
end;

procedure TForm1.PrintDialog1Show(Sender: TObject);
begin
  Memo1.Clear;
  EnumChildWindows(printdialog1.Handle, @EnumProc, Integer(memo1.Lines));
end;

Open in new window


http://www.swissdelphicenter.ch/torry/showcode.php?id=327
Avatar of wackyteseo

ASKER

i have error of variabile at line 15
You need to pass the handle of the required window:
EnumChildWindows(YouWindowHandle, @EnumProc, Integer(memo1.Lines));
yes but i have the error "Variable required"
Where does the compiler point at when you get that error?

To make the above code works, drop memo and button on your form at the button OnClick you will call the function by passing the handle of the form (or any other window handle):
function EnumProc(wnd: HWND; Lines: TStrings): BOOL; stdcall;
var
  buf, Caption: array[0..255] of char;
begin
  Result := True;
  GetClassName(wnd, buf, SizeOf(buf) - 1);
  SendMessage(wnd, WM_GETTEXT, 256, Integer(@Caption));
  Lines.Add(Format('ID: %d, ClassName: %s, Caption: %s',
           [GetDlgCtrlID(wnd), buf, Caption]));
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Clear;
  EnumChildWindows(Handle, @EnumProc, Integer(memo1.Lines));
end;

Open in new window

Enumchild now is ok , but , with the handle of exe file no has child , with winspector , i have found that the child of this software are with class CtsGuiClass.frame but with no name how can i recognize ?
>  "i have found that the child of this software are with class CtsGuiClass.frame but with no name how can i recognize"
I did not get your point!

Here is an updated one:
var
  Searchhandle: HWND;

function EnumProcess(hHwnd: HWND; lParam : integer): boolean; stdcall;
var
  pPid : DWORD;
  title, ClassName : string;
begin
  if (hHwnd=NULL) then
    begin
      result := false;
    end
  else
    begin
      GetWindowThreadProcessId(hHwnd,pPid);
      SetLength(ClassName, 255);
      SetLength(ClassName, GetClassName(hHwnd, PChar(className), Length(className)));
      SetLength(title, 255);
      SetLength(title, GetWindowText(hHwnd, PChar(title), Length(title)));
      if (hHwnd = Searchhandle) or (GetParent(hHwnd) = Searchhandle) then
        begin
          Form1.Memo1.Lines.Add('Class Name = '+ className +', Title = '+ title + ', HWND = '+ IntToStr(hHwnd) +', Pid = ' + IntToStr(pPid));
        end;
      Result := true;
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Lines.Clear;

  Searchhandle := 1234;  // Provide the handle you want to list its details
  if EnumWindows(@EnumProcess,0) = False then
    ShowMessage('Error: Could not obtain process window hook from system.');
end;

Open in new window

Hi JimyX , I get Error in Line 33

Variable required
unit Unit1;

interface

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

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

var
  Form1: TForm1;


implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Lines.Clear;

  Searchhandle := 1234;  // Provide the handle you want to list its details
  if EnumWindows(@EnumProcess,0) = False then
    ShowMessage('Error: Could not obtain process window hook from system.');



end;

function EnumProcess(hHwnd: HWND; lParam : integer): boolean; stdcall;
var
  pPid : DWORD;
  title, ClassName : string;
begin
  if (hHwnd=NULL) then
    begin
      result := false;
    end
  else
    begin
      GetWindowThreadProcessId(hHwnd,pPid);
      SetLength(ClassName, 255);
      SetLength(ClassName, GetClassName(hHwnd, PChar(className), Length(className)));
      SetLength(title, 255);
      SetLength(title, GetWindowText(hHwnd, PChar(title), Length(title)));
      if (hHwnd = Searchhandle) or (GetParent(hHwnd) = Searchhandle) then
        begin
          Form1.Memo1.Lines.Add('Class Name = '+ className +', Title = '+ title + ', HWND = '+ IntToStr(hHwnd) +', Pid = ' + IntToStr(pPid));
        end;
      Result := true;
    end;
end;

Open in new window

The function "EnumProcess" must be on top of the calling procedure of button1Click, as follows:
unit Unit1;

interface

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

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

var
  Form1: TForm1;


implementation

{$R *.dfm}

function EnumProcess(hHwnd: HWND; lParam : integer): boolean; stdcall;
var
  pPid : DWORD;
  title, ClassName : string;
begin
  if (hHwnd=NULL) then
    begin
      result := false;
    end
  else
    begin
      GetWindowThreadProcessId(hHwnd,pPid);
      SetLength(ClassName, 255);
      SetLength(ClassName, GetClassName(hHwnd, PChar(className), Length(className)));
      SetLength(title, 255);
      SetLength(title, GetWindowText(hHwnd, PChar(title), Length(title)));
      if (hHwnd = Searchhandle) or (GetParent(hHwnd) = Searchhandle) then
        begin
          Form1.Memo1.Lines.Add('Class Name = '+ className +', Title = '+ title + ', HWND = '+ IntToStr(hHwnd) +', Pid = ' + IntToStr(pPid));
        end;
      Result := true;
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Lines.Clear;

  Searchhandle := 1234;  // Provide the handle you want to list its details
  if EnumWindows(@EnumProcess,0) = False then
    ShowMessage('Error: Could not obtain process window hook from system.');



end;

Open in new window

Ok , but now i get the error on line 45
Undeclared identifier "Searchhandle"
i have put in the same position of your code
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;

    procedure Button1Click(Sender: TObject);
  
  private
    { Private declarations }
  public
    { Public declarations }
     Searchhandle: HWND;
  end;

var
  Form1: TForm1;



implementation

{$R *.dfm}

function EnumProcess(hHwnd: HWND; lParam : integer): boolean; stdcall;
var
  pPid : DWORD;
  title, ClassName : string;
begin
  if (hHwnd=NULL) then
    begin
      result := false;
    end
  else
    begin
      GetWindowThreadProcessId(hHwnd,pPid);
      SetLength(ClassName, 255);
      SetLength(ClassName, GetClassName(hHwnd, PChar(className), Length(className)));
      SetLength(title, 255);
      SetLength(title, GetWindowText(hHwnd, PChar(title), Length(title)));
      if (hHwnd = Searchhandle) or (GetParent(hHwnd) = Searchhandle) then
        begin
          Form1.Memo1.Lines.Add('Class Name = '+ className +', Title = '+ title + ', HWND = '+ IntToStr(hHwnd) +', Pid = ' + IntToStr(pPid));
        end;
      Result := true;
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Lines.Clear;
  Searchhandle := 1234;  // Provide the handle you want to list its details
  if EnumWindows(@EnumProcess,0) = False then
    ShowMessage('Error: Could not obtain process window hook from system.');



end;



end.

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of jimyX
jimyX

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have fixed the issue, please review that.