C++ 中的 _tmain() 和 main() 有什么区别?

2022-01-21 00:00:00 unicode arguments c++

如果我使用以下 main() 方法运行我的 C++ 应用程序,一切正常:

If I run my C++ application with the following main() method everything is OK:

int main(int argc, char *argv[]) 
{
   cout << "There are " << argc << " arguments:" << endl;

   // Loop through each argument and print its number and value
   for (int i=0; i<argc; i++)
      cout << i << " " << argv[i] << endl;

   return 0;
}

我得到了我的期望并且我的论点被打印出来了.

I get what I expect and my arguments are printed out.

但是,如果我使用 _tmain:

However, if I use _tmain:

int _tmain(int argc, char *argv[]) 
{
   cout << "There are " << argc << " arguments:" << endl;

   // Loop through each argument and print its number and value
   for (int i=0; i<argc; i++)
      cout << i << " " << argv[i] << endl;

   return 0;
}

它只显示每个参数的第一个字符.

It just displays the first character of each argument.

造成这种情况的区别是什么?

What is the difference causing this?

推荐答案

_tmain 在 C++ 中不存在.main 会.

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

_tmain 是 Microsoft 扩展.

_tmain is a Microsoft extension.

main 根据 C++ 标准,是程序的入口点.它具有以下两个签名之一:

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 添加了一个 wmain,将第二个签名替换为:

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

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

然后,为了更容易在 Unicode (UTF-16) 和它们的多字节字符集之间切换,他们定义了 _tmain,如果启用了 Unicode,则将其编译为 wmain,否则为 main.

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.

至于你问题的第二部分,谜题的第一部分是你的主要功能是错误的.wmain 应该采用 wchar_t 参数,而不是 char.由于编译器不会对 main 函数强制执行此操作,因此您会得到一个程序,其中将 wchar_t 字符串数组传递给 main 函数,它将它们解释为 char 字符串.

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.

现在,在启用 Unicode 时 Windows 使用的字符集 UTF-16 中,所有 ASCII 字符都表示为字节对 后跟 ASCII 值.

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 followed by the ASCII value.

由于 x86 CPU 是 little-endian,因此这些字节的顺序被交换,因此 ASCII 值在前,然后是空字节.

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.

在 char 字符串中,字符串通常如何终止?是的,通过一个空字节.所以你的程序会看到一堆字符串,每个字符串长一个字节.

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.

一般来说,在进行 Windows 编程时,您有三种选择:

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

  • 显式使用 Unicode(调用 wmain,对于每个接受 char 相关参数的 Windows API 函数,调用该函数的 -W 版本.而不是 CreateWindow,调用 CreateWindowW).而不是使用 char 而是使用 wchar_t,依此类推
  • 明确禁用 Unicode.调用 main 和 CreateWindowA,并将 char 用于字符串.
  • 两者都允许.(调用 _tmain 和 CreateWindow,解析为 main/_tmain 和 CreateWindowA/CreateWindowW),并使用 TCHAR 代替 char/wchar_t.
  • 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.

同样适用于 windows.h 定义的字符串类型:LPCTSTR 解析为 LPCSTR 或 LPCWSTR,对于包括 char 或 wchar_t 的所有其他类型,始终存在一个 -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.

请注意,所有这些都是 Microsoft 特定的.TCHAR 不是标准的 C++ 类型,它是 windows.h 中定义的宏.wmain 和 _tmain 也仅由 Microsoft 定义.

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.

相关文章