c++ – Difference between WinMain and wWinMain

c++ – Difference between WinMain and wWinMain

On Windows XP, if an application entry is WinMain, does Windows convert the command line from Unicode to Ansi and pass to the application?


If the command line parameter must be in Unicode (for example, Unicode file name, conversion will cause some characters missing), does that mean that I must use wWinMain as the entry function?

Yes, you should, if you want to correctly handle Unicode arguments to your program.

The documentation to WinMain() on MSDN also agrees.

You can, however, also use GetCommandLineW to retrieve the command line specifically in Unicode.

WinMain/wWinMain is not the real Windows entry point. Windows just calls the function specified in the PE header with zero parameters.

When using the Microsoft tool chain this is void WinMainCRTStartup() { ... } when you are creating a GUI application and it is provided for you unless you link with /Zl.

The default WinMainCRTStartup code created by Visual C++ initializes the C run-time library, calls global constructors (if any) and then calls your WinMain/wWinMain function with a HINSTANCE from GetModuleHandle(NULL), the command line from GetCommandLineA/W() (skipping the over the filename in the command line) and the show command from GetStartupInfo.

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.

In Windows NT/2000/XP and later the command line is a Unicode string internally and WinMain/GetCommandLineA() gives you a converted version of this which might not be able to represent every single character correctly. On Windows 95/98/ME it is the other way around but GetCommandLineW() is always able to convert every character from GetCommandLineA().

c++ – Difference between WinMain and wWinMain

Windows XP upwards is unicode by default! And thus, no conversion is required. The C++ Runtime loader takes care of the argument passing to the application. Standard Win32 API dictates that the main entry is WinMain(…).

Leave a Reply

Your email address will not be published.