Jump to content
  • Advertisement
Sign in to follow this  
Eldritch

Problems with const char* and LPCWSTR conversion

This topic is 4581 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!