Reputation: 37
In an c# app, I am performing a EnumWindows of all windows on the desktop and in the callback I get the window title for each window. One 3rd party app in particular can be run in ansi or unicode mode, is it safe to use GetWindowTextW all the time even though the app is running in ansi mode or do I need to be able to detect whether unicode mode is enabled for all external apps in order to determine which GetWindowText I need to call? The problem I'm hitting is we have been using GetWindowTextA even on the unicode apps and has worked up until now. It fails to return the correct window title for obvious reasons when there are chinese characters in the title bar.
Upvotes: 1
Views: 2503
Reputation: 101569
The wide functions are always the best choice (assuming you don't care about Windows 95/98/ME).
The window text can come from two places:
Unicode text "stored internally in the HWND". Most top-level windows store their text here. GetWindowTextW
is always able to retrieve this text.
Some applications dynamically handle the WM_*TEXT*
messages. The window manager handles character conversion for you (SendMessageA
vs SendMessageW
) so you don't have to think about it.
GetWindowTextW
will not send the WM_*TEXT*
messages if you call it on a window in a different process. If you want to always get the "correct" text of windows in other processes you manually have to call SendMessageTimeoutW(..., WM_GETTEXT, ...)
first and if that fails with a timeout, fall back to GetWindowTextW
.
Upvotes: 4