NashiSM
NashiSM

Reputation: 1

Only showing one character while printing in C++

This is my code:

auto text = new wchar_t[WCHAR_MAX];
GetWindowTextW(hEdit, text, WCHAR_MAX);
SetWindowTextW(hWnd, text);
printf_s((const char *)text);

While printing, the char (text), it only outputs one character to the console. It is a WINAPI gui and a console running together. It sets the winapi title successfully and get the text successfully, but i have no idea why this is only printing out one character to the console...

Upvotes: 0

Views: 197

Answers (1)

owacoder
owacoder

Reputation: 4873

You're performing a raw cast from a wide string to a narrow string. This conversion is never safe.

Wide strings are stored as two-byte words in Windows. In your case, the high byte of the first character is 0, and x86 is little-endian, so the print stops at the first character.

Upvotes: 3

Related Questions