Hi all

I have been following a tutorial on creating win32 GUI applications. I'm having a problem getting a dialog box to pop up on top of my main window ...the CreateDialog() method returns a NULL for some reason.

Would really appreciate some help with this. Here is a zip file containing the main CPP and as well as the resource file.


(the program runs, but the test condition for CreateDialog()'s return value shows a NULL value is returned)

Recommended Answers

All 13 Replies


The resource file:

#include "windows.h"
CAPTION "My Dialog Toolbar"
FONT 8, "MS Sans Serif"
PUSHBUTTON "&Press This Button",IDC_PRESS,7,7,84,14
PUSHBUTTON "&Or This One",IDC_OTHER,7,31,84,14

Window Procedure of the main window...

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    switch (message)                  
        case WM_CREATE:
            hDialog = CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_TOOLBAR),hwnd, ToolDlgProc);
            if(hDialog != NULL)
              ShowWindow(hDialog, SW_SHOW);
                        MessageBox(hwnd, "CreateDialog returned NULL", "Warning!", MB_OK | MB_ICONINFORMATION);
        //handle other messages...

...where the (HWND) hDialog is declared as global variable.

Message Handling function of the dialog box...

BOOL CALLBACK ToolDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
        case WM_INITDIALOG:
             return TRUE;
        case WM_COMMAND:
            case IDC_PRESS:
             MessageBox(hwnd, "Hi!", "This is a message", MB_OK | MB_ICONEXCLAMATION);
            case IDC_OTHER:
                  MessageBox(hwnd, "Bye!", "This is also a message", MB_OK | MB_ICONEXCLAMATION);
            return FALSE;
    return TRUE;

Try putting all the definitions,

#include <windows.h>

#define ID_FILE_EXIT 9001
#define ID_STUFF_GO 9002
#define ID_STUFF_ABOUT 9003
#define IDD_TOOLBAR 9004
#define IDC_PRESS 9005
#define IDC_OTHER 9006
#define ID_DIALOG_SHOW 9007
#define ID_DIALOG_HIDE 9008
#define ID_HELP_ABOUT 9009
#define IDD_ABOUT 9010

to a resource.h file and including the resource.h file to both your .rc file and main.cpp file. I dont know if the compiler allows the present rc file name. It hasn't got a file name. Only an extention. Correct that also.

What is the compiler you are using. Is it a IDE or command line compiler?

Hi Wolfpack

I'm using the Dev-Cpp IDE (I think it's the MinGW compiler that comes along with dev-cpp).

I tried the above, CreateDialogBox() still returns a NULL...
Is there an error in the contents of my .rc file?

There was nothing wrong with your rc file. I am using the Visual Studio IDE, and here is what I did to make the project compile.

1. Rename .rc to main.rc 2. Create a new file resource.h 3. Put the

#include <windows.h>

#define ID_FILE_EXIT 9001
#define ID_STUFF_GO 9002
#define ID_STUFF_ABOUT 9003
#define IDD_TOOLBAR 9004
#define IDC_PRESS 9005
#define IDC_OTHER 9006
#define ID_DIALOG_SHOW 9007
#define ID_DIALOG_HIDE 9008
#define ID_HELP_ABOUT 9009
#define IDD_ABOUT 9010

of your main.cpp file to the resource.h file.
4. Added

#include "resource.h"

to your main.cpp file and main.rc file.
5. Added the above 3 files to a Visual Studio Solution and built it..

After that the program ran okay. I dont know the procedure for dev-Cpp. But I think you will have to do the above 4 steps for any compiler. Try it and tell the result. Only the way you compile; step 5, will change for Dev-Cpp. Here are the modified files.

ah ... it runs fine in Dev-cpp. Thank you SO much, Wolfpack.

Just one more question. I'm creating this program to do some bitmap displaying/manipulation ... so I'd like to have another window (a blank one with no windows style/border and NO menu) pop up over this window.

So right after this bit of code ...

WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */

    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Windows App",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */

... I tried the following:
wincl.lpfnWndProc = ImageWindowProc();
registered wincl
created a new HWND imageHwnd using CreateWindowEx()
then used the ShowWindow method to display this new window as well.

created a new WNDCLASSEX -- wincl2.
After the creation of the main window:
initialized wincl2
registered wincl2
created a new HWND imageHwnd using CreateWindowEx()
used ShowWindow method to display this new window as well.

The problem is that in both of the above cases, the new window (whether it is the child is created as the child window of the main window or not) ... appears with the same menu that was added to the main window. The new window appears to share the main window's WindowProcedure method, even though I have tried to set it otherwise.

(My ImageWindowProc does NOT yet respond to any messages, e.g. display menu, dialogbox etc.)

I dont know what you are trying to do. But try replacing your main file with the one I have attached. It is a skeleton window procedure. It worked for me.No problems with menus or whatever.

Return Value

If the function succeeds, the return value is the handle to the dialog box.

If the function fails, the return value is NULL. To get extended error information, call GetLastError.

Try using GetLastError() function.

Hey guys... thanks for the replies

Yesterday, I used the main.cpp that Wolfpack included in GUI.zip ... everything worked fine, and the dialog box popped up.

Today I had to reinstall dev-cpp ... I tried compiling and running the same main.cpp but the dialogbox won't show up (a NULL value is returned again).
Everything is the same, except that I had to create a new resource file, with the same information, but this resource file did compile successfully.

I used GetLastError() ... it printed the value 1813.

Code :1813
Error :The specified resource type cannot be found in the image file.

1813 The specified resource type cannot be found in the image file.

One of the names you have used in the new resource file is different from the one you used yesterday.

ok ...

ah, ok ... nevermind. Something in my project options got reset, and it was no longer including my .rc file in the current project. :o

Thanks again, guys. Much appreciated.

apcxpc, your 7 year old hint with the reset options helped my out in my 3 year old Dev-cpp project, which a tried to reactivate. Thanks a lot ! Jens

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.