Show: Delphi C++
Display Preferences

Enabling C++ Applications for Unicode

From RAD Studio
Jump to: navigation, search

Go Up to Unicode for C++ Index


Because the default string is Unicode, any C++ code that uses RAD Studio frameworks and libraries 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 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 Delphi users do not encounter. These issues are due to the fact that the C++ RTL and the Windows API are narrow by default while RAD Studio frameworks and libraries use Unicode strings. Some of these issues are discussed here.

Using RAD Studio Frameworks and Libraries and the C++ RTL

RAD Studio frameworks and libraries use 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 C++ RTL functions in an application that use RAD Studio frameworks and libraries. To use the C++ RTL in an 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 C++ 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 RAD Studio frameworks and libraries.

To use the Windows API in a C++ application that does not use RAD Studio frameworks and libraries, 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 functions of RAD Studio frameworks and libraries that take PChar as you did before. You can pass the constant to functions of RAD Studio frameworks and libraries 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 Delphi provides:

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

See Also

Personal tools
RAD Studio 10.2 Tokyo
In other languages
Previous Versions
Assistance