Click here to Skip to main content
15,881,882 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: XP/Vista Styles Pin
The_Judgement21-Mar-09 11:14
The_Judgement21-Mar-09 11:14 
GeneralRe: XP/Vista Styles Pin
Code-o-mat21-Mar-09 11:27
Code-o-mat21-Mar-09 11:27 
GeneralRe: XP/Vista Styles Pin
The_Judgement21-Mar-09 12:41
The_Judgement21-Mar-09 12:41 
GeneralRe: XP/Vista Styles Pin
Code-o-mat21-Mar-09 23:04
Code-o-mat21-Mar-09 23:04 
GeneralRe: XP/Vista Styles [modified] Pin
The_Judgement22-Mar-09 11:28
The_Judgement22-Mar-09 11:28 
QuestionUnicode.. Pin
p_196021-Mar-09 7:36
p_196021-Mar-09 7:36 
AnswerRe: Unicode.. Pin
CPallini21-Mar-09 7:48
mveCPallini21-Mar-09 7:48 
AnswerRe: Unicode.. Pin
Jörgen Sigvardsson21-Mar-09 22:43
Jörgen Sigvardsson21-Mar-09 22:43 
There's two basic defines in the windows header files: CHAR and WCHAR. CHAR is defined as an ordinary char, and WCHAR is defined as a 16 bit WORD. CHAR is used for ANSI/MBCS (Multibyte character sets - pre-unicode solutions for "exotic" languages). WCHAR is used for UTF16/Unicode.

To assign an ANSI character, you do as you are used to. CHAR c = 'A'; To assign a Unicode character, you do it like this: CHAR u = L'A'; The L prefix tells the compiler that the character is a Unicode character. The same pattern applies for strings.

Strings are defined like this in the header files:
typedef LPSTR char*;
typedef LPWSTR char*;


Here you can also see a pattern emerge in the string defines. Whenever you see a W in it somewhere, it's a Unicode string. If it's got a C in it (like LPCSTR), it's a const string.

To assign an Ansi string, you do something like this: LPCSTR str = "Hello world!"; Similarly in Unicode: LPCWSTR str = L"Hello world!";

With me so far?

Right. There's a third define in the windows header file which is a bit special. It's called TCHAR. TCHAR is defined like this:
#ifdef _UNICODE
typedef TCHAR WCHAR;
#else
typedef TCHAR CHAR;
#endif


The meaning of TCHAR is dependent on the _UNICODE symbol. When you specify in the project properties that you want to be using Unicode, the compiler will define this symbol for you.

Anyway, this is ideal if you want to support both ANSI and Unicode builds of the same application. There's just one problem though. This line: LPCTSTR str = "Hello world!"; will only compile in ANSI builds, but not in Unicode build. This is because the string in the example is of type LPCSTR (const char*), while str is of type LPCWSTR (const WORD*)1. The remedy are the macros _T() and TEXT(). _T and TEXT are equivalent. I prefer typing _T because it's less typing.
_T (and TEXT) are define like this:
#ifdef _UNICODE
#define _T(x) L##x
#else
#define _T(x) x
#endif


So, the previous code LPCTSTR str = "Hello world!" should be rewritten as LPCTSTR str = _T("Hello world!");. This will be expanded to the correct version, depending on wheter you've specified _UNICODE or not.


You will find that the Microsoft APIs have support for both Ansi and Unicode in parallel. In most cases (such as the Win32 API), the header files will define functions like this:
BOOL CreateWindowA(...);
BOOL CreateWindowW(...);
#ifdef _UNICODE
#define CreateWindow CreateWindowW
#else
#define CreateWindow CreateWindowA
#endif


This way, as long as you are consistent in the use of either Ansi or Unicode strings, you won't have to adapt much. Other APIs such as the C runtime library are a bit different. There you have the good old functions such as strlen left intact - they only work on ANSI strings. In addition to strlen, there is also wcslen (Unicode), and _tcslen. the _t-functions are defined differently depending on the _UNICODE symbol, much like the Win32 functions mentioned earlier.

The quick answer to your question is: Use _T() for all your strings and characters, and use _t-functions from the C runtime library, and you will be pretty much fine. Do remember though, that when you write text to sockets, files or via some other medium, that you might have to reencode your Unicode strings. That, however, is a bit larger topic...


Personally, I think it's best to just pick Unicode from scratch. The world is becoming more and more globalized, and different cultures meet more often, so in a business application, odds are that there will be "trouble" down the line. Windows is also a bit faster at handling Unicode strings, because starting with NT4 (or was it Windows 2000? can't remember), the Ansi-versions of the API functions are just shallow wrappers that converts the strings into Unicode and pass them onto the Unicode implementations.


1 The compiler in these errors complain that the types const char* isn't compatible with the type const unsigned short*.



Questionconvert Pin
durban221-Mar-09 5:07
durban221-Mar-09 5:07 
QuestionRe: convert Pin
CPallini21-Mar-09 5:13
mveCPallini21-Mar-09 5:13 
AnswerRe: convert Pin
Dominik Reichl21-Mar-09 5:14
Dominik Reichl21-Mar-09 5:14 
AnswerRe: convert Pin
Akt_4_U21-Mar-09 5:52
Akt_4_U21-Mar-09 5:52 
AnswerRe: convert Pin
Eytukan21-Mar-09 22:02
Eytukan21-Mar-09 22:02 
Questionhow to write list control data in text file in MFC Pin
amit14aug21-Mar-09 4:38
amit14aug21-Mar-09 4:38 
AnswerRe: how to write list control data in text file in MFC Pin
Rajesh R Subramanian21-Mar-09 4:41
professionalRajesh R Subramanian21-Mar-09 4:41 
Questionpointer-to-array Pin
korte2521-Mar-09 3:49
korte2521-Mar-09 3:49 
AnswerRe: pointer-to-array Pin
CPallini21-Mar-09 4:09
mveCPallini21-Mar-09 4:09 
GeneralRe: pointer-to-array Pin
korte2521-Mar-09 6:09
korte2521-Mar-09 6:09 
GeneralRe: pointer-to-array Pin
CPallini21-Mar-09 6:25
mveCPallini21-Mar-09 6:25 
GeneralRe: pointer-to-array Pin
korte2521-Mar-09 6:57
korte2521-Mar-09 6:57 
GeneralRe: pointer-to-array Pin
CPallini21-Mar-09 7:41
mveCPallini21-Mar-09 7:41 
GeneralRe: pointer-to-array Pin
korte2521-Mar-09 7:44
korte2521-Mar-09 7:44 
GeneralRe: pointer-to-array Pin
CPallini21-Mar-09 7:48
mveCPallini21-Mar-09 7:48 
AnswerRe: pointer-to-array Pin
Stuart Dootson21-Mar-09 12:29
professionalStuart Dootson21-Mar-09 12:29 
GeneralRe: pointer-to-array Pin
korte2522-Mar-09 2:30
korte2522-Mar-09 2:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.