Detect the handle of child window of another application

Hi have one solution in this discussion :
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_26910900.html?cid=239#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 ?
wackyteseoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

systanCommented:
SendMessage(FindWindowEx(FindWindow('TForm1', nil), 0, 'TListBox', nil), LB_INSERTSTRING, 0, Integer(PChar(getcommandline + ' CREATED with HANDLE ' + IntToStr(wp) )));
0
systanCommented:
>>but this time by type of class #32770 ? and detect child of .exe ?
Oh, we'll try, later.
0
jimyXCommented:
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
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

wackyteseoAuthor Commented:
i have error of variabile at line 15
0
jimyXCommented:
You need to pass the handle of the required window:
EnumChildWindows(YouWindowHandle, @EnumProc, Integer(memo1.Lines));
0
wackyteseoAuthor Commented:
yes but i have the error "Variable required"
0
jimyXCommented:
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

0
wackyteseoAuthor Commented:
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 ?
0
jimyXCommented:
>  "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

0
wackyteseoAuthor Commented:
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

0
jimyXCommented:
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

0
wackyteseoAuthor Commented:
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

0
jimyXCommented:
Though I replied to the Author already.
I can still try to continue this in case the Author is still interested.

Move the "Searchhandle: HWND;" to the global VAR:
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 }
  end;

var
  Form1: TForm1;
  Searchhandle: HWND;

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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jimyXCommented:
I have fixed the issue, please review that.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.