Windows
Application Execution
Windows supports two types of applications. One is an application based on a graphical user interface (GUI), and the other is an application based on a console user interface (Cui. (There is no clear line between the two, that is, the Cui program can have a dialog interface, and the GUI interface can also have a console to output information) for an application: first, the operating system actually calls the C/C ++ startup function at runtime. This function is responsible for initializing the C/C ++ runtime database, so that you can call functions such as m a l o C and f r e. It also ensures that all declared global objects and static C ++ objects can be correctly created before code execution. The linked program is responsible for selecting the corresponding C/C ++ runtime startup function when it connects to the executable file. If/s u B s y s t e m: w I n d o W S link program is set, the link Program expects to find a function of Wi n m a I n or W wi n m a I n. If neither function exists, the linked program returns an error message indicating "unconverted external symbols. Otherwise, it can select the WI n m a I n c rt s t a r t u p function or W wi n m a I n C RT s t a r t u p function. The function of starting a function is summarized as follows: • retrieves the complete command line pointer pointing to the new process. • Retrieve the pointer to the environment variable of the new process. • Initialize global variables in the C/C ++ runtime. If the s t d l I B. H file is included, the code can access these variables. Table 4-1 lists these variables. • Initializes memory unit allocation functions (m a l o C and c a l o C) and memory stacks used by other low-layer input/output routines during the C Runtime. • Call constructors for all global and static C ++ class objects. 2. After all these initialization operations are completed, C/C ++ starts the function and calls the entry point function of the application. If you write a W wi n m a I n function, it will be called in the following form:
GetStartupInfo(&StartupInfo);int nMainRetVal = wWinMain(GetMjduleHandle(NULL), NULL, pszCommandLineUnicode, (StartupInfo.dwFlags & STARTF_USESHOWWINDOW) ? StartupInfo.wShowWindow:SW_SHOWDEFAULT);
3.
When the vertex function is returned, the start function calls the e x I t function in the C runtime and passes the return value (n m a I n R E T Va L) to it. E x I t functions are responsible for the following operations: • call any function registered by the _ onexit function. • Call The Destructor for all global and static C ++ class objects. • Call the exitprocess function of the operating system to pass nmainretval to it. This allows the operating system to undo the process and set its e x I t code. Appendix: entry points of various applications and corresponding start Functions
Application Type |
Entry Point |
Embedded Executable File startup Function |
GUI applications that require ANSI characters and strings |
Winmain |
Winmaincrtstartup |
GUI applications that require Unicode characters and strings |
Wwinmainw |
Winmaincrtstartup |
Cui applications that require ANSI characters and strings |
Main |
Maincrtstartup |
Cui applications that require Unicode characters and strings |
Wmain |
Wmaincrtstartup |