Difference between WinMain and wWinMain
Asked Answered
N

3

32

The only difference is that Winmain takes char* for lpCmdLine parameter, while wWinMain takes wchar_t*.

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?

Nita answered 24/11, 2009 at 19:12 Comment(0)
C
25

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

Yes.

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.

Chinchilla answered 24/11, 2009 at 19:22 Comment(1)
There is a Microsoft article WinMain: The Application Entry Point that talks rather clearly to this point. "The WinMain function is identical to wWinMain, except the command-line arguments are passed as an ANSI string. The Unicode version is preferred."Twitty
P
17

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().

Puseyism answered 3/10, 2017 at 16:3 Comment(2)
can you add a source for your info so we can more?Witmer
@Witmer source for which statement?Puseyism
B
1

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(...).

Berwickupontweed answered 24/11, 2009 at 19:22 Comment(1)
"Note that lpCmdLine uses the LPSTR data type instead of the LPTSTR data type. This means that WinMain cannot be used by Unicode programs." (msdn.microsoft.com/en-us/library/ms633559.aspx)Chinchilla

© 2022 - 2024 — McMap. All rights reserved.