zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    php文件hash算法,秒传原理
    pytest-2:allure 的安装、使用
    python操作数据库(Mysql)
    python中OS常用方法
    Selenium2+python-unittest之装饰器(@classmethod)
    selenium中嵌套iframe的切换
    selenium3驱动IE浏览器设置
    selenium测试报告(含通过率统计图和失败截图)
    python3打包成exe---pyinstaller方法
    Python2图像文本识别
  • 原文地址:https://www.cnblogs.com/lihaozy/p/2585148.html
Copyright © 2011-2022 走看看