# I switched the character set to unicode and now I have some compile errors

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

## Recommended Posts

I am using DXUT from the sdk samples. c++, DXUT requires the character set to be set to unicode but now everywhare i have a MessageBox() the 2nd arguement is just a string literal. it returns the error that it cant convert const char [x] to LPCWSTR. any ideas. I am going to try to just typecast it.

##### Share on other sites
Wrap your string literals with _T(). As in _T("I'm a string literal"). This will define the strings to be narrow when you build the app as a narrow character mode and wide when the app is build wide. Alternately you can just add the L prefix, but that changes the literal to be wide all the time.

##### Share on other sites
What does narrow and wide mean?

##### Share on other sites
Narrow means normal chars. Wide means wchar_t.

##### Share on other sites
do i need to source any libs for that to work? it doesn't seem to recognize _T()

##### Share on other sites
Try including tchar.h.

##### Share on other sites
thank you very much, you have been very helpful. Whare can i go to learn more in depth about how all this works?

what is unicode? and how is it related to tchars and such?

So its probably a good idea to always use _T() with string literals then?

thanks

##### Share on other sites
One place to start is the Internationalization section on MSDN for how the Microsoft runtime handles narrow and wide character conversions. TCHAR is a MS convention. The TCHAR type is equal to a char normally and wchar_t when UNICODE is defined.

Unicode is a character set mapping that tries to get all the symbols used by all the languages in the world into the same mapping. That way a single data type can be used to represent, for example, English, German and Japanese all in the same program. However, you can't fit all that information in a normal char, which is usually 8 bits and so can only hold 256 numbers. The original version of unicode used 16 bits; the current version uses 21 bits.

Using the _T convention on your strings makes the code easier to port to a unicode version, but hurts portability in general since many compilers don't provide an equivalent to tchar.h.

##### Share on other sites
thanks for all the help, I have one more question. is ther a way to get the LPCWSTR out of the stringw class. is ther a member function like the the c_str() function only a c_strw() funcion hehe.

##### Share on other sites
You should just be able to use the c_str() member function. ex:
  std::wstring wstr(L"Wide String");    LPCWSTR ptr = wstr.c_str();

##### Share on other sites
Hmm that doesn't work. I am using the D3DXGetImageInfoFromFile() with the string.c_str()

it gives me the following error when it complies

'D3DXGetImageInfoFromFileW' : cannot convert parameter 1 from 'const char *' to 'LPCWSTR'

any ideas?

##### Share on other sites
Take a look at my sample again. It's not a std::string it's a std::wstring, you need the w to specify that it stores wide characters.

##### Share on other sites
yeah i know but i think when unicode is selected, the compiler switches all of my string calls to wstring. but i might be wrong.

##### Share on other sites
You're wrong. UNICODE doesn't affect std::string declarations.

##### Share on other sites
Quote:
 Original post by donjonsonyeah i know but i think when unicode is selected, the compiler switches all of my string calls to wstring. but i might be wrong.

The compiler switches your Win32 calls to use wide characters. Functions like D3DXGetImageInfoFromFile() are actually #defined like this:

#ifdef UNICODE#define D3DXGetImageInfoFromFile D3DXGetImageInfoFromFileW#else#define D3DXGetImageInfoFromFile D3DXGetImageInfoFromFileA#endif

The STL does not work like this, however. You have to make sure to switch which version of string you are using.