Thread: CreateDialog() failure

  1. #1
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856

    CreateDialog() failure

    Hi. I just started writing an API app in MSVC so that I could use a dialog resource instead of CreateWindow() to create all of my controls and such.. Anyway, in WinMain() I am calling CreateDialog() but it is returning NULL. I simply can not figure out why.. I figure that is the only reason that nothing happens when I run the program (the dialog doesn't ever become visible, but the beep of the MessageBox() is heard). Also, my dialog does have the WS_VISIBLE flag, in case you're wondering. My WinMain() follows.
    Code:
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
      HWND hwnd = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_DIALOG_MAIN), NULL, (DLGPROC)WindowProc);
      if (!hwnd) {
        MessageBox (NULL, "Can't create Dialog", NULL, MB_OK);
        return 1;
      }
    
      MSG msg;
      while (GetMessage(&msg, NULL, 0, 0) > 0) {
          TranslateMessage(&msg);
          DispatchMessage(&msg);
      }
    
      return msg.wParam;
    }
    [EDIT]
    I've inserted some diagnostic output and this is what's happening:
    Before CreateDialog

    top of WindowProc
    WM_SETFONT

    top of WindowProc
    WM_DESTROY

    top of WindowProc
    WM_NCDESTROY

    After CreateDialog

    leaving WinMain
    So, at the call of CreateDialog() 3 messages are sent to the DlgProc (note that WM_INITDIALOG is not one of them): WM_SETFONT, WM_DESTROY, WM_NCDESTROY... Iow, after WM_SETFONT is called, the window is destroyed and this all happens before the call to CreateDialog() is even completed! What's going on? Come on API programmers. One of you has to have some insight...
    Last edited by LuckY; 05-08-2003 at 11:05 AM.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    WM_SETFONT is sent before WM_INITDIALOG so that's not an issue. What's most likely from the description you have given is that you are probably using a switch in a 'classic' window/dialog procedure and are not using 'break' or 'return' from the WM_SETFONT case statement/handler and so program execution is dropping through to the WM_DESTROY case statement/handler which is causing window destruction. Since this is happening before WM_INITDIALOG, CreateDialog will return a NULL handle.

    If that's not it, then you'll need to post more information.

    edit: oops! Just re-read your edited information and have just noticed you're not actually handling those messages - sorry about that.

    What does GetLastError have to say about it?
    Last edited by Ken Fitlike; 05-08-2003 at 07:22 PM.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Originally posted by Ken Fitlike
    What does GetLastError have to say about it?
    It is zero! It's making me nuts... I am guessing that it has something to do with the resource.. What I finally ended up doing is finding a skeleton app on the net and am just using that as my base instead and it works fine.. The strange thing about it is that basically line for line my source is identical to the skeleton source, and that's what leads me to believe that it's just the dialog resource that has troubles, but I have no idea why because that looks pretty much identical too!!!
    Anyway... Thanks Ken. Nice sig, btw.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>It is zero!
    I hate the error_success return

    you are including the resource.h? (got to ask the silly ones)

    and have added the script.rc to the project? (got to ask the silly ones)

    there is not another menu, dilaog or control on IDD_DIALOG_MAIN with the same int ID?

    WNDPROC (LRESULT) has a slightly different return to DLGPROC (INT_PTR), try using WNDPROC to cast the callback

    Look at the diffs between your code and the skeleton. Change one line at a time until the error is revealed. (slow and frustrating)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Can you zip everything together and post the .zip, it is so much easier to debug these things in a decent debugger.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  6. #6
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    I am attaching the zip, but I now know the reason for the problem (but not it's cause). I already deleted the project that didn't work, so I just recreated it in a new project and with nothing in the dialog it ran fine. Then... oh, then... I added a a listctrl and POOF it didn't work any more...
    So the dialog can't be created when it has a listctrl! Am I just lacking some info about using listctrl's with API? Is there something you are supposed to do for it?

    Sheesh.. That means I'm still going to have the problem in my new project once I insert the listctrl...

    Please fill me in if you know that which I do not.

  7. #7
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Your dialog uses a listview common control but you have not loaded in commctrl.dll so your application fails.

    Before using CreateDialog:
    Code:
    INITCOMMONCONTROLSEX icx;
    icx.dwSize=sizeof(icx);
    icx.dwICC=ICC_LISTVIEW_CLASSES;
    InitCommonControlsEx(&icx);
    You must #include <commctrl.h> and link with comctl32.lib to get this to work. Read up on the InitCommonControlsEx in msdn for more information.

    Some other points: return values from messages such as WM_DESTROY, WM_CLOSE should be zero ie FALSE. Also return FALSE from WM_INITDIALOG unless you are explicitly setting the focus to a control in your dialog yourself.

    Hope that helps.

    edit: Also read up on WinMain - the third parameter should not be a TCHAR pointer, just stick with LPSTR. This is because this parameter is never a UNICODE string so if you compile with UNICODE #defined you'll probably have problems as you have written it. If you need a UNICODE command line, use the GetCommandLine api function.

    edit: editing
    Last edited by Ken Fitlike; 05-09-2003 at 03:48 PM.

  8. #8
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Thanks Ken. I knew it had to be some kind of initialization problem. Common controls... *shakes head with a smirk* So simple
    My lack of API knowledge is shining brightly.

    Thanks again...

    Oh, btw, I think you meant to say link with comctl32.lib

  9. #9
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>Oh, btw, I think you meant to say link with comctl32.lib<<

    I did. Thanks for the correction.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File I/O Assertion Failure in VS2008
    By clegs in forum C Programming
    Replies: 5
    Last Post: 12-25-2008, 04:47 AM
  2. Integer Emulation
    By Elysia in forum C++ Programming
    Replies: 31
    Last Post: 03-18-2008, 01:03 PM
  3. CreateWindow failure
    By TheDan in forum Windows Programming
    Replies: 6
    Last Post: 07-08-2005, 07:49 AM
  4. Am i a Failure?
    By Failure!!! in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 05-28-2003, 11:50 AM
  5. Assertion failure while creating window
    By roktsyntst in forum Windows Programming
    Replies: 0
    Last Post: 02-10-2003, 08:18 PM