Register
Handmade Hero»Forums»Code»Guide - How to avoid C/C++ runtime on Windows
14 posts
Guide - How to avoid C/C++ runtime on Windows
3 weeks, 1 day ago
mmozeiko
I checked in MSVC 2019 CRT - it does not call ExitProcess.

WinMainCRTStartup is in "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\crt\src\vcruntime\exe_winmain.cpp" file - that simply calls __scrt_common_main() and returns whatever this function returns.

__scrt_common_main is in "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\crt\src\vcruntime\exe_common.inl" file - and that simply calls invoke_main in same file (which calls your main) and uses its return value to return back to caller.

So CRT does not call ExitProcess. Maybe exe loader (one that calls WinMainCRTStartup) calls ExitProcess for you - but that is not part of CRT.


exe_common.inl:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
        //
        // Initialization is complete; invoke main...
        //

        int const main_result = invoke_main();

        //
        // main has returned; exit somehow...
        //

        if (!__scrt_is_managed_app())
            exit(main_result);


Isn't that it right there, the CRT exit function?

"[...]the C runtime library automatically calls ExitProcess when you exit the main thread, regardless of whether there are any worker threads still active." - https://devblogs.microsoft.com/oldnewthing/20100827-00/?p=13023

I stepped through the code to look. After returning from WinMain it checks if it's a managed app (__scrt_is_managed_app), it's not, and so it calls the exit function.
Which in my case, is either in the same executable (-MT, app.exit) or ucrtbase.dll (-MD, ucrtbase.exit).

Inside the exit function it eventually called ExitProcess. It looks like it could also call TerminateProcess depending on the "process end policy"
Mārtiņš Možeiko
2145 posts / 1 project
Guide - How to avoid C/C++ runtime on Windows
3 weeks, 1 day ago Edited by Mārtiņš Možeiko on Oct. 8, 2020, 8:53 a.m.
Oh, I'm blind and misread the condition - did not notice ! operator. You're right, for native code it will call exit(...) from this place.
2 posts
Guide - How to avoid C/C++ runtime on Windows
3 weeks ago
Thank you both for your investigations.

I suppose that I will simply do like the VS 2019 CRT runtime do and use exit.