//////////////////////////////////////// ////
/* 1. Windows Help */
/* The program was born! */
//////////////////////////////////////// //
In Windows, a process core object is created for an application, and a 4 GB process address space is allocated to the application. The system loader
Load the executable file image of the application and some necessary code (including data and the dlls used by some applications) into the process address space of the application.
//////////////////////////////////////// /////
/* 2. What is the startup function? */
//////////////////////////////////////// /////
The Windows operating system will automatically create a main thread for the application while initializing the application process.
The startup function of the C/C ++ Runtime Library starts to run together. Many beginners do not know where the startup functions of the C/C ++ Runtime Library are.
A Brief Introduction: when your applicationWhen the link starts after compilation, The system Linker will set your application according to your application
Select a startup function for the C/C ++ Runtime Library (Note: These function declarations are in ../Visual Studio. NET/vc7/crt/src/crt0.c)
Generally, the startup function of the C/C ++ Runtime Library of the ansi gui application is as follows:
Int WinMainCRTStartup (void );
Note: The function is defined in./Visual Studio. NET/vc7/crt/src/crtexe. c.
The startup functions of other versions of the C/C ++ Runtime Library are as follows:
Ansi cui application: int mainCRTStartup (void );
Unicode CUI application: int wmainCRTStartup (void );
Unicode GUI application: int wWinMainCRTStartup (void );
The main function of the startup function of the C/C ++ Runtime Library is to initialize the C/C ++ Runtime Library and call the constructor for all global and static C ++ class objects.
//////////////////////////////////////// /////////
/* 3. What instructor Hou Jie said: "explosive device "*/
//////////////////////////////////////// /////////
One of the main functions of the startup functions of the C/C ++ Runtime Library mentioned above is to call constructors for all global and static C ++ class objects. The Application Object CMyWinApp theApp is constructed by the startup function calling its constructor.
//////////////////////////////////////// //////
/* 4. WinMain function debut */
//////////////////////////////////////// //////
The startup function int WinMainCRTStartup (void) of the C/C ++ runtime database; the WinMain function called --- the entry function of the main thread is:
Int WINAPI _ tWinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
Return AfxWinMain (hInstance, hPrevInstance, lpCmdLine, nCmdShow );
}
Note 1: This function is defined in ../Visual Studio. NET/vc7/atlmfc/src/mfc/appmodul. cpp.
NOTE 2: _ t is a macro defined to take care of the Unicode version.
//////////////////////////////////////// /////////
/* 5. MFC program entry point function */
//////////////////////////////////////// //////////
MFC makes a "Qian Kun" and transfers all the responsibilities of the WinMain function to the AfxWinMain function, the entry point function of the MFC program.
Note: This function is defined in ../Visual Studio. NET/vc7/atlmfc/src/mfc/winmain. cpp.
The AfxWinMain function consists of four modules: AfxWinInit, InitApplication, InitInstance, and Run.
The functions of these four modules are described below.
//////////////////////////////////////// ///////
/* Internal initialization of afx 5.1 */
//////////////////////////////////////// ///////
Do you still remember what I said in the third title, instructor Hou Jie, in the case of an "explosive device"? Some major data members of the "cwinapp" class will be assigned a value later. "The afxwininit function is the place where the data members are assigned a value. It reinitializes the members that play an important role throughout the process and calls afxinitthread () to initialize the main thread, these have paved the way for the future normal operation of the MFC framework.
//////////////////////////////////////// /////////
/* 5.2 Global initialization of the application */
//////////////////////////////////////// /////////
The InitApplication function (virtual) performs global initialization for the program:
Note 1: This function is defined in ../Visual Studio. NET/vc7/atlmfc/src/mfc/appcore. cpp.
The InitApplication function only calls CWinApp: LoadSysPolicies (), while the latter loads some registry information to initialize some program-defined structures and register some basic information for the program.
NOTE 2: in The MFC Document, there is such a sentence "The CWinApp: InitApplication member function is obsolete in MFC.", so you do not need to care about this virtual function in most cases.
//////////////////////////////////////// /////////
/* 5.3 Standard instantiation of Applications */
//////////////////////////////////////// //////////
CWinApp: InitInstance () is a virtual function. Most applications require the override function.
CMyWinApp: InitInstance () explicitly calls the base class InitInstance ();
CMyWinApp: InitInstance () after the help of its base class, start to execute a series of its own code to complete such operations as "Initializing OLE library ", "Set the primary key of the registry so that the program can save information to the Registry", "analyze standard shell commands", "generate the main framework, document, and view structure of the program ", "display Main Window of the program.
//////////////////////////////////////// ////
/* 5.4 "message pump" started */
//////////////////////////////////////// ////
As we all know, Windows is a message-based, event-driven operating system. This is also true for every Win32 program. So
How does an MFC Application implement the message mechanism? The MFC Application Framework encapsulates the message mechanism into a "message pump", which is started in CMyWinApp: Run.
- Int CWinThread: Run ()
- {
- ...
- // Acquire and dispatch messages until a WM_QUIT message is wrongly ed.
- For (;;)
- {
- // Phase1: check to see if we can do idle work
- While (bIdle &&! : PeekMessage (& (pState-> m_msgCur), NULL, PM_NOREMOVE ))
- {
- // Call OnIdle while in bIdle state
- If (! OnIdle (lIdleCount ++ ))
- BIdle = FALSE; // assume "no idle" state
- }
- // Phase2: pump messages while available
- Do
- {
- // Pump message, but quit on WM_QUIT
- If (! PumpMessage () // message Pump
- Return ExitInstance ();
- // Reset "no idle" state after pumping "normal" message
- // If (IsIdleMessage (& m_msgCur ))
- If (IsIdleMessage (& (pState-> m_msgCur )))
- {
- BIdle = TRUE;
- Lelecount = 0;
- }
- } While (: PeekMessage (& (pState-> m_msgCur), NULL, PM_NOREMOVE ));
- }
- }
- // CWinThread implementation helpers
- BOOL CWinThread: PumpMessage ()
- {
- Return AfxInternalPumpMessage ();
- }
- Bool afxapi AfxInternalPumpMessage () // some source code
- {
- _ AFX_THREAD_STATE * pState = AfxGetThreadState ();
- If (! : GetMessage (& (pState-> m_msgCur), NULL ))
- {
- ...//
- Return FALSE;
- }
- ...//
- // Process this message
- If (pState-> m_msgCur.message! = WM_KICKIDLE &&! AfxPreTranslateMessage (& (pState-> m_msgCur )))
- {
- : TranslateMessage (& (pState-> m_msgCur ));
- : DispatchMessage (& (pState-> m_msgCur ));
- }
- Return TRUE;
- }
Finally, the following message is displayed: TranslateMessage and: DispatchMessage. If you are familiar with Win32API programming, you will surely have a bright eye and finally dig out the source.
/////////////////////////////////////////
/* 6. Final work */
////////////////////////////////////////
Almost all of the main processes of the MFC application have been mined. Let's take a look at the final work.
When the application finds a WM_QUIT message in the message queue, nReturnCode = pThread-> Run (); CWinApp: Run () returns and sets the return value. The following describes how to execute the AfxWinTerm function.
This function is mainly used to clear and release the released items, such as uninstalling hooks.
When the AfxWinTerm function ends, the AfxWinMain function returns the nReturnCode value, and the value is also returned as the return value of the _ WinMain function.
From
Http://www.chinaitpower.com/A/2001-10-06/878.html