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

Output

WinMain calling CWinApp::InitInstance()

MyApp.CPP


#include "afxwin.h"


class CMyApp : public  CWinApp
{

  public:
    virtual int InitInstance()
    {
      MessageBox(NULL,
                 "Demo How MFC WinMain calls CWinApp::InitInstance.",
                 "MFC WinMain CWinApp",
                 MB_OK);
      return 0;
    }
};
CMyApp app;

afxwin.h


#include <windows.h>

class CWinApp;

CWinApp*  AfxGetApp();

class CWinApp
{

  friend  CWinApp*  AfxGetApp();

protected:
  static CWinApp *m_App;
public:
  CWinApp();
  virtual int InitInstance() = 0;

};

APPMODULE.CPP

#include "afxwin.h"

CWinApp::CWinApp()
{
  m_App = this;
}

CWinApp * CWinApp::m_App = NULL;

CWinApp*  AfxGetApp()
{
  return CWinApp::m_App;
}


int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
   CWinApp * g_App = NULL;
    g_App = AfxGetApp();
  if(g_App == NULL) {
    return -1;
  }
  return g_App->InitInstance();
}

About our authors: Team EQA

Further readings

Where is WinMain() function in MFC application ?

MFC hides WinMain in its framework and includes source file on WinMain(). This explains how framework calls global CWinApp::Initinstance() from entry WinMain.

What is the utility of CWinApp class?

This is constructed during global C++ objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the ...

Basic steps in Win32 GUI Application with source code.

Define a custom Window class structure, Register the class name, CreateWindow, Show windows and write message get and dispatch loop statements. Define the Window CallBack procedure and write the handlers.

What is a Window CallBack procedure and what is its utility?

DispatchMessage() is a API which indirectly triggers the Window CallBack procedure. Message structure members from this function are passed to the CallBack procedure. CallBack procedure should implement event handlers depending on the need of the application.

What are LPARAM and WPARAM in window proc function?

LPARAM and WPARAM are the two parameters in Window CallBack procedure. They signifies parameters of various events. They are used in handing individual events.

What are the basic steps of a typical MFC based application?

We need to write WinMain and need to follow all these in a Win32 application. However we need not to write much if we are writing an application with MFC ...

#