“APIENTRY_tWinMain”和“WINAPI WinMain”不同之处

发布于 2024-10-11 12:54:58 字数 388 浏览 7 评论 0原文

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

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

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

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

空宴 2024-10-18 12:54:58

_tWinMain 只是 tchar.h 中 WinMain 相应版本的 #define 快捷方式。

如果 定义 _UNICODE 后,_tWinMain 扩展为 wWinMain。否则,_tWinMainWinMain 相同。

相关的宏看起来像这样(实际上散布着很多其他代码):

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

_tWinMain is just a #define shortcut in tchar.h to the appropriate version of WinMain.

If _UNICODE is defined, then _tWinMain expands to wWinMain. Otherwise, _tWinMain is the same as 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
如何视而不见 2024-10-18 12:54:58

区别在于参数的编码,无论如何都是完全冗余的。只需丢弃参数,而是使用以下内容,您可以在其中控制编码:

hInstance is just GetModuleHandle(0)

hPrevInstance is not valid in无论如何,Win32

lpCmdLine 在 ANSI 和 Unicode 中均可用,分别通过 GetCommandLineA()GetCommandLineW()

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

通过使用 Win32 API 访问这些变量,您可能会保存一些全局变量,例如您小心保存的实例句柄,您认为该句柄仅适用于 WinMain

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:

hInstance is just GetModuleHandle(0)

hPrevInstance is not valid in Win32 anyway

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

nCmdShow is the wShowWindow parameter of the STARTUPINFO structure. Again, ANSI and Unicode variants, accessed using GetStartupInfoA(STARTUPINFOA*) and 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.

酒浓于脸红 2024-10-18 12:54:58

来自此链接

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

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

两者没有区别
两个。

如果未定义 UNICODE,则_tWinMain 被定义为 WinMain,并且
wWinMain 如果是的话。其目的是
让你编写可以构建的代码
在 ansi 和 unicode 下均适用。

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.

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.

戏剧牡丹亭 2024-10-18 12:54:58

这是 Ben Voigt 建议的包装器,无需智能配置和链接即可工作。

#include <windows.h>
#include <TCHAR.h>


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



int main(int nArgs, char* args[])
{

    HINSTANCE hInstance = GetModuleHandle(0);

    HINSTANCE hPrevInstance = NULL;

    LPTSTR cmdLine = GetCommandLine();

    STARTUPINFO startupInfo;
    GetStartupInfoA(&startupInfo);
    int nCmdShow = startupInfo.wShowWindow;

    int res = _tWinMain(hInstance, hPrevInstance, cmdLine, nCmdShow);

    return res;
}

Here is the wrapper as suggested by Ben Voigt, works w/o intelligent configuring and linking.

#include <windows.h>
#include <TCHAR.h>


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



int main(int nArgs, char* args[])
{

    HINSTANCE hInstance = GetModuleHandle(0);

    HINSTANCE hPrevInstance = NULL;

    LPTSTR cmdLine = GetCommandLine();

    STARTUPINFO startupInfo;
    GetStartupInfoA(&startupInfo);
    int nCmdShow = startupInfo.wShowWindow;

    int res = _tWinMain(hInstance, hPrevInstance, cmdLine, nCmdShow);

    return res;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文