Click here to Skip to main content
15,881,803 members
Articles / Desktop Programming / MFC
Article

MFC under the hood

Rate me:
Please Sign up or sign in to vote.
4.86/5 (58 votes)
2 Jul 2002CPOL3 min read 399K   2.4K   147   72
Explains the basic MFC program flow including where WinMain and the message loop are hidden

Introduction

Most MFC/VC++ programmers generate their projects using App Wizard and are quite happy with that. Once in a while, you have a programmer who will ask you, what happened to WinMain and he is normally satisfied with the answer given that WinMain is hidden within the MFC libraries. In this article I'll try and explain the life-cycle of a typical MFC program. Before I do that I'd like to introduce you to the smallest MFC program you can write that will show a window on screen, other than by using a MessageBox.

Smallest MFC window-program

//NWinApp.h
class CNWinApp  : public CWinApp
{
public:
    BOOL InitInstance();
};
//NWinApp.cpp
#include <afxwin.h>
#include "NWinApp.h"

CNWinApp app;

BOOL CNWinApp::InitInstance()
{
    CFrameWnd *pnframe=new CFrameWnd;
    m_pMainWnd=pnframe;
    pnframe->Create(0,"Buster");
    pnframe->ShowWindow(SW_SHOW);
    return TRUE;
}

So, what happened to good old WinMain?

When you run your program the kernel first calls this function, WinMainCRTStartup. WinMainCRTStartup first initializes the CRT routines. Then it parses the command line and removes the filename portion and calls WinMain passing the parsed command line as lpszCommandLine. But then where is WinMain? :-) It is defined in appmodul.cpp which you can find in your MFC\SRC directory. Here is how the function is implemented.

extern "C" int WINAPI
_tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
	LPTSTR lpCmdLine, int nCmdShow)
{
    // call shared/exported WinMain
    return AfxWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
}

As you will observe, WinMain simply calls AfxWinMain. AfxWinMain is defined in winmain.cpp which you will find under your MFC\SRC directory. I'll list the function below exactly as it is defined.

int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
	LPTSTR lpCmdLine, int nCmdShow)
{
    ASSERT(hPrevInstance == NULL);

    int nReturnCode = -1;
    CWinThread* pThread = AfxGetThread();
    CWinApp* pApp = AfxGetApp();

    // AFX internal initialization
    if (!AfxWinInit(hInstance, hPrevInstance, lpCmdLine, nCmdShow))
        goto InitFailure;

    // App global initializations (rare)
    if (pApp != NULL && !pApp->InitApplication())
        goto InitFailure;

    // Perform specific initializations
    if (!pThread->InitInstance())
    {
        if (pThread->m_pMainWnd != NULL)
        {
            TRACE0("Warning: Destroying non-NULL m_pMainWnd\n");
            pThread->m_pMainWnd->DestroyWindow();
        }
        nReturnCode = pThread->ExitInstance();
        goto InitFailure;
    }
    nReturnCode = pThread->Run();

InitFailure:
#ifdef _DEBUG
    // Check for missing AfxLockTempMap calls
    if (AfxGetModuleThreadState()->m_nTempMapLock != 0)
    {
        TRACE1("Warning: Temp map lock count non-zero (%ld).\n",
            AfxGetModuleThreadState()->m_nTempMapLock);
    }
    AfxLockTempMaps();
    AfxUnlockTempMaps(-1);
#endif

    AfxWinTerm();
    return nReturnCode;
}

As you can see the functions AfxGetThread and AfxGetApp are used to get pointers to the CWinApp derived global object  and the current thread. If you are surprised as to how the global CWinApp derived object already exists, relax, C++ programs will first create all global and static objects before execution begins. All that happens much before AfxWinMain gets called. By the way, it must have been a slight shock to you to see a goto in there, eh?

Now there might be those of you who might be wondering where AfxGetThread and AfxGetApp got their information from. The answer is simple. Take a look at the CWinApp constructor in appcore.cpp. You'll find the following two lines.

pThreadState->m_pCurrentWinThread = this;

and 

pModuleState->m_pCurrentWinApp = this;

pThreadState is a AFX_MODULE_THREAD_STATE* and pModuleState is a AFX_MODULE_STATE*.

Thus when we create our global CNWinApp object, it's constructor gets called and the AFX_MODULE_STATE structure is setup properly. Now AfxWinInit is called and this function initializes the MFC framework. Now there is a call to InitApplication [this is for backward compatibility with 16-bit applications]. Now it calls the InitInstance of the CWinApp derived object. And as you can see from our code listing above we have overridden InitInstance and created a CFrameWnd object there. I'll repeat the code snippet here so that you won't have to scroll upwards.

BOOL CNWinApp::InitInstance()
{
    CFrameWnd *pnframe=new CFrameWnd;
    m_pMainWnd=pnframe;
    pnframe->Create(0,"Buster");
    pnframe->ShowWindow(SW_SHOW);
    return TRUE;
}

