C++ “APIENTRY _tWinMain”和“WINAPI WinMain”的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4681443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:08:29  来源:igfitidea点击:

"APIENTRY _tWinMain" and "WINAPI WinMain" difference

c++windowswinapiwinmain

提问by xRobot

What are the difference from these 2 function?:

这两个函数有什么区别?:

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)

int WINAPI WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)

回答by Cody Gray

_tWinMainis just a #defineshortcut in tchar.h to the appropriate version of WinMain.

_tWinMain只是#definetchar.h 中适当版本的WinMain.

If _UNICODEis defined, then _tWinMainexpands to wWinMain. Otherwise, _tWinMainis the same as WinMain.

如果_UNICODE已定义,则_tWinMain扩展为wWinMain。否则,_tWinMain与 相同WinMain

The relevant macro looks something like this (there's actually a lot of other code interspersed):

相关的宏看起来像这样(实际上还有很多其他代码穿插):

#ifdef  _UNICODE
#define _tWinMain  wWinMain
#else
#define _tWinMain  WinMain
#endif

回答by Ben Voigt

The difference is the encoding of the parameters, which are completely redundant anyway. Just throw away the parameters and instead use the following, where you control the encoding:

不同之处在于参数的编码,无论如何都是完全多余的。只需扔掉参数,而是使用以下内容,您可以在其中控制编码:

hInstanceis just GetModuleHandle(0)

hInstance只是 GetModuleHandle(0)

hPrevInstanceis not valid in Win32 anyway

hPrevInstance无论如何在 Win32 中无效

lpCmdLineis available in both ANSI and Unicode, via GetCommandLineA()and GetCommandLineW(), respectively

lpCmdLine分别通过GetCommandLineA()和提供 ANSI 和 Unicode 版本GetCommandLineW()

nCmdShowis the wShowWindowparameter of the STARTUPINFOstructure. Again, ANSI and Unicode variants, accessed using GetStartupInfoA(STARTUPINFOA*)and GetStartupInfoW(STARTUPINFOW*).

nCmdShow是结构wShowWindow参数STARTUPINFO。同样,ANSI 和 Unicode 变体,使用GetStartupInfoA(STARTUPINFOA*)和访问GetStartupInfoW(STARTUPINFOW*)

And by using the Win32 APIs to access these, you're probably going to save a few global variables, like the one where you were carefully saving the instance handle you thought was only available to WinMain.

并且通过使用 Win32 API 来访问这些,您可能会保存一些全局变量,例如您小心地保存您认为仅适用于WinMain.

回答by chrisaycock

From this link:

这个链接

_tWinMain actually does take the hPrevInstance parameter, but that parameter isn''t used.

_tWinMain is just a #define to WinMain (in TCHAR.h).

There is no difference between the two.

_tWinMain 实际上确实采用了 hPrevInstance 参数,但未使用该参数。

_tWinMain 只是 WinMain 的 #define(在 TCHAR.h 中)。

两者没有区别。

and

_tWinMain is defined to WinMain if UNICODE is not defined, and to wWinMain if it is. its purpose is to let you write code that will build both under ansi and under unicode.

如果未定义 UNICODE,则 _tWinMain 定义为 WinMain,如果定义了,则定义为 wWinMain。它的目的是让您编写可以在 ansi 和 unicode 下构建的代码。