I working on an MFC class which derives from CWnd
and creates a hidden window in its constructor. The object itself is constructed inside a derived CWinApp::InitInstance function.
if (
this->CWnd::Create(
nullptr,
nullptr,
WS_DISABLED, // Even disabled it will receive broadcast messages.
{0, 0, 0, 0},
CWnd::GetDesktopWindow(),
fakeWindowId
) == FALSE
)
throw runtime_error{"failed to create window"};
When I run this code in a debug build it triggers the following assertion:
Debug Assertion Failed!
Program: C:\WINDOWS\SYSTEM32\mfc140ud.dll File: f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\wincore.cpp Line: 571
For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application)
If I ignore the assertion, the code works fine and no ill effect is observable. How do I address this assertion?
I'm registering the window as follows as well:
BOOL HiddenWindow::PreCreateWindow(CREATESTRUCTW& cs)
{
if ( ! CWnd::PreCreateWindow(cs))
return FALSE;
cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
WNDCLASSEXW wc;
ZeroMemory(&wc, sizeof(WNDCLASSEXW));
wc.cbSize = sizeof(WNDCLASSEXW);
const auto instance{AfxGetInstanceHandle()};
if (GetClassInfoExW(instance, this->className_.c_str(), &wc) == FALSE)
{
wc.lpszClassName = this->className_.c_str();
if ( ! RegisterClassExW(&wc))
{
Logger::Fatal(
"Registering the window for copy data message failed! Messages will not be "
"copied, error code {}.",
GetLastError()
);
return FALSE;
}
}
else
Logger::Debug(
"There is already a window registered under the class name '{}'.",
toString(this->className_)
);
cs.lpszClass = _wcsdup(this->className_.c_str());
return TRUE;
}