Was just curious to know where the WinMain()
method in the Win32 API
is implemented.
I know that WinMain()
is declared in WinBase.h
, but when the application runs, where does it refer to for WinMain()
function's body?
The documentation for WinMain explains, what it is:
The user-provided entry point for a graphical Windows-based application.
It is declared in the SDK, but the program author is required to implement it.
When a new process is created, the OS calls into the executable image's exported entry point. By default, this is the C Runtime's (CRT) entry point (WinMainCRTStartup), that prepares the execution environment prior to transferring control to the user-provided entry point WinMain
. This is the first user-authored code that executes.
You can find more information on what is going on when when launching an executable on Windows at WinMain is just the conventional name for the Win32 process entry point.
There's also a CppCon 2018 talk by Matt Godbolt titled The Bits Between the Bits: How We Get to main(), that explains at length what's happening before the first line of user-authored code is executed. It is specific to Linux, but the principles apply to Windows as well.