Why can’t I create my dialog box? Rookie mistake #2

Date:February 7, 2007 / year-entry #44
Tags:code
Orig Link:https://blogs.msdn.microsoft.com/oldnewthing/20070207-04/?p=28113
Comments:    8
Summary:Another class of rookie mistake is less obvious from looking at the code. #define DLG_SAMPLE 1 DLG_SAMPLE DIALOGEX 32, 32, 210, 200 ... BEGIN ... CONTROL "",IDC_LISTVIEW,WC_LISTVIEW,LVS_REPORT | WS_TABSTOP | WS_BORDER, 14,92,182, 80 ... END DialogBox(hinst, MAKEINTRESOURCE(DLG_SAMPLE), hwnd, SampleDlgProc); The problem with this code is that we forgot to call InitCommonControlsEx to register the listview...

Another class of rookie mistake is less obvious from looking at the code.

#define DLG_SAMPLE 1

DLG_SAMPLE DIALOGEX 32, 32, 210, 200
...
BEGIN
 ...
 CONTROL "",IDC_LISTVIEW,WC_LISTVIEW,LVS_REPORT |
         WS_TABSTOP | WS_BORDER,
         14,92,182, 80
 ...
END

DialogBox(hinst, MAKEINTRESOURCE(DLG_SAMPLE),
          hwnd, SampleDlgProc);

The problem with this code is that we forgot to call InitCommonControlsEx to register the listview class. More generally, the problem is that one of the controls on the dialog uses a window class that was not registered. (For example, maybe there's a rich edit control on the dialog, but nobody remembered to load the rich edit library.)

Next time, a sophomore version of this mistake.


Comments (8)
  1. Chris says:

    Heh, yeah, I had that problem one time. A listview-equipped dialog was firing the WM_INITDIALOG message and then promptly closing, well before I ever saw a WM_PAINT or WM_COMMAND. Took about 30 minutes of headscratching before I realized I never loaded comctl32.

  2. Brian says:

    This mostly happens when you’ve been working on a medium-large project for a while and start taking for granted comctl32 always being loaded.  Then you decide you want to write a quick and dirty little utility app.

  3. Dean Earley says:

    Ive fallen for this. Debugging isn’t helped by the fact that BOTH CreateDialog and GetLastError returned 0…

  4. Kevin says:

    Is there a better way to debug the cause of failures like this?  Back in the Windows 3.1 days, you could install a "debug" kernel that would print out trace messages for failures of this sort, but the only similar thing I can find for Windows XP is the "checked" build, but most of the copious trace output it produces is useful only to device driver writers and not application developers.

  5. Jules says:

    If I get a failure I can’t understand, something I’ve done in the past is switch to a Linux machine and run it under wine.  You can then step into the (reasonably well-commented) source code for the API functions and see what they’re doing that’s causing a problem.

    That’s how I finally figured out what happens to the user data pointer when you call CreateWindowEx with WS_EX_MDICHILD.

  6. Larry III says:

    Sometimes is works without calling initcommoncontrols. Why? Does any other apis call it?

  7. MattCho says:

    Why do dialog boxes have a hidden file menu?  (You can see it by clicking on "SPACE" + "ALT" and then hitting "SPACE" again.

Comments are closed.


*DISCLAIMER: I DO NOT OWN THIS CONTENT. If you are the owner and would like it removed, please contact me. The content herein is an archived reproduction of entries from Raymond Chen's "Old New Thing" Blog (most recent link is here). It may have slight formatting modifications for consistency and to improve readability.

WHY DID I DUPLICATE THIS CONTENT HERE? Let me first say this site has never had anything to sell and has never shown ads of any kind. I have nothing monetarily to gain by duplicating content here. Because I had made my own local copy of this content throughout the years, for ease of using tools like grep, I decided to put it online after I discovered some of the original content previously and publicly available, had disappeared approximately early to mid 2019. At the same time, I present the content in an easily accessible theme-agnostic way.

The information provided by Raymond's blog is, for all practical purposes, more authoritative on Windows Development than Microsoft's own MSDN documentation and should be considered supplemental reading to that documentation. The wealth of missing details provided by this blog that Microsoft could not or did not document about Windows over the years is vital enough, many would agree an online "backup" of these details is a necessary endeavor. Specifics include:

<-- Back to Old New Thing Archive Index