GetWindowText doesn't copy correctly

I've created a notepad in WIN API, everything is working great, except when I type a bit of information, I cannot get it from the screen...

for example, if I'll write -> "Hello, How are you?", it'll work correctly, but if I'll write "Hello, How are you? I hope you are doing well!" it wont give me the string..

I'm using the function GetWindowText to get the text the user typed....

I've checked for errors using GetLastError, and got 0 on it...

Here is my code :

1
2
3
4
5
6
7
8
9
			case ID_FILE_SAVE:
			{
				string data; // The data the user has typed will be stored here

				GetWindowText(Edit, LPSTR(data.c_str()), GetWindowTextLength(Edit) + 1); // Getting the data the user typed

				SaveDialog(hwnd, data); // Saving the data the user typed into a file
			}
			break;


The save it working correctly, but the problem is that when the user typed a bit of information(like said at the top of the topic), the function GetWindowText gives me this (It isnt showing an error, but in the debuger, when I want to see data's value it says this) :
"+ data <Error reading characters of string.> std::basic_string<char,std::char_traits<char>,std::allocator<char> >"

Thanks for you'r help!
as you know, GetWindowText() stores the string into a c-string, and it should contain enough memory to hold the text.

data is a string object -not a c-string-, strings are dynamically allocated, and when constructing them with the default constructor, they aren't allocating any space.

try resizing the string to fit the text inside the edit-control:
1
2
3
4
5
6
7
8
9
10
			case ID_FILE_SAVE:
			{
				string data; // The data the user has typed will be stored here
				data.resize( GetWindowTextLength( Edit ) + 1,'\0' ); // resize the string so iit can contain the text stored in the edit-control.

				GetWindowText(Edit, LPSTR(data.c_str()), GetWindowTextLength(Edit) + 1); // Getting the data the user typed

				SaveDialog(hwnd, data); // Saving the data the user typed into a file
			}
			break;


EDIT - fixed an error in the code.
Last edited on
Note that storage for a std::string is only guaranteed to be contiguous in C++11. With C++98 you should not really be using a std::string as a buffer; you should use std::vector.

Also, you should preferrably not use a cast to remove const-ness. Instead, get the address of the first element.

1
2
3
int bufferLen = GetWindowTextLength( Edit ) + 1; // why send the message twice?
string data(bufferLen, '\0');  
GetWindowText(Edit, &data[0], bufferLen);


or (with vector)

1
2
3
int bufferLen = GetWindowTextLength( Edit ) + 1; // why send the message twice?
vector<char> data(bufferLen); 
GetWindowText(Edit, &data[0], bufferLen);


Andy
Last edited on
Thanks guys!! you both helped me a lot!! :)
I've fixed my mistake thanks to you :)
Also, you're using the wrong character type. GetWindowText gives you TCHARs, not chars.

Use GetWindowTextA.

Relevant reading:

http://www.cplusplus.com/forum/windows/106683/
Thanks, but I've just saw in my code, that it says about GetWindowText :
#define GetWindowText GetWindowTextA (when I hand the mouse over the word GetWindowText)

so I'm not sure why is that a problem ?
GetWindowText is a #define of either GetWindowTextA or GetWindowTextW depending on your settings (because a TCHAR is either a char or wchar_t depending on the settings)

So yeah the code compiles for you now, but it's still wrong.

It's very simple:

GetWindowTextA takes char's
GetWindowTextW takes wchar_t's
GetWindowText takes TCHAR's


since you are not using TCHARs, you should not be using GetWindowText
Okay, thank you!!
Topic archived. No new replies allowed.