Click here to Skip to main content
15,390,197 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to write data in edit control and copy it in char buffer on button click event.But when I am copy buffer, it add 0 after each byte in it, so it copies half of size buffer in destination buffer and in resultant edit control also.
Why it add one more zero in char buffer?

What I have tried:

Use GetDlgItemTextW() API for copy text from edit control to char buffer as
<br />
GetDlgItemTextW(IDC_CONSOLE,(LPTSTR)buff,index);//index = number of characters entered in edit control

Read back data to destination buffer and placed it into another edit control using CString format :
CString msg;<br />
msg.Format(L"%hc",TempBuff);//TempBuff is destinaton buff<br />

Anything wrong with this code?Please suggest!
Updated 30-May-16 20:45pm
CPallini 31-May-16 2:50am
It looks you are using wide characters in a ANSI context. Are you using a UNICODE or ANSI build?

1 solution

You set your buffer (buff) as a unicode (2 bytes for each character) string (You call the GetDlgItemTextW function which works with unicode strings and, pass a LPTSTR to it which is a wchar_t* if _UNICODE is defined.).

If you try to read buff as a char* (1 byte for each character), that can be the reason you see an additional 0 after each byte.

Venkat Raghvan 31-May-16 5:09am
Thanks!my problem is solved,but it can read all bytes from edit control in buffer except last one.What is the reason of that?
Shmuel Zang 31-May-16 7:35am
What's the value of index? Is that the string's length?
Venkat Raghvan 31-May-16 8:25am
yes! Characters entered in edit control
Jochen Arndt 31-May-16 8:46am
The last parameter to GetDlgItemText[W] is the maximum length including the NULL character. So your buffer must have a size of at least index+1 characters and you must also pass that value.
Venkat Raghvan 31-May-16 9:37am
Ok! I already done and it works fine. Thank you for your valuable support

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900