Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2005-03-30
18
Medium Priority
?
501 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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This is an update to some code that someone else posted on Experts Exchange. It is an alternate approach, I think a little easier to use, & makes sure that things like the Task Bar will update.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

580 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