Is MFC slow because of using CBT Hook

drjackool 956 Reputation points
2021-11-05T18:28:47.367+00:00

Hi
Following code is part of CWnd class and it uses CBT hook on window create so is it slow (because hooks make apps slow)? compared to normal Win32 message processing? also why Hook is used I think it is to catch WM_CREATE and other messages that directly sent to WindowProc!

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)
{
ASSERT(lpszClassName == NULL || AfxIsValidString(lpszClassName) ||
AfxIsValidAtom(lpszClassName));
ENSURE_ARG(lpszWindowName == NULL || AfxIsValidString(lpszWindowName));

// allow modification of several common create parameters
CREATESTRUCT cs;
cs.dwExStyle = dwExStyle;
cs.lpszClass = lpszClassName;
cs.lpszName = lpszWindowName;
cs.style = dwStyle;
cs.x = x;
cs.y = y;
cs.cx = nWidth;
cs.cy = nHeight;
cs.hwndParent = hWndParent;
cs.hMenu = nIDorHMenu;
cs.hInstance = AfxGetInstanceHandle();
cs.lpCreateParams = lpParam;

if (!PreCreateWindow(cs))
{
PostNcDestroy();
return FALSE;
}

AfxHookWindowCreate(this);
HWND hWnd = CreateWindowEx(cs.dwExStyle, cs.lpszClass,
cs.lpszName, cs.style, cs.x, cs.y, cs.cx, cs.cy,
cs.hwndParent, cs.hMenu, cs.hInstance, cs.lpCreateParams);

#ifdef _DEBUG
if (hWnd == NULL)
{
TRACE(traceAppMsg, 0, "Warning: Window creation failed: GetLastError returns 0x%8.8X\n",
GetLastError());
}
#endif

if (!AfxUnhookWindowCreate())
PostNcDestroy();        // cleanup if CreateWindowEx fails too soon

if (hWnd == NULL)
return FALSE;
ASSERT(hWnd == m_hWnd); // should have been set in send msg hook
return TRUE;
}

Thanks

Developer technologies | Visual Studio | Other
{count} votes

Accepted answer
  1. RLWA32 49,636 Reputation points
    2021-11-05T20:27:30.687+00:00

    Further to @David Lowndes comment the CBT hook is a vehicle that MFC uses to do its internal bookkeeping (including installing its own window subclass procedures). The CBT hook is not a global hook, only handles window creation and is removed after CreateWindowEx returns. While all of this is, by definition, more expensive than a vanilla call to CreateWindowEx, the effect of the CBT hook is transitory.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.