user4582812
user4582812

Reputation: 621

How to get the text of an "EDIT" control from another thread?

I have read that I should only use PostMessage() to tell the UI thread to access the UI controls.

There is no problem in following this approach if I am for example setting the text of an "EDIT" control. However, what if I want to get the text of an "EDIT" control, if I send a message to the UI thread using PostMessage(), then PostMessage() will return immediately before the text is set into the buffer, so how should I solve this problem?

Upvotes: 0

Views: 109

Answers (1)

David Heffernan
David Heffernan

Reputation: 612794

You cannot use PostMessage for this purpose for the reasons that you identify. WM_GETTEXT is a synchronous message. What you should do is:

  • If the window is in your process, then you should use GetWindowText.
  • If the window is in a different process, then you should call SendMessageTimeout.

Why SendMessageTimeout rather than SendMessage? Well, if the other process has hung and is not responding, then using SendMessage would never return.

There is more discussion of this topic here: The secret life of GetWindowText.

Upvotes: 2

Related Questions