Translated and rephrased fromWindows via C/C ++
When the program runs, the loader of the operating system searches for information about the subsystem (subsystem) in the header of the executable file. If the sub-system is/Subsystem: ConsoleThe device is responsible for providing an available command line program to the application. If the subsystem is/Sub-system: WindowsThe loader does not do anything, but simply installs this application. Once the program starts running, the operating system does not care about the GUI program you are running.
The operating system does not call the program entry file you write.It will first call the C/C ++ Runtime Library(CRT, Which is msvcrt in Windows ).-EntryFlag. The entry function of the C Runtime Library initializes the Running Conditions of C/C ++ so that applications can use functions such as malloc or free, at the same time, it ensures that all global and static C ++ objects in the program are correctly created.
The linker selects the address of the entry function when linking various binary files of the program to produce executable files. Different entry functions are as follows:
Application Type |
Entry Point |
Startup function embedded In your executable |
GUI application that wants ANSI characters and strings |
_ Twinmain (winmain) |
Winmaincrtstartup |
GUI application that wants Unicode characters and strings |
_ Twinmain (wwinmain) |
Wwinmaincrtstartup |
Cui application that wants ANSI characters and strings |
_ Tmain (main) |
Maincrtstartup |
Cui application that wants Unicode characters and strings |
_ Tmain (wmain) |
Wmaincrtstartup |
The selection of entry functions depends on the settings of the linked GUI or Cui sub-system. If the corresponding function (winmain wwinmain main wmain) is not provided in the program ), the error "unresolved external symbol" is reported.
You can also leave it unspecified in the program./SubsystemIn this way, the program will automatically find whether or not one of the winmain wwinmain main wmain functions in the program, and then set the CRT entry function that needs to be linked to the program.
In vs, the sub-system settings are as follows:
The code of the CRT Startup Program is in crtexe. C, and their functions are as follows:
-Obtain the complete parameters of the command line.
-Get the environment variable of the process
-Initialize C/C ++ runtime variables, which can be defined in stdlib. h. These CRT runtime variables are as follows:
Variable name |
Type |
Description and recommended WINDOWS function replacement |
_ Osver |
Unsigned int |
The build version of the operating system. For example, Windows Vista RTM was build 6000. Thus,_ OsverHas a value of 6000. UseGetversionexInstead. |
_ Winmajor |
Unsigned int |
A major version of Windows in hexadecimal notation. For Windows Vista, the value is 6. UseGetversionexInstead. |
_ Winminor |
Unsigned int |
A minor version of Windows in hexadecimal notation. For Windows Vista, the value is 0. UseGetversionexInstead. |
_ Winver |
Unsigned int |
(_ Winmajor <8) + _ winminor. UseGetversionexInstead. |
_ Argc |
Unsigned int |
The number of arguments passed on the command line. UseGetcommandlineInstead. |
_ Argv _ Wargv |
Char Wchar_t |
An array of size_ ArgcWith pointers to ANSI/Unicode strings. Each array entry points to a command-line argument. Notice that_ ArgvIsNullIf_ UnicodeIs defined and_ WargvIsNullIf it is not defined. UseGetcommandlineInstead. |
_ Environ _ Wenviron |
Char Wchar_t |
An array of pointers to ANSI/Unicode strings. Each array entry points to an environment string. Notice that_ WenvironIsNullIf_ UnicodeIs not defined and_ EnvironIsNullIf_ UnicodeIs defined. UseGetenvironmentstringsOrGetenvironmentvariableInstead. |
_ Pgmptr _ Wpgmptr |
Char Wchar_t |
The ANSI/Unicode full path and name of the running program. Notice that_ PgmptrIsNullIf_ UnicodeIs defined and_ WpgmptrIsNullIf it is not defined. UseGetmodulefilename, PassingNullAs the first parameter, instead. |
-Initialize the heap and some underlying I/O programs used by the CRT memory allocation function (malloc free ).
-Call the global and static constructor of the C ++ object
When your entry-point function returns, the startup function callthe C run-timeExitFunction, passing it your return value (Nmainretval).ExitFunction does the following:
It callany functions registered by callto_ OnexitFunction.
It calldestructors for all global and static C ++ class objects.
InDebugBuilds, leaks in the C/C ++ run-time memory management are listed by a call to_ CrtdumpmemoryleaksFunction if_ Crtdbg_leak_check_dfFlag has been set.
It callthe operating system'sExitprocessFunction, passing itNmainretval. This causes the operating system to kill your process and set its exit code.
Notice that all these variables have been deprecated for security's sake because the code that is using them might be running before the C run-time Library has the chance to initialize them. this is why you shoshould rather directly call the corresponding funding of the Windows API.