Enabling C++ Applications for Unicode

From Appmethod Topics
Jump to: navigation, search

Go Up to Unicode for C++ Index

Because the default string is Unicode, any C++ code that uses the VCL must be Unicode-aware. You can ensure that your code correctly handles both narrow and wide strings by using the _TCHAR Mapping option on the Project > Options > C++ (Shared Options) dialog box.

Note: New C++ applications that use the VCL (including C++ console applications that use the VCL) now automatically set the _TCHAR mapping option to wchar_t and contain a _tmain(...) entry point, which floats to main. C++ applications that have _TCHAR mapping set automatically use the correct Floating Functions.

Unicode-Related Issues for C++

C++ has a unique set of Unicode-related issues that Object Pascal users do not encounter. These issues are due to the fact that the C++ RTL and the Windows API are narrow by default while VCL is now Wide. Some of these issues are discussed here.

Using the VCL and C++ RTL

The VCL is wide (using Unicode), while the C++ RTL is narrow by default but contains routines for both wide and narrow strings (see Floating Functions). Thus, you need to use the wide versions of RTL functions in an application that uses the VCL. To use the C++ RTL in a VCL application (or any application that requires wide strings), you need to do the following:

  • Set _TCHAR Mapping to wchar_t and use _TCHAR in your code.
  • Use the "floating" version of the RTL members, such as _tcscpy instead of strcpy. See the list of Floating Functions.

Using Windows API

The Windows API is typically narrow by default. The _TCHAR mapping option helps tremendously here, but the option is OFF (set to char) by default for C++ applications that do not use the VCL.

To use the Windows API in a C++ application that does not use the VCL, you must explicitly set the _TCHAR maps to option to wchar_t on the Project > Options > C++ (Shared Options) dialog box.

Passing String Constants

String constants, such as "string constant", are still narrow (char*), so you cannot pass them to VCL functions that take PChar as you did before. You can pass the constant to VCL functions with a PChar parameter if you prefix the constant with L, as in:

L"string constant"

This conversion is automatically done for you when you set _TCHAR mapping to wchar_t and use the _TEXT or _T macros, as described in _TCHAR Mapping.

Setting the CodePage

You can set the codepage for AnsiString types with AnsiStringT<codepage>.

The same predefined types are available that Object Pascal provides:

Note: The UTF8 encoding can be specified by using the UTF8 codepage and encoding the strings between the API-RTL and the VCL calls. See UTF-8 Conversion Routines.

See Also