I still don't know where the main function of MFC is? How to run it? It's really not brilliant. So I decided to imitate the jjhou method, pick up the surgical knife, and go deep into the internal operating mechanism of MFC. But as the saying goes, step by step. That is to say, before we go into depth, we should first analyze... ...
If you use Appwizard to step by step next and then look for the winmain function in classview, you will be disappointed. What are the biggest characteristics of MFC? Encapsulation! The MFC package is so good that many people who want to learn it are discouraged. Let's talk less about it. Let's continue with our topic today, the main function! To tell you the truth, even if you search for all the files generated by MFC, you cannot find the word winmain. So where is it nearby?
I believe you have come up with the idea that the main function should be in the main application file. Is it the file "Your defined program name. cpp? That's it. Crtl + F again. Can you see the winmain function we are looking? You may be disappointed again, but you have the following idea:
//////////////////////////////////////// /////////////////////////////////////
// The one and only cmy1app object
Cmy1app
Theapp; // The project name I created is 1.
Isn't it special? Pay attention to the comment "the one and only cmy1app object". Each application has only one cmy1app object. I think you should have thought that each program of the winmain function can only have one. Is there a great relationship between this global object and the winmain function? That's right. Trust your intuition.
Now, we should go deep into the winmain operating mechanism.
First, let's take a look at the library file of MFC, which can bring us many surprises. (The corresponding directory of vc6 is/Microsoft Visual Studio/vc98/mfc/src; the corresponding directory of vc7 is/Microsoft
Visual Studio. NET 2003/vc7/atlmfc/src/mfc)
First, check the base class of cmy1app, the constructor of cwinapp (in appcore. cpp), and find the following content:
Cwinapp: cwinapp (lpctstr lpszappname)
{
If
(Lpszappname! = NULL)
M_pszappname
= _ Tcsdup (lpszappname );
Else
M_pszappname
= NULL;
//
Initialize cwinthread state
Afx_module_state *
Pmodulestate = _ afx_javastarget_getstate ();
Afx_module_thread_state *
Pthreadstate = pmodulestate-> m_thread;
Assert (afxgetthread ()
= NULL );
Pthreadstate-> m_pcurrentwinthread
= This;
Assert (afxgetthread ()
= This );
M_hthread
=: Getcurrentthread ();
M_nthreadid
=: Getcurrentthreadid ();
//
Initialize cwinapp state
Assert (afxcurrentwinapp
= NULL); // only one cwinapp object please
Pmodulestate-> m_pcurrentwinapp
= This;
Assert (afxgetapp ()
= This );
......
}
OK. You can do it here. Note:
Pmodulestate-> m_pcurrentwinapp
= This;
This Code indicates that this pointer is obtained for the Global Object of cwinapp. The member variables in cwinapp get the configuration and initial values because theapp is a global object. After theapp configuration is complete, the next step is today's main role. In the winmain function, open appmodul. cpp:
_ Twinmain (hinstance, hinstance hprevinstance,
Lptstr lpcmdline, int ncmdshow)
{
// Call shared/exported winmain
Return afxwinmain (hinstance,
Hprevinstance, lpcmdline, ncmdshow );
}
Then open winmain. cpp:
Int afxapi afxwinmain (hinstance
Hinstance, hinstance hprevinstance,
Lptstr
Lpcmdline, int ncmdshow)
{
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 initializations
If
(! Pthread-> initinstance ())
{
If
(Pthread-> m_pmainwnd! = NULL)
{
Trace (traceappmsg, 0, "Warning: destroying
Non-null m_pmainwnd/N ");
Pthread-> m_pmainwnd-> destroywindow ();
}
Nreturncode
= Pthread-> exitinstance ();
Goto
Initfailure;
}
Finally, we can see the true nature of winmain, but we cannot satisfy it. Next, we need to know its operating mechanism, but we need to know it. I only know it, but it is not brilliant!