win32 When trying to convert string to LPCWSTR, the result is Japanese characters

someone 20 Reputation points
2024-07-23T23:57:51.72+00:00

I want to make what key the user presses added to a string and then displayed in SetWindowText, but the output is Chinese characters

case WM_CHAR: 
{
	ifpressed;
	static std::string getwparam;
	getwparam.push_back( wParam );
	SetWindowText(hwnd, (wchar_t*)getwparam.c_str());
	break;
}

pls help

in the settings I have Character set: multi-byte chars

i couldn't find a solution to that :<

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,525 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,638 questions
0 comments No comments
{count} votes

Accepted answer
  1. Xiaopo Yang - MSFT 12,231 Reputation points Microsoft Vendor
    2024-07-24T00:48:45.8333333+00:00

    Hello @someone,

    First of all, according to the WM_CHAR message,

    The WM_CHAR message uses UTF-16 (16-bit Unicode Transformation Format) code units in its wParam if the Unicode version of the RegisterClass function was used to register the window class. Otherwise, the system provides characters in the current process code page, which can be set to UTF-8 in Windows Version 1903 (May 2019 Update) and newer. For more information, see Registering Window Classes and Use UTF-8 code pages in Windows apps.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. RLWA32 43,461 Reputation points
    2024-07-24T00:14:05.0266667+00:00

    If you are building with the MBCS character set why are you trying to cast using (LPCWSTR)? If you want to use unicode then set Unicode as the character set for your project. You cannot convert to unicode with a cast. That is the reason you are seeing what appears like Japanese or Chinese characters. Also, for unicode you should use std::wstring.

    1 person found this answer helpful.