• What is the difference between _tmain() and main() in C++?


    _tmain does not exist in C++. main does.

    _tmain is a Microsoft extension.

    main is, according to the C++ standard, the program's entry point. It has one of these two signatures:

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

    Microsoft has added a wmain which replaces the second signature with this:

    int wmain(int argc,wchar_t* argv[]);

    And then, to make it easier to switch between Unicode (UTF-16) and their multibyte character set, they've defined _tmain which, if Unicode is enabled, is compiled as wmain, and otherwise as main.

    As for the second part of your question, the first part of the puzzle is that your main function is wrong. wmain should take a wchar_t argument, not char. Since the compiler doesn't enforce this for the main function, you get a program where an array of wchar_t strings are passed to the main function, which interprets them as char strings.

    Now, in UTF-16, the character set used by Windows when Unicode is enabled, all the ASCII characters are represented as the pair of bytes \0 followed by the ASCII value.

    And since the x86 CPU is little-endian, the order of these bytes are swapped, so that the ASCII value comes first, then followed by a null byte.

    And in a char string, how is the string usually terminated? Yep, by a null byte. So your program sees a bunch of strings, each one byte long.

    In general, you have three options when doing Windows programming:

    • Explicitly use Unicode (call wmain, and for every Windows API function which takes char-related arguments, call the -W version of the function. Instead of CreateWindow, call CreateWindowW). And instead of using char use wchar_t, and so on
    • Explicitly disable Unicode. Call main, and CreateWindowA, and use char for strings.
    • Allow both. (call _tmain, and CreateWindow, which resolve to main/_tmain and CreateWindowA/CreateWindowW), and use TCHAR instead of char/wchar_t.

    The same applies to the string types defined by windows.h: LPCTSTR resolves to either LPCSTR or LPCWSTR, and for every other type that includes char or wchar_t, a -T- version always exists which can be used instead.

    Note that all of this is Microsoft specific. TCHAR is not a standard C++ type, it is a macro defined in windows.h. wmain and _tmain are also defined by Microsoft only.

  • 相关阅读:
    Jmeter接口自动化-5-提取JSON响应中数组的长度
    Redis系列讲解
    jQuery.Autocomplete实现自动完成功能(详解)
    js中获得当前时间是年份和月份
    搭建SSH框架所需Jar包及其解释
    JS中把字符串转成JSON对象的方法
    JBPM数据库表说明
    java的System.getProperty()方法可以获取的值
    Mybatis3.2.3+mysql第一个例子(入门)
    多线程学习
  • 原文地址:https://www.cnblogs.com/lihaozy/p/2585148.html
Copyright © 2020-2023  润新知