MFC Review (2) "Life and Death" of the MFC Application"

Source: Internet
Author: User

//////////////////////////////////////// ////
/* 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.

  1. Int CWinThread: Run ()
  2. {
  3. ...
  4. // Acquire and dispatch messages until a WM_QUIT message is wrongly ed.
  5. For (;;)
  6. {
  7. // Phase1: check to see if we can do idle work
  8. While (bIdle &&! : PeekMessage (& (pState-> m_msgCur), NULL, PM_NOREMOVE ))
  9. {
  10. // Call OnIdle while in bIdle state
  11. If (! OnIdle (lIdleCount ++ ))
  12. BIdle = FALSE; // assume "no idle" state
  13. }
  14. // Phase2: pump messages while available
  15. Do
  16. {
  17. // Pump message, but quit on WM_QUIT
  18. If (! PumpMessage () // message Pump
  19. Return ExitInstance ();
  20. // Reset "no idle" state after pumping "normal" message
  21. // If (IsIdleMessage (& m_msgCur ))
  22. If (IsIdleMessage (& (pState-> m_msgCur )))
  23. {
  24. BIdle = TRUE;
  25. Lelecount = 0;
  26. }
  27. } While (: PeekMessage (& (pState-> m_msgCur), NULL, PM_NOREMOVE ));
  28. }
  29. }
  30. // CWinThread implementation helpers
  31. BOOL CWinThread: PumpMessage ()
  32. {
  33. Return AfxInternalPumpMessage ();
  34. }
  35. Bool afxapi AfxInternalPumpMessage () // some source code
  36. {
  37. _ AFX_THREAD_STATE * pState = AfxGetThreadState ();
  38. If (! : GetMessage (& (pState-> m_msgCur), NULL ))
  39. {
  40. ...//
  41. Return FALSE;
  42. }
  43. ...//
  44. // Process this message
  45. If (pState-> m_msgCur.message! = WM_KICKIDLE &&! AfxPreTranslateMessage (& (pState-> m_msgCur )))
  46. {
  47. : TranslateMessage (& (pState-> m_msgCur ));
  48. : DispatchMessage (& (pState-> m_msgCur ));
  49. }
  50. Return TRUE;
  51. }

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

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.