Irbis
Irbis

Reputation: 1511

Winapi - passing LPWCSTR as LPCSTR

I use CreateWindowEx which expands to CreateWindowExA. That function uses LPCSTR types. I would like to pass as a second argument MSFTEDIT_CLASS (from Richedit.h):

#define MSFTEDIT_CLASS L"RICHEDIT50W"

The following casting doesn't work:

(LPCSTR)MSFTEDIT_CLASS

CreateWindowEx returns NULL. It works when I pass the second argument this way:

"RICHEDIT50W"

but I don't want to copy a string from the header. How to fix that ?

Upvotes: 1

Views: 207

Answers (1)

IInspectable
IInspectable

Reputation: 51511

There is only a single, viable solution here: Call CreateWindowExW, either directly or by defining the UNICODE preprocessor symbol and have the generic-text mapping CreateWindowEx expand to CreateWindowExW.

The window you are creating is a Unicode window, always. The character set used for communicating with a window is set at class registration time. The window class named "RICHEDIT50W" is registered using RegisterClassExW by the system. You don't have control over this.

Since you are eventually going to have to talk to the window using messages, you will need to use the Unicode variants of the message handling functions (GetMessageW, DispatchMessageW, etc.). You cannot use the ANSI versions, unless you are happy with an application, that sometimes doesn't fail.

Upvotes: 6

Related Questions