Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. About wWinMainCRTStartup. Can anybody tell me what it exactly is? How does it work?

About wWinMainCRTStartup. Can anybody tell me what it exactly is? How does it work?

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
8 Posts 4 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    frankgt40
    wrote on last edited by
    #1

    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!

    M M E 3 Replies Last reply
    0
    • F frankgt40

      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!

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #2

      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.

      F 1 Reply Last reply
      0
      • F frankgt40

        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!

        M Offline
        M Offline
        MicroVirus
        wrote on last edited by
        #3

        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.

        F 1 Reply Last reply
        0
        • M Maximilien

          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.

          F Offline
          F Offline
          frankgt40
          wrote on last edited by
          #4

          Tanks for answering. I'm now reading the MFC internals. I want to know the principle MFC works on.

          1 Reply Last reply
          0
          • F frankgt40

            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!

            E Offline
            E Offline
            Emilio Garavaglia
            wrote on last edited by
            #5

            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:

            F 1 Reply Last reply
            0
            • M MicroVirus

              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.

              F Offline
              F Offline
              frankgt40
              wrote on last edited by
              #6

              I already know that. And I just want more materials. This is the word describing. How can I get the concrete principles and explanations of the related Library sources. I searched the item on MSDN website but there is little materials of that subject.

              1 Reply Last reply
              0
              • E Emilio Garavaglia

                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:

                F Offline
                F Offline
                frankgt40
                wrote on last edited by
                #7

                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!

                E 1 Reply Last reply
                0
                • F frankgt40

                  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!

                  E Offline
                  E Offline
                  Emilio Garavaglia
                  wrote on last edited by
                  #8

                  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 destroyed

                  Global 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

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups