Re: Safely reading text from HWND
"Joseph M. Newcomer" <newcomer@flounder.com> wrote in message
news:qc1634pd7c0khgjfb6n54a1ek29538fqsr@4ax.com...
Yes, but if the text changes in between the two APIs, there is going to be
a truncation of
the text if the new text is longer than the buffer. There's no way I
know of to prevent
this. One way to avoid this is to allocate a very large fixed buffer, and
if the length
returned is the length of the buffer (taking the terminal NUL into
account) there was a
truncation, and the operation should be tried again.
Not exactly, though your method is sound. If the buffer you pass it is
exactly the length of the window text (including NUL) then the text is not
truncated, but you can't tell whether it is or not. But truncated or not,
does it really matter? If you're showing the text in, say, a Task Manager
application, showing 4K of characters isn't exactly user friendly anyway.
BTW, Task Manager does not inject any DLL into all the processes, AFAIK. It
just calls GetWindowText(), or better, sends with a timeout the WM_GETTEXT
message, which returns the caption strings fine even from other processes.
OP does not need to inject a DLL to get the strings!
OTOH, if OP does go through the effort and performance overhead of injecting
a DLL, then if the DLL is injected using a Windows hook, e.g. a
WH_GETMESSAGE hook, then the callback is called in context of primary UI
thread, and then there is no way for caption text to change in the middle of
the callback.
-- David