Thread: Dialog Box causes program to exit

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    3

    Dialog Box causes program to exit

    I had my program up and running fine, with my child windows running fine inside the main one. However, as soon as I called the function CreateDialog(...), such as by clicking on a menu item within my program, the program would automatically close.

    Here is the basic code (ignore any functions that are irrelevant):



    Code:
    HWND hAboutDialog ; 
    HWND g_hwnd;
    BOOL CALLBACK AboutDlgProc(HWND , UINT , WPARAM ,  LPARAM);  
    
    
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);		// Windows procedure
    
    int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nShow)
    {
    
    	
    
    
        MSG messages;													// Window Messages
    	
    
        WNDCLASSEX wincl;												// Window class
    	DWORD dwStyle;													// Window Style
    	nShow = SW_SHOW;
        
        wincl.hInstance = hThisInstance;								// Window Structure - Instance
        wincl.lpszClassName = szClassName;								// Window Structure - Class Name
        wincl.lpfnWndProc = WindowProcedure;							// Window Structure - Procedure
        wincl.style = CS_DBLCLKS;										// Window Structure - Style (catch dblclicks)
        wincl.cbSize = sizeof (WNDCLASSEX);								// Window Structure - Size
    
        
        wincl.hIcon = hIcon;											// Load Application's Icon
        wincl.hIconSm = hIcon;											// Load Application's Icon
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);					// Default cursor
        wincl.lpszMenuName = "myMenu";									// Create menu name
        wincl.cbClsExtra = 0;											// No extra bytes after window class
        wincl.cbWndExtra = 0;											// Window Instance
    	
       
        wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;				// Set default background
    
        
        if (!RegisterClassEx (&wincl))									// If window class doesn't register -> Quit
            return 0;
    
    	
    	
    	
    	
    	dwStyle = WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;	// Adjust window settings
    		
    
        
        g_hwnd = CreateWindowEx (											// Create the window
               0,                   
               szClassName,												// Class Name
               "Isis Media",											// Window title
               dwStyle,													// Style
               CW_USEDEFAULT,											// Default position
               CW_USEDEFAULT,											// Default position
               1024,													// Width (px)
               768,														// Height (px)
               NULL,													
               hMenu,													// Load My Menu              
               hThisInstance,											// Instance handler
               NULL                 
               );
    
        hAboutDialog = CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(IDI_ABOUT),g_hwnd,AboutDlgProc);
        ShowWindow (g_hwnd, nShow);			
    
    				ShowWindow(hAboutDialog,SW_SHOW);							// Show the window on the screen
    	
    
       
        while (GetMessage (&messages, NULL, 0, 0))						// Message loop
    		{
           
            TranslateMessage(&messages);								// Translate virtual-key messages into character messages
            
            DispatchMessage(&messages);									// Send message to WindowProcedure 
        }
    
        
        return messages.wParam;											// The program return-value is 0 - The value that PostQuitMessage() gave 
    }
    
    
    /*  This function is called by the Windows function DispatchMessage()  */
    
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)                  /* handle the messages */
        {
            case WM_DESTROY:
    			PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
                break;
    		case WM_COMMAND:
    			GetMenuMessage(hwnd,wParam);							// Check to see if menu item was pressed
    			
    			break;
            default:                      /* for messages that we don't deal with */
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
    
        return 0;
    }
    
    
    BOOL CALLBACK AboutDlgProc(HWND hWnd, UINT message , WPARAM wParam,  LPARAM lParam)
    	{
    		
    	switch(message)
    		{
    		case WM_INITDIALOG:
    			return true;
    			break;
    		default:
    		return DefDlgProc (hWnd, message, wParam, lParam);
    		
    		}
    
    	return false;
    	}



    Everything is set up fine - I've created the dialog box, given it the correct identifier etc

    Am I missing something stupid, such as not linking a specific library or is it something else?

    (I decided against using MFC as I'm not too familiar with it!)

    Thanks,

    Ed

  2. #2
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    First of all, I don't see any definitions for hMenu and hIcon, I'm assuming that they're in a header or that you didn't include them...

    Anyway, I think I found your problem:
    Code:
        g_hwnd = CreateWindowEx (  //Here you make the main window
               0,                   
               szClassName,
               "Isis Media",
               dwStyle,
               CW_USEDEFAULT,
               CW_USEDEFAULT,
               1024,
               768,
               NULL,
               hMenu,
               hThisInstance,
               NULL
               );
    
        hAboutDialog = CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(IDI_ABOUT),g_hwnd,AboutDlgProc);  //Then you make your about dialog	
        ShowWindow (g_hwnd, nShow);   You then hide(?) (Next time make sure to include all relevant code, as I don't know what nShow is) the main dialog
    
        ShowWindow(hAboutDialog,SW_SHOW);  //Lastly, you show your about dialog
    I've commented your code in red. So what are you trying to do? If you wanted to make it load the dialog when you clicked the menu, that would go under WM_COMMAND in the WindowProcedure. Or, if you're trying to make that (the about dialog) the first thing that you see, here's what you should do:
    1. Make the main dialog like you did, and then stop there in the WinMain function
    2. In the WindowProcedure, add switch item 'WM_INITDIALOG'
    3. Under the WM_INITDIALOG, create the about dialog, and you'll be on your way

    That's how you should do it, hope this helps.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    Hi there,

    Thanks for the quick reply. Yes I'd declared the menu and icons in the header, sorry I should have made that more clear. Also, nShow was set to SW_SHOW as default (it's defined at the top of the function.

    I tried your solution of adding a new case statement in the WindowsProcedure function but I'm still getting the same result: as soon as I use the CreateDialog(...) function, it cuts out. I had originally had it set up in the menu but I took it out of there because I thought something else was conflicting with it. Anyway, here's what the WinProce code looks like with your corrections:

    Code:
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)                  /* handle the messages */
        {
            case WM_DESTROY:
    			PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
                break;
    		case WM_COMMAND:
    			GetMenuMessage(hwnd,wParam);							// Check to see if menu item was pressed
    		case WM_INITDIALOG:
    			hAboutDialog = CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(IDI_ABOUT),g_hwnd,AboutDlgProc);
    			break;
            default:                      /* for messages that we don't deal with */
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
    
        return 0;
    }
    Regardless of where I choose to use ShowWindow(hAboutDialog,SW_SHOW), the program doesn't like it. I can't for the life of me see what I've done wrong!

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	switch (message)                  /* handle the messages */
    	{
    		case WM_DESTROY:
    			PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
    			break;
    		case WM_COMMAND:
    			GetMenuMessage(hwnd,wParam);							// Check to see if menu item was pressed
    		case WM_INITDIALOG:
    			hAboutDialog = CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(IDI_ABOUT),g_hwnd,AboutDlgProc);
    			break;
    		default:                      /* for messages that we don't deal with */
    			return DefWindowProc (hwnd, message, wParam, lParam);
    	}
    	return 0;
    }
    Try to keep your indentation consistent. Not doing it only confuses the readers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    >>hAboutDialog = CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE (IDI_ABOUT),g_hwnd,AboutDlgProc);

    You're trying to create it with g_hwnd, which I'm assuming is a global HWND...
    Anyway, do you see the parameters for WindowProcedure? One of them is a HWND called hwnd. That's the one you need to use to create it, not g_hwnd. I think that that's the problem, but I'm not sure.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    Nope still nothing! I had tried it with HWND as NULL in the CreateDialog statement and the effect is still the same. In response to Elysia, my code is laid out properly in VC++, it's just this forum word wraps it when I copy and paste, and I don't really see the need to go through it all and correct the line spacing when the code is still essentially the same...

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, that's because you mix spaces and tabs. Let it be a lesson to you - don't. Use one.
    Try calling GetLastError() after the CreateDialog to see if there's an error creating the dialog.
    I would also encourage you to use MFC in the future since it would reduce the code to 10-20 lines instead of 100 lines you have now.
    Last edited by Elysia; 01-12-2008 at 03:32 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    I can't really tell what you're trying to do here, but I would assume it would be trying to use a modeless dialog box as the main window of your app?

    in your WindowProcedure you are handling WM_INITDIALOG. I doubt you'll ever recieve this message, since the callback is related to a generic window (you create g_hwnd with CreateWindowEx). I'd think you should probably be creating the dialog when WindowProcedure recieves the WM_CREATE message (the equivalent of WM_INITDIALOG for a generic window).

    as for your ShowWindow code, it looks like you are quite confused with it. after calling CreateWindowEx, try hiding the g_hwnd window (you want to display the dialog as your program's main window, not this one). don't use nShow to pass the second argument to ShowWindow, remember you initialized it with SW_SHOW earlier (note this is unnecessary, by default WinMain will recieve a value in nShow equivalent to SW_SHOW). then after creating your dialog in WM_CREATE from WindowProcedure, show your dialog window (hAboutDialog), since unlike DialogBox, CreateDialog does not do this for you.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  9. #9
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Quote Originally Posted by norbs27 View Post
    Nope still nothing! I had tried it with HWND as NULL in the CreateDialog statement and the effect is still the same. In response to Elysia, my code is laid out properly in VC++, it's just this forum word wraps it when I copy and paste, and I don't really see the need to go through it all and correct the line spacing when the code is still essentially the same...
    Well putting the HWND as NULL certainly won't fix the problem
    Last edited by mikeman118; 01-13-2008 at 08:37 PM.

  10. #10
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Okay, I've just realized the problem (I think): you're trying to use CreateWindow to make a window that's already there. You need to use the DialogBox() function, like this:
    Code:
    DialogBox(GetModuleHandle(NULL), 
               MAKEINTRESOURCE(/*Dialog Name*/), hwnd, /*Dialog Procedure*/);
    That should most likely fix your problem.

  11. #11
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    For a start.....

    Is hMenu VALID when you use it?

    Does the WM_INITDIALOG in the aboutdlg proc get called (put a breakpoint in there)?

    Why are you returning TRUE to WM_INITDIALOG in the aboutdlg proc (do you have a control to set focus to)?

    Why are you not testing that calls to create windows/dialogs succeed (ie return valid HWNDs before you use the HWND)?

    What does GetLastError() return when you app fails?

    Where is your use of IsDialogMessage()?

    Where is your call to RegisterClass() for the About Dlg? [as required by your use of DefDlgProc()]


    As your code is all over the place (not indented correctly, shortcuts in syntax, etc) I can not see the error (nor can I be bothered re-indenting it to see if it is a syntax error).

    I have used many versions of MSVC and not had the issue you describe when posting here.

    Look at the code posted by Elysia. The syntax error in that code is MUCH easier to spot than in your code.
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 02-13-2008, 02:59 PM
  2. Dynamic array of pointers
    By csisz3r in forum C Programming
    Replies: 8
    Last Post: 09-25-2005, 02:06 PM
  3. Making dialog box the only window
    By PJYelton in forum Windows Programming
    Replies: 2
    Last Post: 09-06-2005, 12:02 PM
  4. validate data in a dialog box
    By COBOL2C++ in forum Windows Programming
    Replies: 4
    Last Post: 09-22-2003, 01:55 PM
  5. Simple Dialog Box (noob)
    By tegwin in forum Windows Programming
    Replies: 6
    Last Post: 06-30-2002, 06:04 PM