13

I'm fairly new to C++, but have done some .NET programming before.

What is the difference between main(...) and WinMain(...), I wrote a program earlier with main(...) and was able to call Win32 functions just fine like I am with WinMain(...), so this leads me to ask "where would it be best to use one over the other, or does it even make a difference?"

Drew Chapin
  • 7,779
  • 5
  • 58
  • 84
Jason Mills
  • 585
  • 2
  • 6
  • 20

2 Answers2

27

Talking about the Microsoft toolchain, conventionally, Win32 graphical applications have always started with WinMain, while main is used for console applications.

The difference between the two kinds of applications actually boils down (mostly) to a single setting in the executable, and is not in capability - a GUI application can create a console, and a console application can create a window - but in the behavior of the loader: for an exe marked as GUI the loader won't allocate any console, while a console exe will attach to the parent's console or create a new one if there isn't one.

For the entrypoint name, it is all just a matter of linker/CRT defaults: the "real" entry point of the executable is just an offset into the final executable, that points to a function that takes no parameters. It's the CRT that does its stuff, determines the parameters and then calls "your" entrypoint, so, in line of principle, they both could work exactly the same way.

The point is, the name/signature of the default entrypoint that the CRT looks for depends from the type of application you are building; if the compiler and linker are set to build a console application, they will look for a main (or wmain or _tmain, depending on Unicode settings), for a GUI application they use WinMain, and DllMain for a dll.


So:

  • use WinMain if you are building a GUI (=no console created for it at startup) application;
  • main for a console application;
  • DllMain for a dll.

Again, this all isn't written in stone (and there are ways to start a GUI application from a standard main), but "when in Rome, do as the Romans do" - i.e. it's usually best to follow the uses of the platform to avoid confusing other developers and going through untested/unsupported compiler settings just to change the signature of the entrypoint.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • `...the "real" entry point of the executable is just an offset into the final executable, that points to a function that takes no parameters.` Worth mentioning that that function is typically called `_start`. – Cole Tobin Jan 18 '20 at 20:09
  • @ColeJohnson: surely on Linux (I've written several), but is that true also on Win32? – Matteo Italia Jan 18 '20 at 21:14
  • On Windows, it’s generally called `mainCRTStartup` – Cole Tobin Mar 10 '20 at 15:46
2

WinMain() is Windows specific entry point to a Windows-based graphical application (you have windows stuff). main() is a standard C++ entry point (in Windows, it's a console based application)...

That said, you can use GUI stuff in console applications and allocate console in GUI applications.

I would recommend reading on consoles and GUI applications in Windows on MSDN.

lapk
  • 3,838
  • 1
  • 23
  • 28
  • This is by tradition only, there's nothing stopping a console-based app from creating a GUI and there's nothing stopping a GUI app from creating a console (as mentioned in Matteo Italia's answer). – dreamlax Sep 10 '13 at 02:30
  • 2
    @dreamlax And I quote: "_That said, you can use GUI stuff in console applications and allocate console in GUI applications._" – lapk Sep 10 '13 at 02:32