Unicode and _ Unicode definitions in Windows Programming

Source: Internet
Author: User
First go to an article Article :

 

I also encountered this problem when I was a beginner at windows SDK programming. I believe many beginners of Windows programming have also encountered this problem. Later, I gradually understood this problem, but sometimes I am not quite clear about it when asked by others. Today, I would like to take this opportunity to organize my own ideas and explain this question in detail in the following article, hoping to help my friends who have this question.

Unicode and _ Unicode are not "defined" in the header file,ProgramThis is defined by one of the following methods:
1. UseCodeAs defined above;
2. specified in the compilation options. For example, in VC:

Project-> Settings-> C/C ++-> category-> Preprocessor Definitions

Here is the pre-processing command. There are some other common constants such as Win32, _ debug, ndebug, and _ windows.

Therefore, you cannot find the definition in the header file (note that the definition is not used, and there are many places to use it ).

However, these two symbols have specific meanings for Windows programs. That is to say, you cannot define Unicode as Unicode, Unicode, useunicode... otherwise, they will be useless. Because in many header files, these two symbols are used to judge:

1. Use that encoding method, namely Unicode and single-byte;
2. Meanings of specific types in windows;
3. Select different character processing functions based on Unicode and single-byte encoding.

For example, in the case of _ Unicode, you can use tchar. h, which is used to parse whether the tchar type is wide character or single byte character, and whether the processing result of some string macros is wide character or single byte, for example:

# Ifdef _ Unicode
Typedef wchar_t tchar;
# Else
Typedef char tchar;
# Endif

Macro defined in a similar way:
_ Text ("Hello! "), _ T (" Hello! "), Text (" Hello! "), L (" Hello! ")

The macro above indicates that if it is Unicode, It is a wide character; otherwise, it is processed as a traditional single-byte ASCII character.

Unicode is used in many header files like _ Unicode to parse whether the tchar type is a wide character or a single byte character, such as winnt. h:

# Ifdef Unicode
Typedef wchar tchar, * ptchar;
Typedef lpwstr lptch, ptch, ptstr, lptstr;
Typedef maid;
# Else
Typedef char tchar, * ptchar;
Typedef lpstr lptch, ptch, ptstr, lptstr;
Typedef maid;
# Endif

It is also used to select character processing functions, such as in winuser. h:

# Ifdef Unicode
# Define MessageBox messageboxw
# Else
# Define MessageBox messageboxa
# Endif

In this case, we only use one type (such as tchar) and one group of functions (such as MessageBox) to easily process two encoding programs, instead of using conditions to determine whether Char or wchar_t should be used, messageboxa or messageboxw should be used. These details have already been taken into consideration in header files such as Windows. H. All we need to do is to define two symbols when Unicode is required.

On the other hand, windows. h # include and WinNT. h. winuser. H and many other header files are included, and there are also repeated crossover (of course, the Conditional compilation and filtering are used) in many places. Therefore, both symbols (Unicode and _ Unicode) must be defined, otherwise, it will be difficult to predict the result, as defined in the previous definition.

 

 

Conclusion: With these features, you can use macros (such as: _ T, lptstr, and lpctrstr with the t sign) to convert between ASCII and Unicode. The program is highly portable.

 

 

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.