"APIENTRY _tWinMain" and "WINAPI WinMain" difference
Asked Answered
F

3

22

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)
Fey answered 13/1, 2011 at 15:0 Comment(3)
if you defined _UNICODE, then the second example would error because LPTSTR would be WSTR and wouldn't fit with WinMain, both WINAPI and APIENTRY are defined as __stdcallHydrazine
Right click on _tWinMain -> choose go to definition...Vizor
Your WinMain() declaration isn't correct, the 3rd argument is LPSTR. Both are archaic, you should be using wWinMain today.Holophrastic
E
53

_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
Elgin answered 13/1, 2011 at 15:4 Comment(0)
R
28

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.

Rosebay answered 11/8, 2014 at 19:26 Comment(0)
G
1

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.

Gallman answered 13/1, 2011 at 15:3 Comment(1)
Other posts further down on the same page you link to suggest that they are not exactly the same. The difference between the two depends on whether or not _UNICODE is defined.Elgin

© 2022 - 2024 — McMap. All rights reserved.