Home c++ Another "Cannot convert const char * to LPCWSTR"

Another “Cannot convert const char * to LPCWSTR”

Author

Date

Category

There is a self-written function in the library file connected to the main one:

HWND CreateEditor (
  TCHAR * tText,
  HWND hParent,
  POINT pCoords = {0, 0},
  SHAPE Size = {200, 50},
  DWORD dStyle = WS_CHILD | WS_VISIBLE | SS_LEFT)
{
  return CreateWindow ("edit", tText, dStyle, pCoords.x, pCoords.y, Size.width, Size.height, hParent, (HMENU) NULL, hInstance, NULL);
}

The main file declares:

# define _UNICODE

The compiler swears at line 10 like this:

“Cannot convert const char * to LPCWSTR for argument 2 to HWND __ * CreateWindowExW (DWORD, LPCWSTR, LPCTWSTR, int, int, int, int, HWND, HMENUHINSTANCE, LPVOID)”

Please help.


Answer 1, authority 100%

LPCWSTR is a synonym for CONST WCHAR * , see page msdn. In natural C++ terms, this would be const wchar_t * . To make the string literal compatible with the mentioned type, you need to add the prefix L , i.e. in your case use L "edit" . You can also get by with the WinAPI macro _T , which takes into account the presence of the project’s unicode nature. In this case, you can use the _T ("edit") entry.

For more information, see the linked question: char * to LPWSTR

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions