我正在处理32位MFC VC2010应用程序中出现的零星生产问题。该应用程序在Windows Server 2008 R2 Standard SP1 64位上运行。为什么CWnd :: CreateEx无法创建我的窗口?
该问题是由于未能创建CWnd派生类而导致的。发生故障时,AfxUnhookWindowCreate方法在CWnd :: CreateEx中返回false。这是因为pThreadState-> m_pWndInit变量不是NULL。它看起来像_AfxCbtFilterHook应该设置为NULL当HCBT_CREATEWND挂钩,但它似乎没有发生。我注销了CREATESTRUCT并将其与发生故障时的情况进行比较,但未发生且参数基本相同。
有没有人有什么可能会导致这种想法或如何我可以找出原因的想法?谢谢!
BOOL CWnd::CreateEx(DWORD dwExStyle, LPCTSTR lpszClassName,
LPCTSTR lpszWindowName, DWORD dwStyle,
int x, int y, int nWidth, int nHeight,
HWND hWndParent, HMENU nIDorHMenu, LPVOID lpParam)
{
...
if (!PreCreateWindow(cs))
{
PostNcDestroy();
return FALSE;
}
AfxHookWindowCreate(this);
HWND hWnd = ::AfxCtxCreateWindowEx(cs.dwExStyle, cs.lpszClass,
cs.lpszName, cs.style, cs.x, cs.y, cs.cx, cs.cy,
cs.hwndParent, cs.hMenu, cs.hInstance, cs.lpCreateParams);
...
if (!AfxUnhookWindowCreate())
PostNcDestroy(); // cleanup if CreateWindowEx fails too soon
...
BOOL AFXAPI AfxUnhookWindowCreate()
{
_AFX_THREAD_STATE* pThreadState = _afxThreadState.GetData();
#ifndef _AFXDLL
if (afxContextIsDLL && pThreadState->m_hHookOldCbtFilter != NULL)
{
::UnhookWindowsHookEx(pThreadState->m_hHookOldCbtFilter);
pThreadState->m_hHookOldCbtFilter = NULL;
}
#endif
if (pThreadState->m_pWndInit != NULL)
{
pThreadState->m_pWndInit = NULL;
return FALSE; // was not successfully hooked
}
return TRUE;
}
LRESULT CALLBACK
_AfxCbtFilterHook(int code, WPARAM wParam, LPARAM lParam)
{
_AFX_THREAD_STATE* pThreadState = _afxThreadState.GetData();
if (code != HCBT_CREATEWND)
{
// wait for HCBT_CREATEWND just pass others on...
return CallNextHookEx(pThreadState->m_hHookOldCbtFilter, code,
wParam, lParam);
}
...
pThreadState->m_pWndInit = NULL;
听起来像是一个线程问题:-) –
@user你是否从主线程调用了'CWnd :: CreateEx'? – sashoalm
是的,这发生在主线程上。有时窗口创建成功,有时它不在主线程的同一进程内。 – user1707438