Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What functions does _WinMainCRTStartup perform?

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.
like image 330
Thomas Avatar asked Oct 17 '09 20:10

Thomas


People also ask

What is WinMain function?

The WinMain function is identical to wWinMain, except the command-line arguments are passed as an ANSI string. The Unicode version is preferred. You can use the ANSI WinMain function even if you compile your program as Unicode. To get a Unicode copy of the command-line arguments, call the GetCommandLine function.

What is mainCRTStartup?

mainCRTStartup() is the entrypoint of the C runtime library. It initializes the CRT, calls any static initializers that you wrote in your code, then calls your main() function. Clearly it is essential that both the CRT and your own initialization is performed first.

How do I change the entry point in Visual Studio?

Re: setting the entry point in visual studio community 2017 Go to the Solution Explorer. Click on the project. Go to the properties line and click it. Go to the linker line.

Which of the following defines the entry point into a C++ program?

From C/C++ programming perspective, the program entry point is main() function.


2 Answers

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.

like image 134
Michael Avatar answered Nov 19 '22 12:11

Michael


A true Win32 program written in C (not C++) doesn't need any initialization at all, so you can start your project with WinMainCRTStartup() instead of WinMain(HINSTANCE,...).

It's also possible but a bit harder to write console programs as true Win32 applications; the default name of entry point is _mainCRTStartup().

Disable all extra code generation features like stack probes, array checks etc. Debugging is still possible.

Initialization

Sometimes you need the first HINSTANCE parameter. For Win32 (except Win32s), it is fixed to (HINSTANCE)0x400000.

The nCmdShow parameter is always SW_SHOWDEFAULT.

If necessary, retrieve the command line with GetCommandLine().

Termination

When your program spawns threads, e.g. by calling GetOpenFileName(), returning from WinMainCRTStartup() with return keyword will hang your program — use ExitProcess() instead.

Caveats

You will run into considerable trouble when:

  • using stack frames (i.e. local variables) larger than 4 KBytes (per function)
  • using float-point arithmetic (e.g. float->int conversion)
  • using 64-bit integers on 32-bit machines (multiply, bit-shift operations)
  • using C++ new, delete, and static objects with non-zero-out-all-members constructors
  • using standard library functions like fopen(), printf() of course

Troubleshoot

There is a C standard library available on all Windows systems (since Windows 95), the MSVCRT.DLL.

To use it, import their entry points, e.g. using my msvcrt-light.lib (google for it). But there are still some caveats, especially when using compilers newer than MSVC6:

  • stack frames are still limited to 4 KBytes
  • _ftol_sse or _ftol2_sse must be routed to _ftol
  • _iob_func must be routed to _iob

Its initialization seems to run at load time. At least the file functions will run seemlessly.

like image 23
Henrik Haftmann Avatar answered Nov 19 '22 11:11

Henrik Haftmann