EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+

#Previous question #Index of VC++/WIN32/MFC Questions #Next question

MFC OOP and WinMain

MFC applications manage applications in object oriented fashion. It hides WinMain in its framework. MFC library has source folder and WinMain() is located in VC98\MFC\SRC\APPMODUL.CPP. This part of the code gets compiled and statically linked to each and every application we write.

CWinApp Global Instance

CWinApp class maintains a global pointer in a static member. This object should be declared in global scope. This is constructed when other C++ global objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the Microsoft Foundation Class Library.

WinMain to InitInstance

Programmer derives an application class from CWinApp must override the InitInstance member function and must create application's main window object. InitInstance is a pure virtual function and it should be implemented otherwise linking error happens. MFC library implements WinMain() in APPMODULE.CPP and obtains global pointer using AfxGetApp() and then calls InitInstance() virtual function using base pointer of type CWinApp.

Source Code

We have a demo source code to mimic MFC framework. It gives us the concept of how MFC implements WinMain and calls InitInstance(). It contains one header file and two C++ source file. To compile and build this demo create an empty Win32 Application (not console based) workspace and add these files.

  • MyApp.CPP - Our demo application displaying
  • afxwin.h - MFC header contains prototype of CWinApp
  • APPMODULE.CPP - MFC demo implementation of CWinApp and winMain


WinMain calling CWinApp::InitInstance()


#include "afxwin.h"
class CMyApp : public  CWinApp
    virtual int InitInstance()
10      {
11        MessageBox(NULL,
12                   "Demo How MFC WinMain calls CWinApp::InitInstance.",
13                   "MFC WinMain CWinApp",
14                   MB_OK);
15        return 0;
16      }
17  };
18  CMyApp app;


#include <windows.h>
class CWinApp;
CWinApp*  AfxGetApp();
class CWinApp
11    friend  CWinApp*  AfxGetApp();
13  protected:
14    static CWinApp *m_App;
15  public:
16    CWinApp();
17    virtual int InitInstance() = 0;
19  };


#include "afxwin.h"
  m_App = this;
CWinApp * CWinApp::m_App = NULL;
10  CWinApp*  AfxGetApp()
11  {
12    return CWinApp::m_App;
13  }
16  int APIENTRY WinMain(HINSTANCE hInstance,
17                       HINSTANCE hPrevInstance,
18                       LPSTR     lpCmdLine,
19                       int       nCmdShow)
20  {
21     CWinApp * g_App = NULL;
22      g_App = AfxGetApp();
23    if(g_App == NULL) {
24      return -1;
25    }
26    return g_App->InitInstance();
27  }

You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.

 Vote 1

Similar topics related to this section

#Programming Windows, Fifth Edition (Developer Reference)
#Programming Applications for Microsoft Windows (Microsoft Programming Series)
#Programming Windows with MFC, Second Edition
#Visual C++ Programming
#Programming Microsoft Visual C++, Fifth Edition

* #1 webmaster Sat 26 Apr/2014 15:07:37(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.


Your message goes here:

My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.