?
Solved

How to get the handle  of a control we know its identifier

Posted on 2005-03-30
18
Medium Priority
?
485 Views
Last Modified: 2010-04-16
Hi,
A control in an external application. Its "Control ID" in WinDowse is 000007D5.
Can we get the handle of a control using its Control ID?
We already know the parent window's handle.

Here is some info obtained from WinDowse

*** Window Information *********************************************************

Text                                                                            
Process ID          00000F24                                                    
App instance        00400000                                                    
Handle              004704A6                                                    
Parent handle       00600376                                                    
Control ID          000007D5                                                    
Function            00000000                                                    
Menu handle         00000000                                                    
Coords in parent    left:230, top:370, right:311, bottom:398                    
Coords in screen    left:440, top:494, right:521, bottom:522                    
Window size         width:81, height:28                                        
Client area size    width:81, height:28                                        
Style               40000004                                                    
Extended style      00000000                                                    
ws_overlapped | ws_child | ws_ex_left | ws_ex_ltrreading | ws_ex_rightscrollbar


*** Class Information **********************************************************

Name                AfxWnd42s                                                  
Function            77D45C55                                                    
Icon                00000000                                                    
Small icon          00000000                                                    
Cursor              00010011                                                    
Bkg brush           00000000                                                    
Module handle       00400000                                                    
Style               0000000B                                                    
cs_vredraw | cs_hredraw | cs_dblclks


*** Parents ********************************************************************

#    Handle    Class name                       Text                            

001  00600376  #32770                           Beauty & the Beast


*** Children *******************************************************************

#    Handle    Class name                       Text                            



*** Digger *********************************************************************

#    Handle    Class name                       Text                            

001  00600376  #32770                           Beauty & the Beast
002  00010088  SysListView32                    FolderView                      
003  00010086  SHELLDLL_DefView                                                
004  00010084  Progman                          Program Manager                

0
Comment
Question by:xo310
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 6
18 Comments
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 13664785
what about using FindWindow() and FindWindowEx() functions?
0
 

Author Comment

by:xo310
ID: 13664845
there are many controls that share the same class name with the control I need.
but for a unique control in the application like a richedit I use this at the moment
FUNCTION FindRichEditHwnd: Integer;
VAR
  Wnd: Cardinal;
  {the parent handle}
  wnd1: Integer;
  {The richedit handle}
  Txt: PChar;
BEGIN
  Wnd := 0;
  GetMem(Txt, 256);
  REPEAT
    Wnd := FindWindowEx(0, Wnd, '#32770', NIL);
    GetWindowText(Wnd, Txt, 256);
    BEGIN
      IF (AnsiContainsText(Txt, 'Good Luck') = True) THEN
      BEGIN
        Break;
      END;
    END;
  UNTIL Wnd = 0;
  FreeMem(Txt);
  IF Wnd <> 0 THEN
  BEGIN
    wnd1 := FindWindowEx(Wnd, 0, 'RICHEDIT', NIL);
    Hwnd := wnd1
  END
  ELSE
  BEGIN
    Form1.Caption := 'could not get handle';
  END;
END;
0
 

Author Comment

by:xo310
ID: 13664856
There are 15 controls with class name:AfxWnd42s int eh same window
but there IDs or identifiers are different
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:xo310
ID: 13664868
their
0
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 13664885
what about captions? do they have any?
0
 

Author Comment

by:xo310
ID: 13664923
They do have text on them but does not appear when mouse is over them using Windowse.
I want the handle to try getwindowtext with them.
0
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 13664990
Is that program (you want to interact with) freely available somewhere? I could download it and try to help...
0
 

Author Comment

by:xo310
ID: 13665119
yes it is empirepoker.com
you dont have to register to see tables.
the controls I am talking about are player names.
thanks alot
0
 

Author Comment

by:xo310
ID: 13665234
I am also trying EnumChilWindow but this code keeps telling me "variable required" with the cursor after "addr(EnumChildWindowFunc)"

procedure TForm1.Button4Click(Sender: TObject);
begin
 Memo1.Clear;
 EnumChildWindows(ParentGameHwnd, addr(EnumChildWindowFunc), Integer(memo1.Lines));
end;

Function TForm1.EnumChildWindowFunc(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;
0
 

Author Comment

by:xo310
ID: 13665300
ok forget about the new Question it si because it should be a function not a method
0
 
LVL 34

Expert Comment

by:Slick812
ID: 13665786
not sure about what you might need here.. .  but if you have an ID number for a Control, you can get it's Handle from it's parent with the GetDlgItem( ) function. . . .

hScroll := GetDlgItem(hParentWnd, ID_Number);

but in your comment above you have


Handle              004704A6                                                    
Parent handle       00600376                                                    
Control ID          000007D5  


wouldn'i the control handle be -
    $4704A6


0
 

Author Comment

by:xo310
ID: 13665969
the handle will change from my pc to another
I tried it with the resulting name being "S‹Ø‹ÃèÏ" which is meaningless there must be something wrong
here is the code:

procedure TForm1.Button4Click(Sender: TObject);
var
h1:cardinal;
txt:pchar;
begin
h1:=GetDlgItem(GameHwnd,$7D5);//GameHwnd is the parent window handle coming from another function
getwindowtext(h1,txt,100);
edit10.Text:=txt;
end;
0
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 13666089
You should allocate memory before using txt in getwindowtext() function. Try this:

procedure TForm1.Button4Click(Sender: TObject);
var
h1:cardinal;
txt:pchar;
begin
h1:=GetDlgItem(GameHwnd,$7D5);
GetMem(txt, 100); //allocate memory
getwindowtext(h1,txt,100);
edit10.Text:=txt;
FreeMem(txt); //free memory, when it is not needed anymore
end;
0
 
LVL 11

Accepted Solution

by:
ZhaawZ earned 2000 total points
ID: 13666249
Are you sure that AfxWnd42s is what you need?

var
  wnd : cardinal;
  rec : trect;
begin
wnd := WindowFromPoint(mouse.CursorPos);
wnd := FindWindowEx(wnd, 0, 'AfxWnd42s', nil);
GetWindowRect(wnd, rec);
with rec do ShowMessage(format('L:%d; T:%d; W:%d; H:%d;', [left, top, right - left, bottom - top]));

It showed me:
L:-244; T:134; W:81; H:28;


"Left" value is negative, it means that a control is outside a visible area of parent control.
0
 

Author Comment

by:xo310
ID: 13666353
yes it seems as you said. I used the handle i get from Windowse directly and no text came out.
so what do you advice?
No need for a full answer as I know this deserves more than 500 points:)
did you find any point where I can start from?
0
 

Author Comment

by:xo310
ID: 13666393
what made me think it is that class name is that the controls IDs associated with go  from D0 to D9 starting from the dealer. As if these controls hold information about players.
0
 

Author Comment

by:xo310
ID: 13666768
I enumerated all child windows of the parent and showed thier captions but there was not clue about player names.
I found some comments on the web:

"The AfxWnd42s class means the window is not an edit
control, and the text is painted by the application. The only possible
approach (not too practical) is to grab the painted image and employ
optical character recognition algorithms."-- Scott McPhillips [VC++ MVP]
0
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 13670764
All these player names are stored somewhere in memory. I have seen some programs that find some user-defined value in memory that belongs to some user-specified process, so I think it must be possible to read these player names from memory, but I don't know - how. I haven't worked with memory that seriously.
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month10 days, 11 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question