2

I am trying to get text from a textbox in a specific window. For this I'm using SendMessage Api function, I dont know if this is the corect way:

SendMessage(hwnd, WM_GETTEXT, 0, 0);

But I dont know how to print the text. For the argument 3 and 4 in msdn site it says: Additional message-specific information. So i dont know if I need to pass something else beside 0. I tried this also:

SendMessage(hwnd, WM_GETTEXT, sizeof(text), LPARAM(text));

But it prints the name of the textbox, I need to retrieve the text inside the box?
How can I do this? Is SendMessage() the correct API function to use ?

Thank you.

edit:

I omit to say, I am enumerating the child windows from a window, and for me it looks like a textbox, where you have to type a name. I am retrieving the username of a Instant messaging window so I cant compare it to a string, is that a textbox ?

2
  • Textboxes don't have a name. You're trying to read something else. Commented Apr 26, 2011 at 22:51
  • @Mark Ransom: how can I test if a child window is a textbox ? thanks. Commented Apr 26, 2011 at 23:09

3 Answers 3

6

You should use GetWindowText. More information here.

Sign up to request clarification or add additional context in comments.

16 Comments

Still the same, I'm getting the textbox name, not what it is inside here
@vBx: Ok, wild guess here: by textbox name, I take it you mean the third parameter to CreateWindowEx (or equivalent)? That should in fact be the textbox contents - when you give a textbox a name, that's what it displays as its contents. Similarly, if you read its name, you are in fact reading its contents. If it's not doing that, there's something funny going on.
If the window has a caption defined, that is what GetWindowText() and WM_GETTEXT return. Make sure you are specifying the HWND of the actual textbox itself, not its parent window.
Ok, i omit to say, I am enumerating the child windows from a window, and for me it looks like a textbox, where you have to type a name. I am retrieving the username of a Instant messaging window so I cant compare it to a string, is that a textbox ?
@vBx: the standard Edit control classname is "Edit". Spy++ displays IDs in hexidecimal, so 000000D3 is hex for the integer value 211. If you want to hard-code a hex number in your code, you have to use the 0x prefix, ie: 0x000000D3.
|
5

Read the MSDN documentation again. It does NOT say "Additional message-specific information" for those parameters:

wParam The maximum number of characters to be copied, including the terminating null character.

ANSI applications may have the string in the buffer reduced in size (to a minimum of half that of the wParam value) due to conversion from ANSI to Unicode.

lParam A pointer to the buffer that is to receive the text.

4 Comments

Well i taked the documentation from the SendMessage() page, thats how it says there
Anyway so the corect version is the second one, but why I am getting the textbox name and not the text inside the textbox ?
SendMessage() is a general-purpose function, hense its documented parameters are arbitrary. You need to look at the documentation for a particular message to know what the parameters represent. As for your code, using sizeof like you are will only work correctly if you are sending the WM_GETTEXT message to an Ansi window and not a Unicode window. Use IsWindowUnicode() to know whether to use an Ansi buffer or a Unicode buffer. As for why you are getting the textbox name, either you are sending the message to the wrong HWND to begin with, or the textbox has the WS_CAPTION style.
I was getting the handle of a label by mistake...but now I dont know how to get the handle of a textbox, do you know how can I test if a child window is a textbox ?
2

This code works for local only:

char *szText;
szText = (char *)GlobalAlloc(GPTR, 255);
SendMessage(hEditControl, WM_GETTEXT, 255, (LPARAM)szText);
MessageBox(hWnd, szText, "It's your message", MB_OK | MB_TOPMOST);
GlobalFree((HGLOBAL)szText);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.