Windows application initialization process

Source: Internet
Author: User

Windows ApplicationsProgramThere must be an entry point function, which will be called when the application starts to run. C/C ++ developers can use the following two entry point functions:

Int winapi _ twinmain (hinstance, hinstance, ptstr psz1_line,IntNcmdshow );
Int_ Tmain (IntArgc, tchar*Argv [], tchar* Envp []);

The specific symbols depend on whether we want to use Unicode strings. The operating system will not actually call the entry point function we have written. It will call the implementation by the C/C ++ Runtime Library and use-entry when linking: command Line option to set a C/C ++ startup function. This function initializes the C/C ++ Runtime Library so that we can use functions such as malloc and free. It ensures thatCodeBefore Execution, all global and static C ++ objects declared are correctly constructed.

All C/C ++ runtime functions do the same thing. The difference is that they are processing ANSI strings or Unicode strings. And the entry point function they call after initializing the C Runtime Library. Visual c ++Source code. You can find the source code of the four startup functions in the crtexe. c file. The functions of these functions are summarized as follows:

    • Gets a pointer to the complete command line pointing to the new process.
    • Gets a pointer to the environment variable of the new process.
    • Initialize the global variables of the C/C ++ runtime. If stdlib. H is included, our code can access these variables: _ osver, _ winmajor, _ winminor, _ argc, _ argv, etc.
    • Initialize the heap used by the memory allocation function (malloc and calloc) of the C Runtime Library and other underlying I/O routines ).
    • Call constructors of all global and static C ++ class objects.

After all the initialization work is completed, the C/C ++ start function calls the entry point function of the application. If I write a _ twinmain function and define _ Unicode, the calling process is as follows:

Getstartupinfo (&Startupinfo );IntNmainretval = wwinmain (hinstance )&_ Imagebase, null, pszcommandlineunicode, (startupinfo. dwflags& Startf_useshowwindow )? Startupinfo. wshowwindow: sw_showdefault );

If _ Unicode is not defined, the calling process is as follows:

Getstartupinfo (&Startupinfo );IntNmainretval = winmain (hinstance )&_ Imagebase, null, pszcommandlineansi, (startupinfo. dwflags& Startf_useshowwindow )? Startupinfo. wshowwindow: sw_showdefault );

Note: _ imagebase is a pseudo variable defined by the linker, indicating where the executable file is mapped to the application memory.

If we write a _ tmain function and define _ Unicode, the call process is as follows:

IntNmainretval = wmain (argc, argv, argp );

If _ Unicode is not defined, the call process is as follows:

IntNmainretval = Main (argc, argv, argp );

When you use the application generated by the Visual Studio wizard, the Cui application entry does not define the third parameter (Environment Variable Block), as shown below:

Int_ Tmain (IntArgc, tchar * argv []);

To access the environment variables of a process, replace

Int_ Tmain (IntArgc, tchar * argv [], tchar * env []);

This env parameter points to an array that contains all environment variables and their values, which are separated by equal signs (=.

After the entry point function returns, the start function calls the exit function of the C Runtime Library to pass the returned value (nmainretval) to it ).

The exit function executes the following tasks:

Call the _ onexit function to call any registered function.

Call the destructor of all global and static C ++ class objects.

In debug generation, if the _ crtdbg_leak_check_df flag is set, the _ crtdumpmemoryleaks function is called to generate a memory leak report.

Call the exitprocess function of the operating system to pass in nmainretval. This will cause the operating system to "kill" our process and set its exit code.

Note: For security reasons, Microsoft does not approve of using all these variables, because the code that uses these variables may start to execute before the C Runtime Library initializes these variables. We should directly call the corresponding Windows API functions.

Related Article

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: 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.