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?

- 4,798
- 4
- 28
- 52

- 131
- 1
- 2
- 10
2 Answers
It's up to the program writer to implement it. Similarly to the requirement to implement main() in non-Windows environment.

- 18,025
- 3
- 42
- 85
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.

- 46,945
- 8
- 85
- 181
-
"*This is the first user-authored code that executes*" - it should be noted that it is possible to write user code that runs *before* `WinMain()`/`main()` is called. For instance, in C++, using constructors of global objects. Or `#pragma startup` callback functions. – Remy Lebeau Apr 07 '20 at 07:05
-
@rem: That has already been noted in the referenced article by Raymond Chen (*"For example, the C++ language startup code will run global constructors before calling into `WinMain`"*). – IInspectable Apr 07 '20 at 07:16