tags:

views:

336

answers:

2

A dialog I'm working on isn't displaying, using:

CWnd::CreateDlgIndirect(LPCDLGTEMPLATE lpDialogTemplate,CWnd* pParentWnd, HINSTANCE hInst)

The call to CreateDlgIndirect is in a lon-used base-class, which effectively takes the IDD of the dialog template in the resource file - it works fine for many other dialogs but I can't see what's different in my dialog. My dialog works fine when created in a more normal way, but I have to use the base class as it has loads of other functionality built in.

What I find when trawling through CWnd::CreateDlgIndirect in dlgcore.cpp, is that the plain Win32 API call is failing:

hWnd = ::CreateDialogIndirect(hInst, lpDialogTemplate,pParentWnd->GetSafeHwnd(), AfxDlgProc);

I can't step into that function for some reason, so all I see is the HWND is NULL.

Can anyone suggest what kind of problems might be causing this? I compared the two dialog resource templates and their properties are the same.

edit: I have one custom control on the dialog. When I remove this, it works. No idea why, what difference might this make?

+1  A: 

One of the more obscure ways for CreateDialogXXX to fail is for a child control on the dialog to fail creation. Usually because the application has not initialized the common controls library before attempting to effect the dialog creation. See InitCommonControlsEx

One way to check this is to open the dialog in the resource editor, go to the dialog's properties, and find and turn on the DS_NOFAILCREATE flag. Usually called something obscure like "No Fail Create". Or add the DS_NOFAILCREATE directly to your dialog template in memory. This will allow the dialog to show, and the culprit should be evident by its absence.

In the case that the child control is an actual custom control - well the custom window class is either not registered correctly, or at all. Check the HINSTANCE used in registration - unless the CS_GLOBAL flag is specified, window classes are identified by (hInstance, ClassName) - this prevents window classes using the same name in different dlls conflicting.

Chris Becke
All very helpful... it definitely is the custom control breaking things. Using DS_NOFAILCREATE the dialog appears without that control, and I get ASSERTs when anything tries to interact with it.But, why? It's just subclassing CWnd and registering a Window class in the constructor to allow me to add it as a custom control on the dialog template.
John
When you create a custom control you need to specify a class name. The class name needs to match a string passed to RegisterClassEx to register a window class. If you are subclassing the control, then instead of custom, just create a static/text control or something simple and subclass that.
Chris Becke
That's a hacky way to do it. It's the 'old' way from my research.
John
Whats hacky about it? there are two basic methods for providing custom controls: Subclass an existing control, or make a new control from scratch. If you make a new one from scratch, theres no need to subclass it.
Chris Becke
A: 

I found the problem. Please see my answer to this question

John