将 TCHAR.H 数据类型用于 _MBCSUsing TCHAR.H Data Types with _MBCS

Microsoft 专用Microsoft Specific

如一般文本例程映射表所示(参阅一般文本映射),定义清单常量 _MBCS 后,给定的一般文本例程会映射到以下其中一种例程中:As the table of generic-text routine mappings indicates (see Generic-Text Mappings), when the manifest constant _MBCS is defined, a given generic-text routine maps to one of the following kinds of routines:

  • 可恰当处理多字节字节、字符和字符串的 SBCS 例程。An SBCS routine that handles multibyte bytes, characters, and strings appropriately. 在这种情况下,字符串参数类型应为 char*In this case, the string arguments are expected to be of type char*. 例如,_tprintf 映射到 printfprintf 的字符串参数类型为 char*For example, _tprintf maps to printf; the string arguments to printf are of type char*. 如果将 _TCHAR 一般文本数据类型用于字符串类型,printf 的形参和实参类型将匹配,因为 _TCHAR* 映射到 char*If you use the _TCHAR generic-text data type for your string types, the formal and actual parameter types for printf match because _TCHAR* maps to char*.

  • MBCS 专用例程。An MBCS-specific routine. 在这种情况下,字符串参数类型应为 unsigned char*In this case, the string arguments are expected to be of type unsigned char*. 例如,_tcsrev 映射到 _mbsrev,这应该并且会返回的类型为 unsigned char* 的字符串。For example, _tcsrev maps to _mbsrev, which expects and returns a string of type unsigned char*. 同样,如果将 _TCHAR 一般文本数据类型用于字符串类型,则会出现潜在的类型冲突,因为 _TCHAR 映射到类型 charAgain, if you use the _TCHAR generic-text data type for your string types, there is a potential type conflict because _TCHAR maps to type char.

    以下是防止此类型冲突(以及可能导致的 C 编译器警告或 C++ 编译器错误)的三个解决方案:Following are three solutions for preventing this type conflict (and the C compiler warnings or C++ compiler errors that would result):

  • 使用默认行为。Use the default behavior. TCHAR.H 为运行时库中的例程提供一般文本例程原型,如下例所示。TCHAR.H provides generic-text routine prototypes for routines in the run-time libraries, as in the following example.

    char *_tcsrev(char *);  
    

    默认情况下,_tcsrev 的原型通过 LIBC.LIB 中的 thunk 映射到 _mbsrevIn the default case, the prototype for _tcsrev maps to _mbsrev through a thunk in LIBC.LIB. 这将 _mbsrev 传入参数和传出返回值的类型从 _TCHAR *(例如 char *)更改为 unsigned char *This changes the types of the _mbsrev incoming parameters and outgoing return value from _TCHAR * (such as char *) to unsigned char *. 这种方法可以确保在使用 _TCHAR 时进行类型匹配,但该过程由于函数调用产生系统开销而相对较慢。This method ensures type matching when you are using _TCHAR, but it is relatively slow because of the function call overhead.

  • 通过在代码中包含以下预处理器语句来使用函数内联。Use function inlining by incorporating the following preprocessor statement in your code.

    #define _USE_INLINING  
    

    此方法会导致 TCHAR.H 中提供的内联函数 thunk 将一般文本例程直接映射到相应的 MBCS 例程。This method causes an inline function thunk, provided in TCHAR.H, to map the generic-text routine directly to the appropriate MBCS routine. 摘自 TCHAR.H 的以下代码提供了一个如何完成此操作的示例。The following code excerpt from TCHAR.H provides an example of how this is done.

    __inline char *_tcsrev(char *_s1)  
    {return (char *)_mbsrev((unsigned char *)_s1);}  
    

    如果可以使用内联,这是最好的解决方案,因为它保证了类型匹配,并且不会产生额外的时间成本。If you can use inlining, this is the best solution, because it guarantees type matching and has no additional time cost.

  • 通过在代码中包含以下预处理器语句来使用“直接映射”。Use "direct mapping" by incorporating the following preprocessor statement in your code.

    #define _MB_MAP_DIRECT  
    

    如果不想使用默认行为或无法使用内联,则此方法提供了一种快速替代方法。This approach provides a fast alternative if you do not want to use the default behavior or cannot use inlining. 它使一般文本例程由宏直接映射到例程的 MBCS 版本,如以下 TCHAR.H 中的示例所示。It causes the generic-text routine to be mapped by a macro directly to the MBCS version of the routine, as in the following example from TCHAR.H.

    #define _tcschr _mbschr  
    

    在采用此方法时,必须小心以确保将适当的数据类型用于字符串参数和字符串返回值。When you take this approach, you must be careful to ensure that appropriate data types are used for string arguments and string return values. 可以使用类型强制转换来确保类型匹配正确,也可以使用 _TXCHAR 一般文本数据类型。You can use type casting to ensure proper type matching or you can use the _TXCHAR generic-text data type. _TXCHAR 映射到 SBCS 代码中的类型 char,但映射到 MBCS 代码中的类型 unsigned char_TXCHAR maps to type char in SBCS code but maps to type unsigned char in MBCS code. 有关一般文本宏的详细信息,请参阅一般文本映射For more information about generic-text macros, see Generic-Text Mappings.

    结束 Microsoft 专用END Microsoft Specific

另请参阅See Also

国际化 Internationalization
按类别分的运行时例程Run-Time Routines by Category