Discussion:
GetWindowText
(too old to reply)
Mike Larson
2003-09-16 18:04:20 UTC
Hello,

I'm working in a plain DLL, not a driver.

I am trying to use the GetWindowText function to get the text in a TextBox
from a VB6 app.

The GetWindowText function in always returning an empty string. Here is the
code:
long _stdcall ReadText (HWND ReadControl, HWND WriteControl)
{
char ReceiveString [40];
GetWindowText(ReadControl, ReceiveString, 40);
SetWindowText(WriteControl, ReceiveString);
SendMessage(WriteControl, WM_KEYUP, 0, 0);

return ((long)ReceiveString [0]);
}

ReadControlis the hWnd value from the VB6 app of the TextBox to read,
WriteControl is the textbox to copy to.

Am I using it correctly? Is there another function I should be using.

I'm returning the first character just to see what if anything is being
read.

Thanks for any help.

Mike
Mike Larson
2003-09-16 18:40:22 UTC
Thanks for the reply.

It turns out that I forgot that I cleared the textbox in the VB app before
the call to ReadText. As it turns out both GetWindowText and the
SendMessage with WM_GETTEXT works as expected.

Thanks again.
Instead of using GetWindowText() use WM_GETTEXT message. Something
like
long _stdcall ReadText (HWND ReadControl, HWND WriteControl)
{
TCHAR ReceiveString [MAX_PATH+1] = {0};
SendMessage( ReadControl, WM_GETTEXT, MAX_PATH,
(LPARAM)(LPTSTR)ReceiveString );
..........
..........
}
--
Cheers
Check Abdoul [ VC++ MVP ]
-----------------------------------
Post by Mike Larson
Hello,
I'm working in a plain DLL, not a driver.
I am trying to use the GetWindowText function to get the text in a TextBox
from a VB6 app.
The GetWindowText function in always returning an empty string. Here is
the
Post by Mike Larson
long _stdcall ReadText (HWND ReadControl, HWND WriteControl)
{
char ReceiveString [40];
GetWindowText(ReadControl, ReceiveString, 40);
SetWindowText(WriteControl, ReceiveString);
SendMessage(WriteControl, WM_KEYUP, 0, 0);
return ((long)ReceiveString [0]);
}
ReadControlis the hWnd value from the VB6 app of the TextBox to read,
WriteControl is the textbox to copy to.
Am I using it correctly? Is there another function I should be using.
I'm returning the first character just to see what if anything is being
read.
Thanks for any help.
Mike
Alan Carre
2003-09-17 10:44:40 UTC
Actually... strangely enough, you don't need to send a message OR call GetWindowText.
You can simply use "DefWindowProc" with the 'messages' WM_GETTEXTLENGTH and
WM_GETTEXT to get the length first, and then the text. I know it sounds crazy, but it
works. DefWindowProc doesn't need to be called inside the WndProc, or even the
process associated with the HWND parameter.

- Alan

P.S. Also note that, sending the message can cause your application to hang if the
receving thread is blocked for some reason.
Post by Mike Larson
It turns out that I forgot that I cleared the textbox in the VB app before
the call to ReadText. As it turns out both GetWindowText and the
SendMessage with WM_GETTEXT works as expected.
Just by the way, note that GetWindowText() is guaranteed to work only when
the target window is owned by a thread in the same process as the calling
thread. Otherwise, you need to send the message.
Regards,
Will
William DePalo [MVP VC++ ]
2003-09-17 14:55:15 UTC
Post by Alan Carre
Actually... strangely enough, you don't need to send a
message OR call GetWindowText. You can simply use
"DefWindowProc" with the 'messages' WM_GETTEXTLENGTH
and WM_GETTEXT to get the length first, and then the text.
I know it sounds crazy, but it works.
Yes, it does sound crazy. :-) You have tried this on NT/2K/XP?
Post by Alan Carre
P.S. Also note that, sending the message can cause
your application to hang if the receving thread is blocked
for some reason.
Right. That's why there is a SendMessageTimeout() function.

Regards,
Will