Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"APIENTRY _tWinMain" and "WINAPI WinMain" difference

Tags:

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)
like image 585
xRobot Avatar asked Jan 13 '11 15:01

xRobot


People also ask

Should I use WinMain or wWinMain?

The only difference between WinMain and wWinMain is the command line string and you should use wWinMain in Unicode applications (and all applications created these days should use Unicode). You can of course manually call GetCommandLineW() in WinMain and parse it yourself if you really want to.

What is function WinMain?

The WinMain function is identical to wWinMain, except the command-line arguments are passed as an ANSI string. The Unicode version is preferred. You can use the ANSI WinMain function even if you compile your program as Unicode. To get a Unicode copy of the command-line arguments, call the GetCommandLine function.


2 Answers

_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
like image 136
Cody Gray Avatar answered Sep 28 '22 00:09

Cody Gray


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.

like image 30
Ben Voigt Avatar answered Sep 27 '22 22:09

Ben Voigt