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

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5137
  • Last Modified:

FindWindow question.


this question is related to icq.

ICQ's messenger window name is #32770.

So I do: handlexyz := FindWindow('#32770', nil);
The textbox of icq is a RichEdit20A.

In that messenger (#32770) window there is one RichEdit20A for the textbox where you type messenges and one RichEdit20A for the messeges of the other person.

So when I do a handlendxyz := FindWindowEx(WIN, 0, 'RichEdit20A', nil);
and then a SendMessage(hwndxyz, WM_GETTEXT, 255, integer(@Buffer));
then it sometimes returns the text of the input textbox and sometimes the text of the other person.

Do you have any ideas why sometimes it returns the input textbox text and sometimes the text of the other person. I guess it has something to do with the focus. The question is, do you have any idea how to get a handle on both textboxes?

1 Solution
you have 2 controls with name "RichEdit20A" ... ?

I recommend you to check it again. As I a tool, I can recomment you is:

> "FindWindowEx(WIN, 0, 'RichEdit20A', nil);"
WIN is the the handle of the parent window. So you should do it like this:

handlexyz := FindWindow('#32770', nil);
handlendxyz := FindWindowEx(handlexyz, 0, 'RichEdit20A', nil);
Hmm... with ICQ it is a bit hard huh... relying on #32770 alone is quite 'dangerous', because that is the name used by all standard dialogs.

I guess what you can do after the FindWindowEx is... to check the caption of the window? Because ICQ message windows contain the phrase "Message Session" at the back.

Or, you can actually enumerate through all windows and find the one that contains "Message Session" as its windows title...
Eddie ShipmanAll-around developerCommented:
Dragon Slayer, the second parameter of FindWindow is the Window Title, no need to use FindWindowEx.
From MSDN:

  HWND FindWindow(      
    LPCTSTR lpClassName,
    LPCTSTR lpWindowName

        [in] Pointer to a null-terminated string that specifies the class name or a class atom created
              by a previous call to the RegisterClass or RegisterClassEx function. The atom must be in
              the low-order word of lpClassName; the high-order word must be zero.

        If lpClassName points to a string, it specifies the window class name. The class name can
        be any name registered with RegisterClass or RegisterClassEx, or any of the predefined
        control-class names.

        If lpClassName is NULL, it finds any window whose title matches the lpWindowName parameter.

        [in] Pointer to a null-terminated string that specifies the window name (the window's title).
              If this parameter is NULL, all window names match.

Return Value

    If the function succeeds, the return value is a handle to the window that has the specified class
    name and window name.

    If the function fails, the return value is NULL. To get extended error information, call GetLastError."
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.

TonyJizAuthor Commented:

I used that tool. The ICQ message window is just a textbox (RichEdit20A) with a line that seperates the textbox for input and the textbox for messages from the other user. So maybe that there only is one RichEdit20A, that's possible since sometimes the GETTEXT returns the input message and sometimes it returns the messages from the other user. I have no idea why.
About the WIN handle, i copy pasted that wrong, I did do it with the handlexyz and not the win handle.


I don't just rely on #32770, also on the filename, so no need for the message window. It's just that sometimes RichEdit20A returns the input messages and sometimes the messages from the other user. I just want to retrieve the text from both message windows.

Russell LibbySoftware Engineer, Advisory Commented:

To answer your question, FindWindow(ex) works by finding the first window in the z-order that matches your criteria. FindWindowEx is a little more advanced, as you can specify a child window to start the search from, for example, in cases when you want to continue a search...

What is happening (I believe) is that the z-order is changing depending on focus, thus depending on when you call findwindowex, you may get either the upper or lower edit window.

An easy solution, and one that will give you both handles, is to call FindWindowEx twice. Once with a child handle of zero, and the second time with  the child handle from the first call. Then check the client rects to determine which is the upper, and which is the lower.


var  hwndICQ:       HWND;
     hwndSwap:      HWND;
     hwndEdit1:     HWND;
     hwndEdit2:     HWND;
     rcEdit1:       TRect;
     rcEdit2:       TRect;

  // Get base window
  hwndICQ:=FindWindow('#32770', nil);

  // Check handle
  if (hwndICQ <> 0) then
     // Get first edit window in z-order
     hwndEdit1:=FindWindowEx(hwndICQ, 0, 'RichEdit20A', nil);
     // If non null, get next edit window
     if (hwndEdit1 <> 0) then hwndEdit2:=FindWindowEx(hwndICQ, hwndEdit1, 'RichEdit20A', nil);
     // Check handles
     if (hwndEdit1 <> 0) and (hwndEdit2 <> 0) and (hwndEdit1 <> hwndEdit2) then
        // Get rects for both windows
        Windows.GetClientRect(hwndEdit1, rcEdit1);
        Windows.GetClientRect(hwndEdit2, rcEdit2);
        // Swap if out of order (edit2's top is less than edit1's top)
        if (rcEdit2.Top < rcEdit1.Top) then
           // Swap handles


This will give you both handles, and it also allows you to determine which is which.

Hope this helps,

TonyJizAuthor Commented:
That's some great stuff Russell! Points given.

Hey could you also goto http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_21217560.html maybe you know that too.

Anyway, thnx!

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now