Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problems with const char* and LPCWSTR conversion

Started by Eldritch May 29, 2006 at 12:43 PM 1 replies 12.5k views
Original Post
Eldritch
Eldritch
I am in the middle of writing an application, and have run into problems when trying to convert a const char* variable into an LPCWSTR type variable. Why convert? 1. CreateWindowEx(), which I use to create my application window with, takes only LPCWSTR type parameters for the window title/caption, so I simply do a type conversion (i.e. (LPCWSTR)windowTitle) on my parameter which is initially a const char*. 2. TextOut(), which I use to print text (I know it is supposed to be slow, but I am using it sparingly so do not worry about that), uses an LPCWSTR for the text that is to be printed, so I have a make a similar type conversion there as well. Now, converting from a const char* into an LPCWSTR makes the text appear garbled. Is there any way to solve this? I remember that it worked prior to acquiring Visual Studio 2005, when those functions accepted LPCSTR instead of the "wide char" (apparently unicode) LPCWSTR.
raz0r
raz0r
Go to your project settings and select "Use Multi-Byte Character Set" as your primary character set, or just #undef UNICODE before including windows header, or you can use TEXT macro, or you can directly call these ASCII functions. For example, CreateWindowExA and TextOutA.
janta
janta
Personally, I decided to use wide characters in my whole program to avoid such troubles.
If you are using static hard-coded strings somewhere you can prepend a L to specify wide character, like

WCHAR szName = L"My App";

Otherwise, maybe the function mbstowcs() can help you.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.