About wWinMainCRTStartup. Can anybody tell me what it exactly is? How does it work?
-
When I ask what it exactly is, there are always someone told me that the wWinMainCRTStartup is the real entrance of the windows programs. And when you code with UNICODE you should set it with VC++. I found that this wWinMainCRTStartup function was called before the global object of 'CxxxApp' being constructed. It's called before the programs running. Can anybody tell what it exactly is? Is it involved with OS or compiler? Where does the Windows programs actually start and how? Or, are there any good books or references to be read for that? Thanks a lot!
-
When I ask what it exactly is, there are always someone told me that the wWinMainCRTStartup is the real entrance of the windows programs. And when you code with UNICODE you should set it with VC++. I found that this wWinMainCRTStartup function was called before the global object of 'CxxxApp' being constructed. It's called before the programs running. Can anybody tell what it exactly is? Is it involved with OS or compiler? Where does the Windows programs actually start and how? Or, are there any good books or references to be read for that? Thanks a lot!
The best book to lean what is going on in an MFC application is/was "MFC Internals[^]" have a look at this (and other related google searches) :http://msdn.microsoft.com/en-us/library/ff381406(v=vs.85).aspx[^] (or http://msdn.microsoft.com/en-us/library/f9t8842e.aspx[^] ) (or Win32 vs. MFC - Part I[^] )
Watched code never compiles.
-
When I ask what it exactly is, there are always someone told me that the wWinMainCRTStartup is the real entrance of the windows programs. And when you code with UNICODE you should set it with VC++. I found that this wWinMainCRTStartup function was called before the global object of 'CxxxApp' being constructed. It's called before the programs running. Can anybody tell what it exactly is? Is it involved with OS or compiler? Where does the Windows programs actually start and how? Or, are there any good books or references to be read for that? Thanks a lot!
Every windows application needs an entry point, to start execution. It's the first thing that gets run when windows starts a new process. This is wWinMainCRTStartup and not 'main', which is the first point of user code run, is because the C-runtime library needs to initialise before user code can be run. If you just want a barebone executable without CRT support then you could set it to your own function via the compiler.
-
The best book to lean what is going on in an MFC application is/was "MFC Internals[^]" have a look at this (and other related google searches) :http://msdn.microsoft.com/en-us/library/ff381406(v=vs.85).aspx[^] (or http://msdn.microsoft.com/en-us/library/f9t8842e.aspx[^] ) (or Win32 vs. MFC - Part I[^] )
Watched code never compiles.
-
When I ask what it exactly is, there are always someone told me that the wWinMainCRTStartup is the real entrance of the windows programs. And when you code with UNICODE you should set it with VC++. I found that this wWinMainCRTStartup function was called before the global object of 'CxxxApp' being constructed. It's called before the programs running. Can anybody tell what it exactly is? Is it involved with OS or compiler? Where does the Windows programs actually start and how? Or, are there any good books or references to be read for that? Thanks a lot!
It is the "initializer" and "cleaner" of the C-RunTine. The operating system calls it with a machine code jump after loading the executable in memory and filled up the relocation table. It takes care of the invocation of the constructors of all the global objects (since the exist "outside of main", they have to be create before main is called). It then invokes "main" (or WinMain) after unpacking the command line, and - when main returns, calls the destructor of the on-fly created static objects and the destructors of the global objects in reverse construction order, then finally, returns the main return value to the OS process who invoked the app.
2 bugs found. > recompile ... 65534 bugs found. :doh:
-
Every windows application needs an entry point, to start execution. It's the first thing that gets run when windows starts a new process. This is wWinMainCRTStartup and not 'main', which is the first point of user code run, is because the C-runtime library needs to initialise before user code can be run. If you just want a barebone executable without CRT support then you could set it to your own function via the compiler.
-
It is the "initializer" and "cleaner" of the C-RunTine. The operating system calls it with a machine code jump after loading the executable in memory and filled up the relocation table. It takes care of the invocation of the constructors of all the global objects (since the exist "outside of main", they have to be create before main is called). It then invokes "main" (or WinMain) after unpacking the command line, and - when main returns, calls the destructor of the on-fly created static objects and the destructors of the global objects in reverse construction order, then finally, returns the main return value to the OS process who invoked the app.
2 bugs found. > recompile ... 65534 bugs found. :doh:
-
You are right. I've followed the source code in crtexe.c and watched the program's executing sequences. And can you tell me why the OS must do this? Is it because of the relocating? Thanks!
The reason is not because of the OS (it simply adjust the relocation table after copying the executable image in memory BEFORE jumping in it, so the exe itself has no role in that) but because of how C++ works. Objects has constructors and destructors. Try this
#include <iostream>
class A
{
public:
A() { std::cout << "A created" << std::endl; }
~A() { std::cout << "A destroyed" << std::endl; }
};A global_a;
int main()
{
std::cout << "this is main" << std::endl;
return 0;
}The output will be
A created
this is main
A destroyedGlobal objects must be created/destroyed outside the scope of main. This is not an OS requirement, but a requirement for the C++ specification.
mainCRTSturtup
is the "bridge" between the OS (that has no clue about the app language specifications: it just want an address to be called) and a language like C++ that requires some code (constructors and destructors of global objects) to be executed independently of the main() function.2 bugs found. > recompile ... 65534 bugs found. :doh:
modified on Sunday, August 14, 2011 4:33 AM