2024年4月2日发(作者:)
MFC程序的启动过程与相关函数的执行顺序
1、创建Application object对象theApp
程序一开始生产一个(且只有一个)Application object对象theApp,也即一个CWinApp对象,
这个全局对象一产生,便执行其构造函数,因为并没有定义CMyWinApp构造函数,所以即执行
CWinApp类的构造函数。该函数定义于第75行,你可以自己搜出来啃一啃,因此,
CWinApp之中的成员变量将因为theApp这个全局对象的诞生而获得配置与初值。
2、WinMain登场
用SDK编程序时,程序的入口点是WinMain函数,而在MFC程序里我们并没有看到WinMain
函数,哦!~ 原来她是被隐藏在MFC代码里面了。当theApp配置完成后,WinMain登场,慢!细
看程序,并没连到WinMain函数的代码啊!这个我也不知道,MFC早已准备好并由链接器直接加到
应用程序代码中了,原来她在里面,好,我们就认为当theApp配置完成后,程序
就转到来了。那执行什么呢?看看下面从摘出来的代码:
extern "C" int WINAPI
_tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int
nCmdShow)
{
// call shared/exported WinMain
return AfxWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
}
_tWinMain函数的“_t”是为了支持Unicode而准备的一个宏。
_tWinMain函数返回值是AfxWinMain函数的返回值,AfxWinMain函数定义于
第21行,稍加整理,去芜存菁,就可以看到这个“程序进入点”主要做些什么事:
int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR
lpCmdLine, int nCmdShow)
{
int nReturnCode = -1;
CWinApp* pApp = AfxGetApp();
AfxWinInit(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
pApp->InitApplication();
pApp->InitInstance()
nReturnCode = pApp->Run();


发布评论