Sossenbinder Sossenbinder - 1 year ago 80
C++ Question

CWindowImpl - Create() returns error code 1406

I just tried to setup a small win32 project, and was just about to create a window.

I created a dialog in the resource designer and set up this class for my window:

#pragma once
#include "stdafx.h"

class TTTMainDialog : public CWindowImpl<TTTMainDialog>


enum { IDD = IDD_TTT_DIALOG };




const BOOL OnInitDialog(const CWindow wndFocus, const LPARAM lInitParam);
void OnClose();
void OnDestroy();

As you can see, I added the window class declaration, I inherited CWindowImpl, I registered the dialog. I don't think I forgot something here.

In the class which is supposed to create the dialog, I tried to create it like this:

TTTMainDialog myDialog;
HWND handle = myDialog.Create(NULL);

However, the Create method does return NULL all the time. I checked the error code with GetLastError(), and it turns out i am getting error code 1406, or "ERROR_TLW_WITH_WSCHILD".

The msdn documentation says the following about this error:

"Cannot create a top-level child window."

I tried to google up on this, but there is not much to find.

If I had to take a guess I would say the problem is caused by some window class name details, but i'm really not sure.

Any advice?

Answer Source

You are trying to build a window class from wrong pieces.

The error is pretty descriptive: you are trying to create a parentless window with a WS_CHILD style and this does not work out.

You get the child style from default template parameter: CWindowImpl -> CWindowImplBaseT -> TWinTraits -> CControlWinTraits. CControlWinTraits is supposed for use with child control windows.

If you are going to use a dialog template (IDD_TTT_DIALOG) then the proper base class is CDialogImpl, which is already prepared to use proper window styles. Also, it has what it takes to create both modal and modeless dialogs. The latter act more like windows and are non-blocking but in the same time take dialog template resource with predefined controls.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download