To simplify code transmission for international use, the Microsoft Runtime Library provides Microsoft-specific "general text" ing for many data types, routines, and other objects. You can use the mappings defined in tchar. h.# DefineThe list constant defined by the statement. You can compile common code for single-byte, multi-byte, or Unicode compiling. Generally, text ing is a Microsoft extension incompatible with ANSI.
You can use tchar. h to generate single-byte, MBCS, and Unicode applications from the same source. Tchar. H is defined_ TCSMacros with a prefix, which are mapped to the appropriate ones according to the correct pre-processor DefinitionStr,_ MBSOrWCSFunction. To generate MBCS, define_ MBCSSymbol. To generate Unicode, define_ UnicodeSymbol. To generate a single-byte application, do not define anything (default ). By default, what is defined for the MFC application is_ MBCS.
Define conditions in tchar. h_ TcharData type. If you have defined_ UnicodeSymbol, then_ TcharIs definedWchar_tOtherwise, _ tchar is definedChar. ((Wchar_tIs the Basic Unicode wide character data type, which is 8-Bit SignedChar.) For international applications_ Tchar(Not in bytes)_ TCSFunction Family. For example,_ TcsncpyCopyNItems_ TcharInsteadNBytes.
Some sbcs string processing functions use (Signed)Char *Parameter, so the definition_ MBCSWill generate a compiler warning that the type does not match. There are three methods to avoid this warning, in order of efficiency:
Use the type-safe inline function thunk in tchar. h. This is the default action.
By defining_ Mb_map_directUse a direct macro in tchar. h. If you do this, you must manually match the type. This is the fastest method, but not the type-safe method.
Use the "type security" static Link Library Function thunk in tchar. h. To do this, define_ No_inliningConstant. This is the slowest method, but it is the method with the highest type security.
General pre-processor commands for text ing
# Define |
Build |
Example |
_ Unicode |
Unicode (wide character) |
_ TcsrevMap_ Wcsrev |
_ MBCS |
Multi-byte characters |
_ TcsrevMap_ Mbsrev |
None (default: neither defined_ UnicodeNot defined either._ MBCS) |
Sbcs (ASCII) |
_ TcsrevMapStrrev |
For example, if you define_ MBCS, Then the general text function defined in tchar. h_ TcsrevMap_ Mbsrev. Or if you define_ Unicode, Then_ TcsrevMap_ Wcsrev. Otherwise_ TcsrevMapStrrev. Other data type mappings are provided in tchar. h to facilitate programming._ TcharIs the most useful.
General text data type ing
Common text data type name |
_ Unicode and _ MBCS are not defined |
_ MBCS defined |
_ 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 |
_ TOr_ Text |
Invalid (removed from Preprocessor) |
Invalid (removed from Preprocessor) |
L(Convert the subsequent characters or strings to the corresponding Unicode form) |
For a complete list of general text mappings between routines, variables, and other objects, see general text ing in Runtime Library Reference.
Note:
Unicode strings may contain embedded NULL bytes, so do not use the STR function family in Unicode strings. Similarly, do not use it in the MBCS (or sbcs) string.WCSFunction Family.
The following code snippet illustrates how to map to the MBCS, Unicode, and sbcs Models_ TcharAnd_ Tcsrev.
- _ Tchar * retval, * szstring;
- Retval = _ tcsrev (szstring );
If you have defined_ MBCS, The pre-processor maps this segment to the following code:
char *RetVal, *szString;RetVal = _mbsrev(szString);
If you have defined_ Unicode, The pre-processor maps this segment to the following code:
wchar_t *RetVal, *szString;RetVal = _wcsrev(szString);
If neither is defined_ MBCSNot defined either._ Unicode, The pre-processor maps this segment to the single-byte ASCII code:
char *RetVal, *szString;RetVal = strrev(szString);
Therefore, you can compile, maintain, and compile a single source code file that runs together with any of the three character sets.