程序的静态分析:
由于所有的windows程序都开始于WinMain()函数,而在这段程序中并没有看到这个函数,事实上这个函数被封装到了MFC中了;CMyApp继承于类CWinApp,而CWinApp又继承于CWinThread。因此CWinThread是CMyApp的超类,m_pMainWnd 是定义在类CWinThread中的访问控制为public的CWnd*。在头文件及源文件中,宏定义也用到了多次,如afx _msg,它仅仅是一个占位符,以说明该函数是属于消息处理函数;而DECLARE_MESSAGE_MAP()、BEGIN_MESSAGE_MAP(CMyFrame, CFrameWnd)和END_MESSAGE_MAP()更是暗藏玄机。
程序的动态分析:
说实在的,这个简单的程序已经运行了很多次了,以前都是一带而过,而现在仔细想想,里面却包含了很多自己完全不知道的东西,那么就干脆来一次最彻底的接触。于是按下了F11的快捷键,进行Step Into Debug。
首先黄色箭头停在了APPMODUL.cpp文件中,而不是我所预料的那样出现在MyApp.cpp文件:
extern "C" int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { // call shared/exported WinMain return AfxWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow); } |
看来程序的入口就在这里了。_tWinMain的函数体只是简单的调用了AfxWinMain函数。OK,F11 again。
黄色箭头来到了WINMAIN.cpp文件中:
int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { ASSERT(hPrevInstance == NULL);
int nReturnCode = -1; CWinThread* pThread = AfxGetThread(); CWinApp* pApp = AfxGetApp();
// AFX internal initialization if (!AfxWinInit(hInstance, hPrevInstance, lpCmdLine, nCmdShow)) goto InitFailure;
// App global initializations (rare) if (pApp != NULL && !pApp->InitApplication()) goto InitFailure;
// Perform specific initializations if (!pThread->InitInstance()) { if (pThread->m_pMainWnd != NULL) { TRACE0("Warning: Destroying non-NULL m_pMainWnd "); pThread->m_pMainWnd->DestroyWindow(); } nReturnCode = pThread->ExitInstance(); goto InitFailure; } nReturnCode = pThread->Run();
InitFailure: #ifdef _DEBUG // Check for missing AfxLockTempMap calls if (AfxGetModuleThreadState()->m_nTempMapLock != 0) { TRACE1("Warning: Temp map lock count non-zero (%ld). ", AfxGetModuleThreadState()->m_nTempMapLock); } AfxLockTempMaps(); AfxUnlockTempMaps(-1); #endif AfxWinTerm(); return nReturnCode; } |
在AfxWinMain方法中:
首先,通过调用AfxGetThread方法获得一个CWinThread的类指针。继续step into 到AfxGetThread方法,可以看到该方法包含了获取当前进程指针的语句,如果得到的进程指针为空的话,则调用AfxGetApp以返回一个CWinThread的类指针。当执行了获取当前进程指针的语句后,可以发现返回的CWinThread类指针对应的类为CMyApp。
接着,通过调用AfxGetApp()获得一个CWinApp的类指针。我们可以发现pThread和pApp这两个指针对应着相同的地址,即都是CMyApp类指针对应的类亦为CMyApp。
在调用了两个MFC本身提供的初始化方法之后,调用了在CMyApp中实现的InitInstance方法;紧跟着就是一个十分重要的方法了—— Run。step into到Run方法,我们可以看到Run方法中包含了一个无限的循环,在这个循环当中,不断的从消息队列中获取消息并分发消息,直到获取的消息为退出(WM_QUIT)的时候才终止。
|