title: MFC CWnd::Create triggers assertion wincore.cpp
body: <p>I working on an MFC class which derives from <code>CWnd</code> and creates a hidden window in its constructor. The object itself is constructed inside a derived CWinApp::InitInstance function.</p>
  
  <pre class="lang-cpp prettyprint-override"><code>if (
      this-&gt;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"};
  </code></pre>
  
  <p>When I run this code in a debug build it triggers the following assertion:</p>
  
  <blockquote>
    <p>Debug Assertion Failed!</p>
    
    <p>Program: C:\WINDOWS\SYSTEM32\mfc140ud.dll File:
    f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\wincore.cpp Line: 571</p>
    
    <p>For information on how your program can cause an assertion failure,
    see the Visual C++ documentation on asserts.</p>
    
    <p>(Press Retry to debug the application)</p>
  </blockquote>
  
  <p>If I ignore the assertion, the code works fine and no ill effect is observable. How do I address this assertion?</p>
  
  <p>I'm registering the window as follows as well:</p>
  
  <pre><code>BOOL HiddenWindow::PreCreateWindow(CREATESTRUCTW&amp; cs)
  {
      if ( ! CWnd::PreCreateWindow(cs))
          return FALSE;
      cs.dwExStyle &amp;= ~WS_EX_CLIENTEDGE;
      WNDCLASSEXW wc;
      ZeroMemory(&amp;wc, sizeof(WNDCLASSEXW));
      wc.cbSize = sizeof(WNDCLASSEXW);
      const auto instance{AfxGetInstanceHandle()};
  
      if (GetClassInfoExW(instance, this-&gt;className_.c_str(), &amp;wc) == FALSE)
      {
          wc.lpszClassName = this-&gt;className_.c_str();
  
          if ( ! RegisterClassExW(&amp;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-&gt;className_)
          );
      cs.lpszClass = _wcsdup(this-&gt;className_.c_str());
      return TRUE;
  }
  </code></pre>