3
votes

Greeting, I'm novice in MFC area.

I have a question about process of starting MFC application.

I learned that unlike SDK program, I don't have to write WinMain. Because It is supplied by the class library and is called when the application starts up.* (See reference here : https://msdn.microsoft.com/en-us/library/akdx0603.aspx)

And my curious part is here: *Then CWinApp calls member functions of the application object to initialize and run the application.

That sentence indicates that CWinApp already know address value of application object which is made by a programmer.

However, even though application object is defined as global variable, how can WinMain function find application object's address value?

I couldn't find any connection or declaration in my sample MFC code which brings address value to the WinMain function.

#include <afxwin.h>

class CHelloApp : public CWinApp
{
public:
virtual BOOL InitInstance();
};

class CMainFrame : public CFrameWnd
{
public:
CMainFrame();
protected:
afx_msg void OnPaint();
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
DECLARE_MESSAGE_MAP()
};
CHelloApp theApp;

BOOL CHelloApp::InitInstance()
{
m_pMainWnd = new CMainFrame;
m_pMainWnd->ShowWindow(m_nCmdShow);
return TRUE;
}

CMainFrame::CMainFrame()
{
Create(NULL, "HelloMFC Application");
}


BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()
1
MFC knows CWinApp, the InitInstance() function is virtual for a good reason.Hans Passant
appmodul.cpp implements _tWinMain, that simply calls AfxWinMain (defined in winmain.cpp). The pointer to the global CWinApp-derived object is stored in process-local storage, and is accessible from anywhere calling AfxGetApp.IInspectable
Thanks, but now I want to know how AfxGetApp function works. Googling on that function makes me more confused...구마왕
You have full source code available for AfxGetApp() (see afxstate.cpp). Navigating through MFC code is easiest when loading a solution of an MFC project into Visual Studio. You get syntax highlighting and navigation aids ("Go to definition...", etc.).IInspectable
First of all, learn to indent your code.Jabberwocky

1 Answers

1
votes

Good question, but you could answer it yourself when running your exe under a debugger, setting breakpoints at the right places and stepping into MFC source code.

The CRT provides a function mainCRTStartup. This function is the entry point that gets called when your program starts. mainCRTStartup calls __tmainCRTStartup. This function first calls _initterm to call the constructors for all global objects - like your CWinApp theApp. That constructor also calls CWinApp::CWinApp which stores the this pointer in a global state variable. When that is done __tmainCRTStartup calls WinMain which calls AfxWinMain. AfxWinMain is reading out the pointer to CWinApp theApp from the global state variable and calls the CWinApp's virtual member functions.

This only works because...

  • the constructor stored the this pointer
  • only one CWinApp object exists
  • CWinApp is used as an interface and thus the MFC does not need to know how exactly your CWinApp derived class looks like