win32程式分為兩種:
1.控制台(/SUBSYSTEM:CONSOLE )
2.GUI(/SUBSYSTEM:WINDOWS)
首先看控制台版本的:
寫一段最簡單的,或者就直接使用編譯器參數的預設main函數,如下:
[cpp] view plain copy // EntryFunction.cpp : 定義控制台應用程式的進入點。 // #include "stdafx.h" int _tmain(int argc, _TCHAR* argv[]) { return 0; }
1.使用/MD選項
F9下斷到main函數的標籤處,F11後,在Call Stack中回溯到_tmainCRTStartup(),當前檔案為crtexe.c。如下:
2.使用/MT選項
F9下斷到main函數的標籤處,F11後,在Call Stack中回溯到_tmainCRTStartup(),當前檔案為crt0.c。如下:
也就是說,不同的運行時連結方式,其實現代碼也是不同的。但是其進入點函數的名稱是相同的,都是_tmainCRTStartup。這便是Windows下的啟動函數(真正的入口函數)。
下面我們分別討論,這兩者在入口函數中都幹了些什麼。
首先看_security_init_cookie,這個函數在兩個實現檔案中是一樣的,代碼如下:
[cpp] view plain copy /*** *__security_init_cookie(cookie) - init buffer overrun security cookie. * *Purpose: * Initialize the global buffer overrun security cookie which is used by * the /GS compile switch to detect overwrites to local array variables * the potentially corrupt the return address. This routine is called * at EXE/DLL startup. * *Entry: * *Exit: * *Exceptions: * *******************************************************************************/ void __cdecl __security_init_cookie(void) { UINT_PTR cookie; FT systime={0}; LARGE_INTEGER perfctr; /* * Do nothing if the global cookie has already been initialized. On x86, * reinitialize the cookie if it has been previously initialized to a * value with the high word 0x0000. Some versions of Windows will init * the cookie in the loader, but using an older mechanism which forced the * high word to zero. */