RAD Studio |  |

TCHAR Mapping
Use _TCHAR maps to wchar_t for VCL | Use _TEXT function for Constants Strings | Code Changes Are Required for Using _TCHAR maps to wchar_t | See Also
Collapse All

TCHAR, which is declared in the header file tchar.h, is 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.

The TCHAR maps to option controls the floating definition of _TCHAR. Your choices are:

_TCHAR Mapping Options

char | _TCHAR does not float to a wide definition.
wchar_t | Sets the UNICODE and _UNICODE defines. _TCHAR floats to the wide definitions of standard library and API functions.

To set the TCHAR maps to option, go to the ProjectOptionsDirectories and Conditionals dialog box.

Use _TCHAR maps to 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 to wchar_t:

Copy Code

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

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

Use _TEXT function for Constants Strings

To ensure that constants strings float properly to ANSI or Unicode, use either the function _TEXT or _T. For example:

Copy Code

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

Code Changes Are Required for Using _TCHAR maps to wchar_t

Before you can set the _TCHAR option to wchar_t, your project must have an entry point called either _tmain or _tWinMain. New projects created with C++Builder 2009 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.

See Also

Floating Functions

Unicode in RAD Studio

Enabling Unicode in Your Applications

Enabling C++ Applications for Unicode

Directories and Conditionals Dialog Box

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!