"Reprint" The main function of MFC ran where

Source: Internet
Author: User

Original: http://blog.csdn.net/weiwenhp/article/details/8455471

The habit of Thinking

People who are used to C have to look at a program first to find out where the main function is, and then look down. Because the main function acts as the entry point for the program, the entire program is executed from there. When the SDK (Win32 API Project) is developed in C + +, it inherits the thought of C. , there is a main function, but now the main function to change the name, called WinMain, of course, sometimes there are variations, such as called _tWinMain, anyway, the name will always take a main, let us look at it. And in Qt, like C, the standard main function is the honest one.

We will find that there can be a separate main function in C + +, not in which class, or a global variable or global function that does not belong to any class. So it's not purely object-oriented. So C + + supports a variety of programming paradigms, it can be the same process-oriented paradigm as C, Or the addition of some ordinary class is based on the object paradigm, if the use of inheritance and polymorphism is object-oriented, and if the use of template is the generic paradigm. And these paradigms can be mixed with each other. and C # is a pure object-oriented, so it has a main function, but it is also in a class, As for the specific class which does not matter, you casually put. The general default is to put the program in this class. Of course, it is not that pure object-oriented is better than the mixed paradigm, and there should be advantages and disadvantages.

Hey, it's a bit far.

SDK the process in

Develop an interface with the SDK program the approximate process is like this. First of all, there is a main function to do the entry point. Then follow the steps below (for discussion convenience, just about the process, the code is incomplete)

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

{

MSG msg;

Initapplicatio (hinstance hinstance)//1th, register the form class, and specify the form procedure here WndProc

InitInstance (hinstance hinstance, int ncmdshow)//2nd step, create form

while (GetMessage (&msg, NULL, 0, 0))//3rd Step message loop, dispatch message

{

TranslateMessage (&MSG);

DispatchMessage (&MSG);

}

return (int) Msg.wparam; 4th step, exit the program

}

BOOL initapplicatio (hinstance hinstance)

{

Return RegisterClass (...);

}

BOOL InitInstance (hinstance hinstance, int ncmdshow)

{

CreateWindow (...); Create a form

ShowWindow (...); Show form

UpdateWindow (...); Send out WM_PAINT

return TRUE;

}

LRESULT CALLBACK WndProc (...) { }

Generating an interface program in MFC the general process is the same, just encapsulated. So we're interested in two questions.

1.MFC There are no Main function. , If there's a run, where is it ?

2. If there is a main function , which of the 4 steps involved in the specific operation is also Win32 API ?

Let's answer the next question.

MFC package behind the process

In fact, the Czech Republic of the book is very clear. But because of the details, there are dozens of pages, see easy to faint, and he cited the example of the old version of the MFC class, in the new version of some of the functions of the class will have a little change.

I'll just summarize the most concise process here. First, if there is a class CMyApp inherit from CWinApp.

1. for the first question , MFC is useful to the main function

Export WinMain to force linkage to the This module

extern int AFXAPI Afxwinmain (hinstance hinstance, HInstance hprevinstance,

_in_ LPTSTR lpcmdline, int ncmdshow);

This is the function, in the MFC source file Appmodul.cpp can see the code, then how the main is called by MFC, you see that note, is the linkage to the This module, which is called by the linker to call. Exactly, it was c-runtime. Called by the DLL,C Runtime dynamic link library.

The order in which main is called

We know that the entry point that can be seen from code in MFC is to define a global class that inherits from CWinApp. For example CMyApp Theapp; in C + +, the global variable is executed before main, so the Theapp is initialized before the main is called.

2. for the first 2 questions , the main function in the specific operation .

Knowing that there is a main function, you may have a hint of comfort in your heart. But still some feel uneasy is this main function of the specific operation is the same as in the SDK, is not also a few steps, the first registration window and then create a window and so on.

The answer is that the main function of the MFC call is roughly the same as the process, but the implementation details are quite different. Let's see what it says in the afxwinmain. You can see the detailed code in the Winmain.cpp.

Make this main function a little bit simpler, and the operation is probably

Afxwinmain (...)

{

The pointers to CWinApp and CWinThread are obtained by a global function first, because the two classes have been initialized before calling main. CMyApp initializes his parent class CWinApp, and the parent class of the parent class CWinThread

cwinthread* pThread = Afxgetthread ();

cwinapp* papp = AfxGetApp ();

The next few functions are almost complete with all the steps in the previous SDK

Papp->initapplication ();

Pthread->initinstance ();

Pthread->run ();

Afxwinterm (); End Program

}

Anyway, the end of the program we do not have to worry about, focus on the previous three steps, the registration window, create a window, and message dispatch.

The previous SDK program also happens to have the function initapplication Registration window, InitInstance Create and display the window. And the run function you suspect might be a dispatch message. In fact, the general idea is still true, but the implementation of details or there are quite a lot of differences.

Papp->initapplication (); This function does not actually register the window. Register window, create the display window is all in pthread->initinstance (); This function completes, InitInstance is a virtual function, And we'll rewrite it in our own code. So the last call is the InitInstance function we wrote, which is the function of the object-oriented polymorphism. Your pointer eventually points to the function defined by the corresponding subclass.

BOOL cmyapp::initinstance ()

{

m_pMainWnd = new Cmyframewnd; This action registers and creates the window, and m_pMainWnd is the window handle that is returned

M_pmainwnd->showwindow (m_nCmdShow); Display window

M_pmainwnd->updatewindow ();

}

Pthread->run (); Is the dispatch message, you can see the source code of the Cwinthreed Run function in Thrdcore.cpp, a little bit below.

Acquire and dispatch messages until a WM_QUIT message is received.

for (;;)

{}

But the handling of messages MFC uses a message-mapping mechanism, such as complexity. This is not discussed here, anyway, it is probably the Cmyframewnd as a window process on the line.

MFC How to package CreateWindow See : http://blog.csdn.net/weiwenhp/article/details/8796337

summed up can be so simple to say , MFC in a Main function , but it's called by the system. . then Main The function performs the same operation. , but it is through CWinApp and the CWinThread the pointer to call some related functions . and the pointer, because the virtual function is called , so the use of polymorphic in object-oriented , and then he wandered. . and then the hardest part might be that the message mechanism is a little more complicated here. . can not be simple with SDK in a one-to-one comparison . .

"Reprint" The main function of MFC ran where

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.