How to Set VC ++ 6.0 to support Unicode Programming

Source: Internet
Author: User

VC ++ 6.0 supports Unicode programming, but the default value is ANSI. Therefore, developers can easily write Unicode-Supported Applications by slightly changing the coding habits. Using VC ++ 6.0 for Unicode 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 are aware of Unicode encoding.

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.

Unicode programming example

Step 1:
Open VC ++ 6.0 and create 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 and Unicode encoding test"); m_disp = str1; updatedata (false);} Add static text box idc_disp, 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:
Change to the unicode.exe environment to compile the project and generate unicode.exe. Run the unicode.exe program again and click the Unicode button. See the advantages of Unicode encoding.

This article from: It Knowledge Network (http://www.itwis.com) detailed source reference: http://www.itwis.com/html/c/ctool/20091127/7024.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.