Wide string handling issues in Windows

Source: Internet
Author: User
Tags function prototype

When using VC to do development, for some strings, will often call General _t, _t, T (), in fact, these things are related to Unicode. For example, AfxMessageBox (_t ("error! Fail to connect the database! ")); Here a _t () is used, and no _t () is used to compile the error.

The Windows operating system uses Unicode as the default text encoding format. Unicode defines support for character encodings that are greater than 8 bits. Windows uses the UTF-16 format, known as wide character encoding, which UTF-16 encoded in 16-bit unsigned integers, with two bytes per character. Many Windows functions define Two entry points : TheUnicode version entry function ends with the W character , which is for wide characters, and theANSI character entry point function ends with a . For example, there are CREATEMUTEXW () and Createmutexa () for CreateMutex (). The appropriate function call is made at compile time based on whether Unicode is defined. Windows uses wide characters internally, and the ANSI entry point is actually just wrapping it outside the wide-character version function call, making the appropriate string conversions.

The 16-bit character type is wchar_t, which can be substituted for the char type. WCHAR is the equivalent of defining a wide character, and in the source code the string is interpreted as a 8-bit ANSI character, so it needs to be specified as a wide character and can be specified using the L specifier or the macro text ().

wchar_t str1[]=l "some text"; WCHAR st2[]=text ("MoreText");

In the case where Unicode is defined, the text () macro is useful for converting a string to a wide character format, and it keeps the string 8-bit ANSI text formatting if Unicode is not defined. The TCHAR type is similar, when Unicode is defined, resolves to wchar_t, otherwise resolves to char.

Some functions also use macros that are parsed differently, depending on whether Unicode is defined . For example, the main function _tmain () resolves to wmain () in the case of defining Unicode, otherwise resolves to main (), _tprintf () resolves to wprintf () when Unicode is defined, Otherwise parse into printf ().

In fact, when using _tmain, using goto definition can be seen, in stdafx.h there is such a line, #include <tchar.h>, so use _tmain () must use the #include< Tchar.h>, find _tmain macro definition in header file <tchar.h>,

#define _tmain Main

After pre-compiling, the _tmain becomes main. Main () is a function entry for standard C + +. Standard C + + program entry point function, the default character encoding format for the ANSI function prototype is:

int main ();

int main (int argc, char* argv[]);

_tmain () is a program entry point function that Windows provides for automatic conversion of the Unicode character set and the ANSI character set, with its prototype int _tmain (int argc, TCHAR *argv[]).

When the program's current character set is Unicode, int _tmain (int argc, TCHAR *argv[]) is translated into

int wmain (int argc, wchar_t *argv[]), when the program's current character set is ANSI, int_tmain (int argc, TCHAR *argv[]) is translated into int main (int argc, char *argv [])。

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Wide string handling issues in Windows

Related Article

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.