MFC是一个类库, 是别人写好的一套源码,实现了对系统API调用的封装,
与其辛苦学习使用别人设计的类库,不如好好学习一下其实现原理,
一个EXE窗口程序运行后,由系统载入调用的函数过程如下:
一、调用VC运行库文件crtexe.c中的WinMainCRTStartup函数
大致内容整理如下:
主要的功能是设置命令行参数和窗口启动的一些参数。
void WinMainCRTStartup(void )
{
int argc; /* three standard arguments to main */
_TSCHAR **argv;
_TSCHAR **envp;
int mainret;
_TUCHAR *lpszCommandLine;
STARTUPINFO StartupInfo;
_startupinfo startinfo;
__try {
/*
* Set __app_type properly
*/
__set_app_type(_GUI_APP);
__onexitbegin = __onexitend = (_PVFV *)(-1);
_adjust_fdiv = * _imp___adjust_fdiv;
_setargv();
if ( !__defaultmatherr )
__setusermatherr(_matherr);
_setdefaultprecision();
_initterm( __xi_a, __xi_z );
startinfo.newmode = _newmode;
__getmainargs(&argc, &argv, &envp, _dowildcard, &startinfo);
_initterm( __xc_a, __xc_z );
lpszCommandLine = (unsigned char *)_acmdln;
if ( *lpszCommandLine == DQUOTECHAR ) {
while ( *++lpszCommandLine && (*lpszCommandLine
if ( *lpszCommandLine == DQUOTECHAR )
lpszCommandLine++;
}
else {
while (*lpszCommandLine > SPACECHAR)
lpszCommandLine++;
}
while (*lpszCommandLine && (*lpszCommandLine <= SPACECHAR)) {
lpszCommandLine++;
}
StartupInfo.dwFlags = 0;
GetStartupInfo( &StartupInfo );
mainret = WinMain(
GetModuleHandle(NULL),
NULL,
lpszCommandLine,
StartupInfo.dwFlags & STARTF_USESHOWWINDOW
? StartupInfo.wShowWindow
: SW_SHOWDEFAULT
);
__initenv = envp;
mainret = main(argc, argv, envp);
exit(mainret);
}
__except ( _XcptFilter(GetExceptionCode(), GetExceptionInformation()) )
{
/*
* Should never reach here
*/
_exit( GetExceptionCode() );
} /* end of try - except */
}
二、调用WinMain函数
winmain是一个函数声明,每个程序中都要自己实现,如果自己没有实现这个函数,就会使用MFC库中实现,
所以每个Win32程序,必须要有一个winmain函数,因为VC运行库中的启动函数WinMainCRTStartup需要调用这个实现函数。
如MFC中也有这个函数声明
extern "C" int WINAPI
_tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
{
// call shared/exported WinMain
return AfxWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
}
三、MFC中的AfxWinMain调用
MFC程序也是先调用WinMain,然后转到MFC的AfxWinMain函数。
/
// export WinMain to force linkage to this module
extern int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow);
extern "C" int WINAPI
_tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
{
// call shared/exported WinMain
return AfxWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
}
四、AfxWinMain函数调用CWinApp中的函数
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)
{
TRACE(traceAppMsg, 0, "Warning: Destroying non-NULL m_pMainWnd\n");
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)
{
TRACE(traceAppMsg, 0, "Warning: Temp map lock count non-zero (%ld).\n",
AfxGetModuleThreadState()->m_nTempMapLock);
}
AfxLockTempMaps();
AfxUnlockTempMaps(-1);
#endif
AfxWinTerm();
return nReturnCode;
}
在这里就可以开始创建窗口,现在可以开始工作了。
BOOL CTEST1App::InitInstance()
{
CWinApp::InitInstance();
CTEST1Dlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
return nResponse;
}