使用一般文本映射Using Generic-Text Mappings

Microsoft 专用Microsoft Specific

为了简化各种国际市场的代码开发,Microsoft 运行时库为许多数据类型、例程和其他对象提供了 Microsoft 专用的“一般文本”映射。To simplify code development for various international markets, the Microsoft run-time library provides Microsoft-specific "generic-text" mappings for many data types, routines, and other objects. 这些映射在 TCHAR.H 中进行定义。These mappings are defined in TCHAR.H. 可以使用这些名称映射来编写可编译全部三种字符集(ASCII (SBCS)、MBCS 或 Unicode)的通用代码:具体取决于使用 #define 语句定义的清单常量。You can use these name mappings to write generic code that can be compiled for any of the three kinds of character sets: ASCII (SBCS), MBCS, or Unicode, depending on a manifest constant you define using a #define statement. 一般文本映射是与 ANSI 不兼容的 Microsoft 扩展。Generic-text mappings are Microsoft extensions that are not ANSI compatible.

用于一般文本映射的预处理器指令Preprocessor Directives for Generic-Text Mappings

#define#define 编译的版本Compiled version 示例Example
_UNICODE Unicode(宽字符)Unicode (wide-character) _tcsrev 映射到 _wcsrev_tcsrev maps to _wcsrev
_MBCS 多字节字符Multibyte-character _tcsrev 映射到 _mbsrev_tcsrev maps to _mbsrev
无(默认值:未定义 _UNICODE_MBCSNone (the default: neither _UNICODE nor _MBCS defined) SBCS (ASCII)SBCS (ASCII) _tcsrev 映射到 strrev_tcsrev maps to strrev

例如,如果已在程序中定义了 MBCS,则在 TCHAR.H 中定义的一般文本函数 _tcsrev 映射到 mbsrev,如果已定义了 _UNICODE,则将映射到 _wcsrevFor example, the generic-text function _tcsrev, defined in TCHAR.H, maps to mbsrev if MBCS has been defined in your program, or to _wcsrev if _UNICODE has been defined. 否则,_tcsrev 将映射到 strrevOtherwise _tcsrev maps to strrev.

如果已定义了 _MBCS,则仍在 TCHAR.H 中定义的一般文本数据类型 _TCHAR 将映射到类型 char,如果定义了 _UNICODE,则映射到类型 wchar_t,如果未定义任何常量,则映射到 charThe generic-text data type _TCHAR, also defined in TCHAR.H, maps to type char if _MBCS is defined, to type wchar_t if _UNICODE is defined, and to type char if neither constant is defined. TCHAR.H 中提供了其他数据类型映射,可方便地用于编程,但 _TCHAR 是最有用的类型。Other data type mappings are provided in TCHAR.H for programming convenience, but _TCHAR is the type that is most useful.

一般文本数据类型映射Generic-Text Data Type Mappings

一般文本数据类型名称Generic-text data type name SBCS(未定义的 _UNICODE 和 _MBCS)SBCS (_UNICODE, _MBCS not defined) 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_TCHAR char char wchar_t
_TINT int int wint_t
_TSCHAR signed char signed char wchar_t
_TUCHAR unsigned char unsigned char wchar_t
_TXCHAR char unsigned char wchar_t
_T_TEXT_T or _TEXT 无效果(由预处理器删除)No effect (removed by preprocessor) 无效果(由预处理器删除)No effect (removed by preprocessor) L(将以下字符或字符串转换为其 Unicode 对应项)L (converts following character or string to its Unicode counterpart)

有关例程、变量和其他对象的一般文本映射的完整列表,请参阅一般文本映射For a complete list of generic-text mappings of routines, variables, and other objects, see Generic-Text Mappings.

以下代码片段说明了用于映射到 MBCS、Unicode 和 SBCS 模型的 _TCHAR_tcsrev 的使用方法。The following code fragments illustrate the use of _TCHAR and _tcsrev for mapping to the MBCS, Unicode, and SBCS models.

_TCHAR *RetVal, *szString;  
RetVal = _tcsrev(szString);  

如果定义了 MBCS,则预处理器将前面的片段映射到以下代码:If MBCS has been defined, the preprocessor maps the preceding fragment to the following code:

char *RetVal, *szString;  
RetVal = _mbsrev(szString);  

如果定义了 _UNICODE,则预处理器将同一片段映射到以下代码:If _UNICODE has been defined, the preprocessor maps the same fragment to the following code:

wchar_t *RetVal, *szString;  
RetVal = _wcsrev(szString);  

如果 _MBCS_UNICODE 均未定义,则预处理器将片段映射到单字节 ASCII 代码,如下所示:If neither _MBCS nor _UNICODE has been defined, the preprocessor maps the fragment to single-byte ASCII code, as follows:

char *RetVal, *szString;  
RetVal = strrev(szString);  

因此,可以编写、维护和编译单个源代码文件,来与特定于三种字符集中任何一种的例程一起运行。Thus you can write, maintain, and compile a single source code file to run with routines that are specific to any of the three kinds of character sets.

结束 Microsoft 专用END Microsoft Specific

请参阅See Also

一般文本映射 Generic-Text Mappings
数据类型映射 Data Type Mappings
常量和全局变量映射 Constant and Global Variable Mappings
例程映射 Routine Mappings
示例一般文本程序A Sample Generic-Text Program