I have created my CFrameWnd object on the heap, otherwise the moment InitInstance exits, the window will get destroyed. I have also set m_pMainWnd to point to my CFrameWnd window. Once InitInstance returns [if it returns false an error is assumed, so we return true], CWinApp::Run is called. Within the Run function is implemented our default message loop. Run keeps getting and dispatching messages till it receives a WM_QUIT message. Once WM_QUIT is received Run returns and control returns to AfxWinMain which performs clean-up and lastly calls AfxWinTerm which deletes all the global application structures that were created.

Well, that's it. Pretty amusing to think that all this while you were writing MFC applications and you never bothered to think about what was happening under the hood.

DISCLAIMER

None of the information in this article is endorsed by me. I don't work for Microsoft and all my assumptions are just that - assumptions. Some of the info I have given might indeed be incorrect, though I'd say the probability for that is rather low. In case I have erred I'll be delighted if someone can correct those mistakes.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Nish Nishant is a technology enthusiast from Columbus, Ohio. He has over 20 years of software industry experience in various roles including Chief Technology Officer, Senior Solution Architect, Lead Software Architect, Principal Software Engineer, and Engineering/Architecture Team Leader. Nish is a 14-time recipient of the Microsoft Visual C++ MVP Award.

Nish authored C++/CLI in Action for Manning Publications in 2005, and co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is experienced in technology leadership, solution architecture, software architecture, cloud development (AWS and Azure), REST services, software engineering best practices, CI/CD, mentoring, and directing all stages of software development.

Nish's Technology Blog : voidnish.wordpress.com

Comments and Discussions

 
GeneralRe: Who is Colin Davies ? Pin
Nish Nishant8-Jun-03 7:15
sitebuilderNish Nishant8-Jun-03 7:15 
GeneralRe: Who is Colin Davies ? Pin
Anonymous8-Jun-03 23:56
Anonymous8-Jun-03 23:56 
AnswerRe: Who is Colin Davies ? Pin
ColinDavies9-Jun-03 0:43
ColinDavies9-Jun-03 0:43 
GeneralCommand Line Pin
andrewgs737-Jun-03 11:21
andrewgs737-Jun-03 11:21 
GeneralRe: Command Line Pin
Mohammed El Shoukry8-Jun-03 5:10
Mohammed El Shoukry8-Jun-03 5:10 
GeneralRe: Command Line Pin
staubc22-Jul-03 7:39
staubc22-Jul-03 7:39 
GeneralRe: Command Line Pin
Jobin Wilson8-Nov-05 20:10
Jobin Wilson8-Nov-05 20:10 
GeneralHello Sir Pin
Kamal A6-Jun-03 4:21
Kamal A6-Jun-03 4:21 
GeneralRe: Hello Sir Pin
Nish Nishant6-Jun-03 8:00
sitebuilderNish Nishant6-Jun-03 8:00 
GeneralHi Pin
Harvail28-Sep-02 5:50
Harvail28-Sep-02 5:50 
QuestionHow to trap a mouse click on the frame window? Pin
2-Jul-02 21:54
suss2-Jul-02 21:54 
AnswerRe: How to trap a mouse click on the frame window? Pin
Nish Nishant2-Jul-02 22:04
sitebuilderNish Nishant2-Jul-02 22:04 
AnswerRe: How to trap a mouse click on the frame window? Pin
Nish Nishant2-Jul-02 23:07
sitebuilderNish Nishant2-Jul-02 23:07 
GeneralArticle updated July 03rd 2002 Pin
Nish Nishant2-Jul-02 20:40
sitebuilderNish Nishant2-Jul-02 20:40 
GeneralRe: Article updated July 03rd 2002 Pin
Zac Howland3-Jul-02 8:54
Zac Howland3-Jul-02 8:54 
GeneralRe: Article updated July 03rd 2002 Pin
Nish Nishant3-Jul-02 14:42
sitebuilderNish Nishant3-Jul-02 14:42 
GeneralI can't compile the samallest MFC program. Pin
22-Feb-02 23:04
suss22-Feb-02 23:04 
GeneralRe: I can't compile the samallest MFC program. Pin
2-Mar-02 4:01
suss2-Mar-02 4:01 
GeneralRe: I can't compile the samallest MFC program. Pin
Zac Howland4-Jun-02 5:30
Zac Howland4-Jun-02 5:30 
GeneralRe: I can't compile the samallest MFC program. Pin
3-Jul-02 15:55
suss3-Jul-02 15:55 
GeneralYaar - you are the best Pin
18-Dec-01 11:12
suss18-Dec-01 11:12 
GeneralRe: Yaar - you are the best Pin
Nish Nishant18-Dec-01 16:02
sitebuilderNish Nishant18-Dec-01 16:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.