代码很简单如下:
BOOL CExtFuncs::_SetGroupCustomProp(LPCTSTR sGrpName, LPCTSTR sPropName)
{
HWND hwnd = AfxGetMainWnd()->GetSafeHwnd();//此处返回空
CWinApp* pApp = AfxGetApp();//此处返回空
CFrameWnd* pMain = (CFrameWnd*)AfxGetApp()->m_pMainWnd;//此处异常了
ASSERT(pMain);
::SendMessage(pMain->GetSafeHwnd(), WM_BASIC_GRP_PROP, (WPARAM)sGrpName, (LPARAM)sPropName);
return TRUE;
}
这是一个DLL中的函数在多文档中响应后调用,在Debug下一切正常,但是到了Release下居然出错了,调试发现居然是AfxGetApp返回值为空,这是为什么?编译器是VS2008。
BOOL CExtFuncs::_SetGroupCustomProp(LPCTSTR sGrpName, LPCTSTR sPropName)
{
HWND hwnd = AfxGetMainWnd()->GetSafeHwnd();//此处返回空
CWinApp* pApp = AfxGetApp();//此处返回空
CFrameWnd* pMain = (CFrameWnd*)AfxGetApp()->m_pMainWnd;//此处异常了
ASSERT(pMain);
::SendMessage(pMain->GetSafeHwnd(), WM_BASIC_GRP_PROP, (WPARAM)sGrpName, (LPARAM)sPropName);
return TRUE;
}
这是一个DLL中的函数在多文档中响应后调用,在Debug下一切正常,但是到了Release下居然出错了,调试发现居然是AfxGetApp返回值为空,这是为什么?编译器是VS2008。
{
HWND *phwnd = (HWND *)pParam;
return TRUE;
}
类似这样行不
具体参加源代码 1_AFXWIN_INLINE CWnd* AFXAPI AfxGetMainWnd()
2{ CWinThread* pThread = AfxGetThread();
3 return pThread != NULL ? pThread->GetMainWnd() : NULL; }
4
5//而AfxGetThread获取的是当前线程,而不是主线程!
6CWinThread* AFXAPI AfxGetThread()
7{
8// check for current thread in module thread state
9AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState();
10CWinThread* pThread = pState->m_pCurrentWinThread;
11return pThread;
12}
13
所以在非主线程里使用可能会有问题,解决方法,在新创建的线程里使用AfxGetApp()->m_pMainWnd;