MFC encapsulates the flow of Windows applications

Source: Internet
Author: User
Tags goto

MFC encapsulates the win API. Everyone knows. But how many people know about the real process of MFC applications?

Here are some of my analysis of the MFC startup code.

In TCHAR.H, there are 2 small pieces of define. Through my simplification, we can see:

//TCHAR.H
#ifdef _UNICODE
#define _tWinMain wWinMain
#else
#define _tWinMain WinMain
#endif

Because the WinMain is actually differentiated between Unicode and ANSI versions to support the UNICODE,C runtime. For the Unicode version of the program, the C Run-time Library will invoke the wWinMain, and for the ANSI version of the application, call WinMain. This is the 1th.

Then, in fact, MFC's code design is automatically support Unicode, so, MFC's WinMain in APPMODUL.CPP is defined as

_tWinMain (hinstance hinstance, hinstance hprevinstance, LPTSTR lpcmdline, int ncmdshow)

In this way, regardless of whether the user #define _UNICODE or not, MFC WinMain will be invoked. Next, the actual operation of _tWinMain is as follows:

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

In this way, Afxwinmain and MFC hook up. Even in Peter Norton's book, Afxwinmain is the WinMain of MFC, but Comrade Norton does not seem to be telling the obvious. because, WinMain is still the WINMAIN,C runtime and does not redesign the Afxwinmain portal because of the existence of MFC.

In the Afxwinmain of MS (WINMAIN. CPP), we can see the following code:

int Afxapi afxwinmain (hinstance hinstance, hinstance hprevinstance,
LPTSTR lpcmdline, int ncmdshow)
{ br> 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 initializatio NS
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;
}

The above code completely launches the entire large MFC architecture. If you want to understand its AFX big head of the function group, you can look at the MFC source code in the *core. Cpp.

The real difficulty is to find the alias of WinMain in TCHAR.H. Because almost no one would think that TCHAR.H incredibly hidden such a large MFC confidential AH ...

Now, MFC's startup process is completely clear ...:)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.