Enter GetWindowText
THE GETWINDOWTEXT function has a problem: Window text needs to be readily available without hanging. FindWindow needs to get window text to find a window. Task-switching applications need to get window text so that they can display the window title in the switcher window. It should not be possible for a hung application to clog up other applications. This is particularly true of the task-switcher scenario.
This argues against sending WM_GETTEXT messages, because the target window of the WM_GETTEXT might be hung. Instead, GetWindowText should use the “special place” because that cannot be affected by hung applications.
On the other hand, GetWindowText is used to retrieve text from controls on a dialog, and those controls frequently employ custom text management. This argues for sending WM_GETTEXT messages, because that is the only way to retrieve custom-managed text.
GetWindowText strikes a compromise:
- If you are trying to get the window text from a window in your own process, GetWindowText will send the WM_GETTEXT message.
- If you are trying to get the window from a window in another process, GetWindowText will use the string from the special place and not send a message.
According to the first rule, if you are trying to get text from a window in your own process, and the window is hung, GetWindowText will also hang. But because the window belongs to your process, it’s your own fault, and you deserve to lose. Sending the WM_GETTEXT message ensures that text from windows that do custom text management (typically, custom controls) are properly retrieved.
According to the second rule, if you are trying to get text from a window in another process, GetWindowText will not send a message; it just retrieves the string from the special place. Because the most common reason for getting text from a window in another process is to get the title of the frame, and because frame windows typically do not do custom window text manipulation, this usually gets the right string.
The documentation simplifies this as “GetWindowText cannot retrieve text from a window from another application.”