handle strings

I have two edit box. One single line and one multiline.
Ide is get text from single line edit. Add it to wstring. Add newline to string.
Finally print string to multiline box.

Code look like that:

wstring text_buffer;
{
wchar_t buffer[256];
SendMessage(singleline,
EM_GETLINE,
(WPARAM) 0, // line 0
(LPARAM) buffer);
text_buffer += buffer;
text_buffer += L"\n";
SendMessage(multiline, WM_SETTEXT, 0, (LPARAM)(LPCTSTR) text_buffer.str());
}
if I give to singleline "Hello", I get to the multiline Hello, but not new line. As well it crach somewhere everything else after Hello.
If I add line: text_buffer += L"OK"; before text_buffer += buffer; print is O and it doesn't print anythig else
[899 byte] By [jolti] at [2007-11-19 7:24:39]
# 1 Re: handle strings
I'd do it that way:

#define _UNICODE
#include <windows.h>

wchar_t *buffer = NULL;
int len = 0;
wstring text_buffer;

len = GetWindowTextLength(singleline);
if ( !len )
{
/* handle error */
}
else
{
/* Retrieve window text */
buffer = (wchar_t*)malloc(sizeof(wchar_t) * len + 1);
len = GetWindowText(singleline, buffer, len);
buffer[len] = L'\0';

text_buffer += buffer;
text_buffer += L"\r\n"; /* Or is it \n\r :confused: */

SetWindowText(multiline, text_buffer.c_str());
free(buffer);
}
NoHero at 2007-11-9 13:15:11 >
# 2 Re: handle strings
Thanks No Hero

Code is OK. just few litlle bugs.

#define _UNICODE
#include <windows.h>

wchar_t *buffer = NULL;
int len = 0;
wstring text_buffer;

len = GetWindowTextLength(singleline);
if ( !len )
{
/* handle error */
}
else
{
/* Retrieve window text */
buffer = (wchar_t*)malloc(sizeof(wchar_t) * len + 1);
len = GetWindowText(singleline, buffer, len); // Should be len +1 and because buffer type is LPTSTR you can use strings and chars.
buffer[len] = L'\0';

text_buffer += buffer;
text_buffer += L"\r\n"; /* Or is it \n\r :confused: */

SetWindowText(multiline, text_buffer.c_str());
free(buffer);
}
jolti at 2007-11-9 13:16:12 >
# 3 Re: handle strings
Please use code tags when posting code...

// Should be len +1 and because buffer type is LPTSTR you can use strings and chars.

Well... Not really... There is just a space left for the terminating null character...
NoHero at 2007-11-9 13:17:08 >