What functions does _WinMainCRTStartup perform?

Thomas picture Thomas · Oct 17, 2009 · Viewed 12.9k times · Source

This is part of a series of at least two closely related, but distinct questions. I hope I'm doing the right thing by asking them separately.

I'm trying to get my Visual C++ 2008 app to work without the C Runtime Library. It's a Win32 GUI app without MFC or other fancy stuff, just plain Windows API.

So I set Project Properties -> Configuration -> C/C++ -> Advanced -> Omit Default Library Names to Yes (compiler flag /Zl) and rebuilt.

Then the linker complains about an unresolved external _WinMainCRTStartup. Fair enough, I can tell the linker to use a different entry point, say MyStartup. From what I gather around the web, _WinMainCRTStartup does some initialization stuff, and I probably want MyStartup to do a subset of that.

So my question is: What functions does _WinMainCRTStartup perform, and which of these can I omit if I don't use the CRT?

If you are knowledgeable about this stuff, please have a look at my other question too. Thanks!

Aside: Why do I want to do this in the first place?

  1. My app doesn't explicitly use any CRT functions.
  2. I like lean and mean apps.
  3. It'll teach me something new.

Answer

Michael picture Michael · Oct 17, 2009

The CRT's entry point does the following (this list is not complete):

  • Initializes global state needed by the CRT. If this is not done, you cannot use any functions or state provided by the CRT.
  • Initializes some global state that is used by the compiler. Run-time checks such as the security cookie used by /GS definitely stands out here. You can call __security_init_cookie yourself, however. You may need to add other code for other run-time checks.
  • Calls constructors on C++ objects. If you are writing C++ code, you may need to emulate this.
  • Retrieves command line and start up information provided by the OS and passes it your main. By default, no parameters are passed to the entry point of the program by the OS - they are all provied by the CRT.

The CRT source code is available with Visual Studio and you can step through the CRT's entry point in a debugger and find out exactly what it is doing.