Results 1 to 5 of 5

Thread: [RESOLVED] WM_GETTEXT vs GetWindowText

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    45

    Resolved [RESOLVED] WM_GETTEXT vs GetWindowText

    I was trying to get the text of a control from an application. So I first tried using GetWindowText and got a string back but to my surprise it wasn't the text in the actual control. So I used WM_GETTEXT instead and now I got the text of the control. Why is it that I get two different results using these two approaches? Doesn't GetWindowText just send the WM_GETTEXT message?

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: WM_GETTEXT vs GetWindowText

    The GetWindowText function copies the text of the specified window's title bar (if it has one) into a buffer. If the specified window is a control, the text of the control is copied. However, GetWindowText cannot retrieve the text of a control in another application.

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    45

    Re: WM_GETTEXT vs GetWindowText

    Yes I know what the function is supposed to do. I'm also aware that the MSDN library claimes that it can't be used to retrieve the text of a textbox from another process, but I've used it many times retrieving for example the text of a button and the like. What I wondered was what the difference between the WM_GETTEXT message and the GetWindowText function really is? I also wondered why I got two different results with a specific control in another process? If GetWindowText doesn't send the WM_GETTEXT message I also wonder why not?

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: WM_GETTEXT vs GetWindowText

    There is two different ways a window can handle its text. Either let system handle it or handle it themself. If the system handles it it will store the text passed to CreateWindow/CreateWindowEx in a dedicated memory area. This is returned by GetWindowText or by sending WM_GETTEXT. However the window/control can handle it themselfs, which is pretty common for a control to do. In that case the control handles the WM_GETTEXT and WM_GETTEXTLENGTH messages and returns whatever text they want/use.

    So when you send a WM_GETTEXT message, if the window/control doesn't handle that message itself, Windows will return the string stored in the dedicated memory area when the window was created, and it will replace it if a WM_SETTEXT message is sent. This is the default behaviour.

    Now, if you use the GetWindowText function it will send a WM_GETTEXT message if the window you're requesting the text for have been created by the same process that calls GetWindowText. But if you use GetWindowText on a control in another process it will return the text stored in this special memory area and not send a WM_GETTEXT message. The reason for this is simply because if GetWindowText would send the message to a window that is hung it would never return, hence hanging the process that call the function. That would cause a huge problem for such things as the task switcher that would hang if one window is hung. A hung application/window shouldn't cause problems for other applications/windows.

    This is also the reason you can get different values using GetWindowText and sending WM_GETTEXT to a control. The control was created with a call to CreateWindow or CreateWindowEx with one text (that will be returned by GetWindowText) and then handles the WM_GETTEXT message itself and returns something else. So if you want to be sure to get the correct text you should use WM_GETTEXT for other processes, however you should also be aware that if the window/control in question is hung SendMessage will never return. So the best approach is to use SendMessageTimeout instead of SendMessage.

  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    45

    Re: WM_GETTEXT vs GetWindowText

    I'm sorry for the long delay, but thank you very much Joacim. That makes it very clear.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width