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 ...:)