Click here to Skip to main content
11,416,787 members (62,225 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VC8.0
Hi,

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 20:56pm
hakz.code1.5K
Rate this: bad
good
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";

[EDIT]
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.

See http://msdn.microsoft.com/en-us/library/dybsewaf(v=VS.100).aspx[^].

Null-terminated portable string types based on _TCHAR are LPTSTR and LPCTSTR, see
http://stackoverflow.com/questions/321413/lpcstr-lpctstr-and-lptstr[^].

—SA
  Permalink  
v5
Comments
santosh dhanawade at 27-May-11 2:18am
   
Good one -SA
Can tell me how to send bug to microsoft.
for please see my prevous post.
SAKryukov at 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.
--SA
SP 24 at 27-May-11 3:03am
   
+5 :)
HarishTheLearner at 27-May-11 2:22am
   
Hi SAKryukov,
Thanks for the reply,
I studied from this link - http://msdn.microsoft.com/en-uslibrary/ey142t48%28v=vs.80%29.aspx#_core_mfc_support_for_unicode_strings, 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.
Thanks
Niklas Lindquist at 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 at 27-May-11 3:03am
   
Sure. See updated answer.
--SA
Niklas Lindquist at 27-May-11 3:43am
   
A well deserved 5.
SAKryukov at 27-May-11 4:05am
   
Thank you, Niklas.
--SA
Richard MacCutchan at 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 at 28-May-11 3:26am
   
Thank you, Richard -- important note.
--SA
HarishTheLearner at 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 at 28-May-11 3:24am
   
You're welcome.
Thanks for accepting this answer.
Good luck, call again,
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

WCHAR string[] = TEXT("MyWideCharString");
  Permalink  
Comments
Richard MacCutchan at 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
0 Sergey Alexandrovich Kryukov 9,295
1 OriginalGriff 7,592
2 Maciej Los 3,718
3 Abhinav S 3,456
4 Peter Leow 3,084


Advertise | Privacy | Mobile
Web02 | 2.8.150427.4 | Last Updated 6 Jan 2013
Copyright © CodeProject, 1999-2015
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