Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between WinMain and wWinMain

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?

like image 938
cuteCAT Avatar asked Nov 24 '09 19:11

cuteCAT


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 does WinMain mean?

WinMain() is the C entry point function of any windows application. Like normal DOS/console based application which has main() function as C entry point, in windows we have WinMain() instead. WinMain() is a function which is called by system during creation of a process.

What should WinMain return?

As a convention, WinMain should return 0 if it terminates before the msg loop is entered, and msg.

What is mainCRTStartup?

mainCRTStartup() is the entrypoint of the C runtime library. It initializes the CRT, calls any static initializers that you wrote in your code, then calls your main() function. Clearly it is essential that both the CRT and your own initialization is performed first.


2 Answers

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.

like image 72
Joey Avatar answered Sep 21 '22 09:09

Joey


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

like image 29
Anders Avatar answered Sep 22 '22 09:09

Anders