InitInstance() and Overrides

How does the override InitInstance works?In writing the MFC program ( Using MFC in shared DLL memory).The class derived from the CWndApp contains a public function InitInstance which works as a constructor in properties.

consider a be an object of  Derived class from CWndApp()

(i.e . a.InitInstance is not itself called.Only the object 'a' is created .But the code written inside the InitInstance gets worked.How it is possible.A object can be communicated only thru messages only na.)

So please guide me the internals behind the Initializers and Overrides of MFC.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vinayak KumbarSr Program ManagerCommented:

VC++ online help says that
Windows allows several copies of the same program to run at the same time. Application initialization is conceptually divided into two sections: one-time application initialization that is done the first time the program runs, and instance initialization that runs each time a copy of the program runs, including the first time. The framework's implementation of WinMain calls this function.

So frame work calls it by its own.  

There are still some more articles on this. U can go through them.
Vinayak KumbarSr Program ManagerCommented:

Adding to that...

The applications InitInstance will be called by WinMain function
In Winmain.cpp u have a function called
int AFXAPI AfxWinMain(...). It calls the applications initInstance by pApp->InitInstance();

This is the very much vital function. A normal Initinstance function creates a main frame window and returns TRUE, allowing the program's message loop to run

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
When your application starts executing the following things happen
1. The application object gets created globally. Note that in the application class' implementation file, an instance of the application object gets created. This is outside any function and hence global.
2. WinMain() gets called. This function is added by the application framework.
3. Within WinMain a pointer to the application object is obtained by making the following call:
    CWinApp *pApp=AfxGetApp();
4. A call to the InitInstance function is then made in WinMain() as follows:
This will call the overriden version of the InitInstance function.
"VinExpert", I doubt "pamboo" can walk away after reading your answer, feeling he now understands the way InitInstance() gets activated.  Your answer consists of bare bones with hardly any meat to it.

"pagladasu's" answer is more illuminating, and is what I shall expand on.


C++ guarantees that a global object will get created BEFORE 'main()' or 'WinMain()' gets activated.  Therefore the global object I'm talking about is the one instantiated from CWinApp (which is derived from CWinThread, CCmdTarget, etc) that'll be created BEFORE WinMain() gets executed.

When the CWinApp object gets created, its constructor is responsible for initializing the various CWinApp member variables, including the module's thread state and the module state structure (i.e. AFX_MODULE_STATE).  Mostly all of these variables are set to NULL.

After the CWinApp object has been created, WinMain() is then called, which immediately calls another function by the name of AfxWinMain() that uses the same four parameters as WinMain().

Inside AfxWinMain(), one of the first thing it does, is to declare a pointer variable of type CWinApp and assign the value of AfxGetApp() to it.  This is shown in the following statement (as "pagladasu" also indicated).

CWinApp* pApp = AfxGetApp();

Next, AfxWinMain() calls AfxWinInit() whose main job is to initialize the MFC framework, setting the error mode for the application.  AfxWinInit() then calls AfxGetModuleState() to get the application's AFX_MODULE_STATE structure.

It is at this point now, that AfxWinMain() calls InitInstance(), initializing the specific instance of the application.  This is very important, because CWinApp's default version of InitInstance() does nothing.  It simply returns TRUE, which is why you must supply meaningful activities for InitInstance() to do (such as setting all the documents for the application and showing the main window).  You are able to perform these meaningful activities in your version of InitInstance() because it is declared as virtual in CWinApp, and it's your version that's overriding CWinApp's version.

After your version of InitInstance() has completed its activities, the last thing AfxWinMain() does before completing, is to call the CWinApp-derived object's Run() function, which starts the ball rolling with the message loop.


All I have done here is simply expanded on "pagladasu's" answer, who IMO should get the points.  "VinExpert" did very little and left you ("pamboo") on your own with his parting advice that "There are still some more articles on this. U can go through them."

You offered points to someone to explain this situation to you, NOT to tell you that "you can go through them."
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.