_TCHAR Mapping

From RAD Studio
Jump to: navigation, search

Go Up to Unicode for C++ Index


The _TCHAR data type is declared conditionally in the tchar.h header file. _TCHAR is defined as a typedef (alias) that maps either to char or to wchar_t. When you want to write portable code, you should use _TCHAR instead of hard coding char or wchar_t data types.


Contents

IDE Option "_TCHAR maps to"

The IDE option _TCHAR maps to controls the floating definition of _TCHAR to char or wchar_t.

"_TCHAR maps to" Values:

char

_TCHAR substitutes char. This option does not float to wide definitions of standard library and API functions.

wchar_t

_TCHAR substitutes wchar_t. This option defines the UNICODE and _UNICODE. It floats to the wide definitions of standard library and API functions.


To set the _TCHAR maps to option, go to the Project > Options > Directories and Conditionals dialog box.

Use "_TCHAR maps to" with wchar_t for VCL

The VCL is implemented in Unicode and always expects Unicode. To use the VCL, you should set _TCHAR maps to to wchar_t. For example, the following code does not compile unless you have set _TCHAR maps to to wchar_t:

TResourceStream* res =  new
    TResourceStream(HInstance, ResourceId, RT_RCDATA);

If _TCHAR is char, RT_RCDATA maps to a char*, but VCL expects wchar_t*.

Code Changes Required for Using "_TCHAR maps to" with wchar_t

Before you can set the _TCHAR maps to option to wchar_t, your project must have an entry point called either _tmain or _tWinMain. New projects created with C++Builder have these entry points by default, but for imported projects you might need to add these entry points by hand. You must also include the tchar.h header file, which contains the floating definitions and the entry points that you need. For a list of the floating functions contained in tchar.h, see Floating Functions.

Use _TEXT Macro before Text Literals

To ensure that character and string literals float properly to ANSI or Unicode, use either the _TEXT or _T macros. For example:

::MessageBox(0, _TEXT("The message"), _TEXT("The caption"), MB_OK);

With _UNICODE defined, the _TEXT (or _T) macro translates a literal string (character) to the L-prefixed form; otherwise, _TEXT translates the literal without the L prefix. For example, if you define _UNICODE, then

_TEXT("The message")

translates to the L-prefixed form:

L"The message"

Otherwise, this macro translates without the L prefix:

"The message"

See Also

Personal tools
Newest Version: XE
In other languages