Search code examples
c++winapiatlwtl

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>
{
public:

    DECLARE_WND_CLASS(_T("TTTDlg"))

    enum { IDD = IDD_TTT_DIALOG };

    BEGIN_MSG_MAP_EX(MusicPlayerDialog)
        MSG_WM_INITDIALOG(OnInitDialog);
        MSG_WM_CLOSE(OnClose);
        MSG_WM_DESTROY(OnDestroy);
    END_MSG_MAP()

    TTTMainDialog();
    ~TTTMainDialog();

private:

    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);
myDialog.ShowWindow(nCmdShow);

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?


Solution

  • 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.