Is MFC slow because of using CBT Hook

drjackool 651 Reputation points

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

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

if (!PreCreateWindow(cs))
return FALSE;

HWND hWnd = CreateWindowEx(cs.dwExStyle, cs.lpszClass,
cs.lpszName,, cs.x, cs.y,,,
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",

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;


Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
2,423 questions
{count} votes

Accepted answer
  1. RLWA32 28,656 Reputation points

    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.

    No comments

0 additional answers

Sort by: Most helpful