VC++6.0Unicode programming is supported, but the default value is ANSI. developers can easily write UNICODE-Supported Applications by slightly changing the coding habits.
UseVC++6.0Unicode programming mainly involves the following tasks:
1. Add UNICODE and _ UNICODE preprocessing options for the project.
Specific steps: open [project]-> [settings…] In the "pre-processing program definition" in the C/C ++ label dialog box, remove _ MBCS and add _ UNICODE and UNICODE. (Note that separate them with commas ).
Before UNICODE and _ UNICODE are defined, all functions and types Use the ANSI version by default. After UNICODE and _ UNICODE are defined, all the MFC classes and Windows APIs have been changed to the wide-byte version.
2. Set the program entry point
Because the MFC application has a program entry point dedicated to Unicode, we need to set the entry point. Otherwise, a connection error occurs.
To set an entry point, open [project]-> [set…]. Dialog box, fill in wWinMainCRTStartup in the Entry Point of the Output category on the Link Page.
3. Use ANSI/Unicode Universal Data Types
Microsoft provides some common data types compatible with ANSI and Unicode. Our most common data types include _ T, TCHAR, LPTSTR, and LPCTSTR.
By the way, LPCTSTR and const TCHAR * are exactly the same. Here, L indicates the long pointer, which is left behind for compatibility with Windows 3.1 and other 16-bit operating systems. In Win32 and other 32-bit operating systems, the long pointer, near pointer, and far modifier are both intended for compatibility and have no practical significance. P (pointer) indicates a pointer; C (const) indicates a constant; T (_ T macro) indicates compatibility with ANSI and Unicode, STR (string) this variable is a string. In summary, we can see that LPCTSTR indicates a string that points to a fixed address and can change the semantics according to some macro definitions. For example:
TCHAR * szText = _ T ("Hello !"); TCHAR szText [] = _ T ("I Love You"); LPCTSTR lpszText = _ T ("Hello everyone !");
It is best to change the parameters in the function, for example:
MessageBox (_ T ("hello "));
In fact, in the preceding statement, even if you do not add a _ T macro, The MessageBox function will automatically forcibly convert the "hello" string. However, we recommend that you use the _ T macro to indicate that you haveUnicode encodingConsciousness.
4. Modifying string operations
Some string operation functions need to obtain the number of characters (sizeof (szBuffer)/sizeof (TCHAR) of the string, while other functions may need to obtain the number of bytes of the string sizeof (szBuffer ). You should pay attention to this problem and carefully analyze the string operation functions to confirm the correct results.
ANSI operation functions start with str, such as strcpy (), strcat (), strlen ();
Unicode operation functions start with the wcs, such as wcscpy, wcscpy (), and wcslen ();
ANSI/Unicode operation functions start with _ tcs _ tcscpy (C Runtime Library );
ANSI/Unicode operation functions start with lstr lstrcpy (Windows function );
Considering compatibility between ANSI and Unicode, we need to use a universal string operation function starting with _ TCS or starting with lstr.
Vi. Unicode programming example
Step 1:
OpenVC++6.0Create a project Unicode Based on the dialog box. In the main dialog box, add a button control to idd_unicode_dialog, double-click the control, and add the response function of the control:
Void cunicodedlg: onbuttonunicode () {tchar * str1 = _ T ("ANSI andUNICODE encodingTest "); m_disp = str1; updatedata (false );}
Add the static text box idc_disp and use classwizard to add the cstring type variable m_disp to the control. Compile the project using the ansi.pdf environment and generate unicode.exe.
Step 2: modify the system internal code
Open the control panel and click the date, time, language, and Region settings option, in the "date, time, language, and Region Settings" window, click the "region and language options" option to bring up the "region and language options" dialog box. In the dialog box, click the "advanced" tab, change the "language of a non-Unicode program" option to "Japanese", and click the "application" button,
In the displayed dialog box, click "yes" and restart the computer to make the settings take effect.
Run the unicode.exe program and click "button1". The static text box is garbled.
Step 3:
ChangeUnicode encodingCompile the project and generate unicode.exe. Run the unicode.exe program again and click the Unicode button. SeeUnicode encodingThe advantage.
Operation:
What is ANSI and UNICODE? In fact, these are two different encoding methods. ANSI adopts 8 bits, while UNICODE uses 16 bits. 8-bit ANSI encoding can only represent 256 characters, indicating that 26 English letters are more than enough, but it is not enough to represent non-Western characters with thousands of characters, such as Chinese characters, Korean and Japanese, in this way, the UNICODE standard is introduced.
In software development, especially some functions related to string processing in C language, ANSI and UNICODE are used for distinguishing. How can we define ANSI and UNICODE characters, how to use it? How can we convert ANSI and UNICODE?
I. Definition:
ANSI:CharStr [1024]; available string processing functions: strcpy (), strcat (), strlen (), and so on. UNICODE:Wchar_tStr [1024]; string processing functions available
Ii. Available functions:
ANSI: that isChar,Available string processing functions:Strcat (), strcpy (), strlen ()AndStrIndicates the function of hitting the header.
UNICODE: that isWchar_tAvailable string processing functions:Wcscat (), wcscpy (), wcslen ()AndWcsIndicates the function of hitting the header.
Iii. System Support
Windows 98: only ANSI is supported.
Windows 2 k: supports both ANSI and UNICODE.
Windows CE: Only UNICODE is supported.
Description
1. Only UNICODE is supported in COM.
2. in Windows 2000, the entire OS system is Unicode-based. Therefore, using ANSI in Windows 2000 requires a price. Although no conversion is required for encoding, this conversion is hidden, CPU and memory are occupied by system resources ).
3. Unicode must be used in Windows 98. You need to manually switch the encoding.
Iii. How to differentiate:
In our software development, we often need to support ANSI and Unicode. It is impossible to re-change the string type and use the string operation functions when type conversion is required. To this end, the standard C Runtime Library and windows provide macro-defined methods.
_ Unicode macros (with underscores) are provided in the C language, and Unicode macros (without underscores) are provided in windows. If _ Unicode macros and Unicode macros are specified, the system automatically switches to the Unicode version. Otherwise, the system compiles and runs in ANSI mode.
Only macros are defined and cannot be automatically converted. It also requires support for a series of character definitions.
1. tchar
If a unicode macro is defined, tchar is defined as wchar_t.
Typedef wchar_t tchar;
Otherwise, tchar is defined as char.
Typedef char tchar;
2. lptstr
If a unicode macro is defined, lptstr is defined as lpwstr.
Typedef lptstr lpwstr;
Otherwise, tchar is defined as char.
Typedef lptstr lpstr;
Note: _ TEXT ("MyStr") must be used to support automatic conversion of string constants.