Click here to Skip to main content
12,552,181 members (45,849 online)
Rate this:
Please Sign up or sign in to vote.
See more: VC8.0 C++

I have a Win32 library for which the character set is preset to - Use Unicode Character Set and I want to use a WCHAR array for storing a string literal constant.
For example:
WCHAR string[] = "MyWideCharString";

When I compile the code I get the error that states:
Error   1   error C2440: 'initializing' : cannot convert from 'const char [17]' to 'WCHAR []'   14

I tried using _T macro also,but I got a compile error again:
Error   1   error C3861: '_T': identifier not found 14

So how can I use the WCHAR character arrays in my library?
Posted 26-May-11 19:56pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The problem is the syntax for a string literal. The type WCHAR is the character type wchar_t; the string literal for this type should be prefixed with "L":

WCHAR string[] = L"MyWideCharString";

You can also use portable declaration style:

_TCHAR string[] = _T("MyWideCharString");

Here, _TCHAR and _T are just macro which works depending the definition of _UNICODE. They make type declaration either char or w_char and the string literal either L-prefixed or not, which allows you to switch from non-Unicode (ANSI) to Unicode version of the same code. This way, the type declarations and string literal using the macro remain consistent in either case.


Null-terminated portable string types based on _TCHAR are LPTSTR and LPCTSTR, see[^].

santosh dhanawade 27-May-11 2:18am
Good one -SA
Can tell me how to send bug to microsoft.
for please see my prevous post.
SAKryukov 27-May-11 2:43am
Thank you, Santosh,
I saw it, did not try to reproduce. I have no good idea about the situation with reporting to Microsoft. I've done it once too long time age, not sure Microsoft is really responsive.
SP 24 27-May-11 3:03am
+5 :)
HarishTheLearner 27-May-11 2:22am
Hi SAKryukov,
Thanks for the reply,
I studied from this link -, that said to use _T macro for Unicode!so I assume in case of MFC to use _T and for win32 to use L.
Please correct if I am wrong.
Niklas Lindquist 27-May-11 3:01am
_T(x) is only a macro which will expand to prefix its argument string with 'L' if UNICODE is defined or just x otherwise. It can be used with or without MFC.
SAKryukov 27-May-11 3:03am
Sure. See updated answer.
Niklas Lindquist 27-May-11 3:43am
A well deserved 5.
SAKryukov 27-May-11 4:05am
Thank you, Niklas.
Richard MacCutchan 27-May-11 4:59am
If you wish to use the _T() macro then you need to include tchar.h somewhere, either at the top of the .cpp file or in one of your other included headers.
SAKryukov 28-May-11 3:26am
Thank you, Richard -- important note.
HarishTheLearner 27-May-11 6:22am
Yes,Now I am able to get it running well with inclusion of tchar.h,Thank you all for the help
SAKryukov 28-May-11 3:24am
You're welcome.
Thanks for accepting this answer.
Good luck, call again,
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

WCHAR string[] = TEXT("MyWideCharString");
Richard MacCutchan 6-Jan-13 12:22pm
Why are you trying to answer a question that is nearly two years old?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.161021.1 | Last Updated 6 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100