在MFC 的源码 winmain.cpp 中 //为什么不用pApp指针调用 InitInstance()?
#include "stdafx.h "
#include "sal.h "
/////////////////////////////////////////////////////////////////////////////
// Standard WinMain implementation
// Can be replaced as long as 'AfxWinInit ' is called first
int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
__in 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()) //为什么不用pApp指针调用 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;
}
[解决办法]
如果你要创建用户线程。
一般是从CThread继承。没有CWinApp 对象。。
并且跟据动态连编的原理。
如果你的程序有CWinApp对象。
pThread-> InitInstance()
应该就是调用的pApp-> InitInstance();
[解决办法]
可能是在创建用户界面线程的时候会不一样吧!
到时候
CWinThread* pThread = AfxGetThread();//当前线程
CWinApp* pApp = AfxGetApp(); //当前应用程序对象指针
这两个指针就不一样了!
pApp和pThread 只在初始化主线程的时候一样!
个人理解是这样的!错误的话还请高人指点!