CreateWindow returns NULL in debug mode 
Author Message
 CreateWindow returns NULL in debug mode

I have a strange problem.  When I compile the code below in Release mode
everything works fine (a window is opened up), but when I compile it in Debug
mode the CreateWindow function keeps returning NULL (my OutputDebugString
prints to the de{*filter*}).  Then the program gets to the while(GetMessage) loop
and just sits there unless I tell the de{*filter*} to Break or Stop.

After seeing that hwnd was NULL, I put the GetLastError statement you can see
in there.  But there doesn't appear to be an error, as the error DWORD
variable is set to 0x00000000 when this statement runs..

So does anyone know what might be causing this?  Please keep in mind that I'm
not too familiar with Windows programming.  This code is pretty much ripped
straight from Programming Windows95 by Charles Petzold.

Thanks,
Geoff

-----window.cpp-------------------------------------
#include <windows.h>
#include "window.h" //<--this doesn't even have anything in yet

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR
lpCmdLine, int nShowCmd)
{

        WNDCLASSEX wndclass;
        HWND hwnd;
        char AppName[] = "GDS\0";
        char *szAppName = &AppName[0];
        MSG msg;
        DWORD error;

//      GDSengine eng;

//      eng = GDSengine();

                //Initialize wndclass
        wndclass.cbSize = sizeof(WNDCLASSEX);
        wndclass.style = CS_VREDRAW ;
        wndclass.lpfnWndProc = WndProc;
        wndclass.cbClsExtra = 0;
        wndclass.cbWndExtra = 0;
        wndclass.hInstance = hInstance;
        wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
        wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
        wndclass.hbrBackground = HBRUSH(GetStockObject(BLACK_BRUSH));
        wndclass.lpszClassName = szAppName;

        //Register the class
        RegisterClassEx (&wndclass);

        //Create the window
        //Up until this point hwnd = 0xccccccccc
        hwnd = CreateWindow(
                                szAppName,
                                "GDS\0",
                                WS_BORDER | WS_CAPTION | WS_SYSMENU |
WS_MINIMIZEBOX,
                                CW_USEDEFAULT,
                                CW_USEDEFAULT,
                                 640,
                                 480,
                                NULL,
                                NULL,
                                hInstance,
                                NULL);
        //now hwnd has beet set to 0x0000000000

        //engine.initializeViewer(hwnd);

        //up until this point error = 0xccccccccccc
        error = GetLastError();
        //now error = 0x000000000

        if (hwnd == NULL) OutputDebugString("Yep, it's NULL all right.\0");

        ShowWindow(hwnd, nShowCmd);
        UpdateWindow(hwnd);

        //Get and dispatch messages
        while (GetMessage(&msg,NULL,0,0))
        {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
        }

        //terminate the program
        return msg.wParam;

Quote:
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{

        switch(iMsg)
        {
        case WM_CREATE:
                return 0;
        case WM_PAINT:
                return 0;
        case WM_DESTROY:
                PostQuitMessage(0);
                return 0;
        }

        return DefWindowProc(hwnd, iMsg, wParam, lParam);

Quote:
}



Sun, 10 Nov 2002 03:00:00 GMT  
 CreateWindow returns NULL in debug mode
Do you check the return value of RegisterClassEx() ?

Cheers,
Guillaume.

Quote:

>I have a strange problem.  When I compile the code below in Release mode
>everything works fine (a window is opened up), but when I compile it in
Debug
>mode the CreateWindow function keeps returning NULL (my OutputDebugString
>prints to the de{*filter*}).  Then the program gets to the while(GetMessage)
loop
>and just sits there unless I tell the de{*filter*} to Break or Stop.

>After seeing that hwnd was NULL, I put the GetLastError statement you can
see
>in there.  But there doesn't appear to be an error, as the error DWORD
>variable is set to 0x00000000 when this statement runs..

>So does anyone know what might be causing this?  Please keep in mind that
I'm
>not too familiar with Windows programming.  This code is pretty much ripped
>straight from Programming Windows95 by Charles Petzold.

>Thanks,
>Geoff

>-----window.cpp-------------------------------------
>#include <windows.h>
>#include "window.h" //<--this doesn't even have anything in yet

>LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

>int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR
>lpCmdLine, int nShowCmd)
>{

>        WNDCLASSEX wndclass;
>        HWND hwnd;
>        char AppName[] = "GDS\0";
>        char *szAppName = &AppName[0];
>        MSG msg;
>        DWORD error;

>//      GDSengine eng;

>//      eng = GDSengine();

>                //Initialize wndclass
>        wndclass.cbSize = sizeof(WNDCLASSEX);
>        wndclass.style = CS_VREDRAW ;
>        wndclass.lpfnWndProc = WndProc;
>        wndclass.cbClsExtra = 0;
>        wndclass.cbWndExtra = 0;
>        wndclass.hInstance = hInstance;
>        wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
>        wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
>        wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
>        wndclass.hbrBackground = HBRUSH(GetStockObject(BLACK_BRUSH));
>        wndclass.lpszClassName = szAppName;

>        //Register the class
>        RegisterClassEx (&wndclass);

>        //Create the window
>        //Up until this point hwnd = 0xccccccccc
>        hwnd = CreateWindow(
>                                szAppName,
>                                "GDS\0",
>                                WS_BORDER | WS_CAPTION | WS_SYSMENU |
>WS_MINIMIZEBOX,
>                                CW_USEDEFAULT,
>                                CW_USEDEFAULT,
>                                 640,
>                                 480,
>                                NULL,
>                                NULL,
>                                hInstance,
>                                NULL);
>        //now hwnd has beet set to 0x0000000000

>        //engine.initializeViewer(hwnd);

>        //up until this point error = 0xccccccccccc
>        error = GetLastError();
>        //now error = 0x000000000

>        if (hwnd == NULL) OutputDebugString("Yep, it's NULL all right.\0");

>        ShowWindow(hwnd, nShowCmd);
>        UpdateWindow(hwnd);

>        //Get and dispatch messages
>        while (GetMessage(&msg,NULL,0,0))
>        {
>                TranslateMessage(&msg);
>                DispatchMessage(&msg);
>        }

>        //terminate the program
>        return msg.wParam;
>}

>LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM
lParam)
>{

>        switch(iMsg)
>        {
>        case WM_CREATE:
>                return 0;
>        case WM_PAINT:
>                return 0;
>        case WM_DESTROY:
>                PostQuitMessage(0);
>                return 0;
>        }

>        return DefWindowProc(hwnd, iMsg, wParam, lParam);
>}



Sun, 10 Nov 2002 03:00:00 GMT  
 CreateWindow returns NULL in debug mode
No, I wasn't.  I just went to put in something to check that, but it appears
to be working now.  I didn't change anything.  Hmmmmm.

Thanks anyways,
Geoff


Quote:

>Do you check the return value of RegisterClassEx() ?

>Cheers,
>Guillaume.


>>I have a strange problem.  When I compile the code below in Release mode
>>everything works fine (a window is opened up), but when I compile it in
>Debug
>>mode the CreateWindow function keeps returning NULL (my OutputDebugString
>>prints to the de{*filter*}).  Then the program gets to the while(GetMessage)
>loop
>>and just sits there unless I tell the de{*filter*} to Break or Stop.

>>After seeing that hwnd was NULL, I put the GetLastError statement you can
>see
>>in there.  But there doesn't appear to be an error, as the error DWORD
>>variable is set to 0x00000000 when this statement runs..

>>So does anyone know what might be causing this?  Please keep in mind that
>I'm
>>not too familiar with Windows programming.  This code is pretty much ripped
>>straight from Programming Windows95 by Charles Petzold.

>>Thanks,
>>Geoff

>>-----window.cpp-------------------------------------
>>#include <windows.h>
>>#include "window.h" //<--this doesn't even have anything in yet

>>LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

>>int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR
>>lpCmdLine, int nShowCmd)
>>{

>>        WNDCLASSEX wndclass;
>>        HWND hwnd;
>>        char AppName[] = "GDS\0";
>>        char *szAppName = &AppName[0];
>>        MSG msg;
>>        DWORD error;

>>//      GDSengine eng;

>>//      eng = GDSengine();

>>                //Initialize wndclass
>>        wndclass.cbSize = sizeof(WNDCLASSEX);
>>        wndclass.style = CS_VREDRAW ;
>>        wndclass.lpfnWndProc = WndProc;
>>        wndclass.cbClsExtra = 0;
>>        wndclass.cbWndExtra = 0;
>>        wndclass.hInstance = hInstance;
>>        wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
>>        wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
>>        wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
>>        wndclass.hbrBackground = HBRUSH(GetStockObject(BLACK_BRUSH));
>>        wndclass.lpszClassName = szAppName;

>>        //Register the class
>>        RegisterClassEx (&wndclass);

>>        //Create the window
>>        //Up until this point hwnd = 0xccccccccc
>>        hwnd = CreateWindow(
>>                                szAppName,
>>                                "GDS\0",
>>                                WS_BORDER | WS_CAPTION | WS_SYSMENU |
>>WS_MINIMIZEBOX,
>>                                CW_USEDEFAULT,
>>                                CW_USEDEFAULT,
>>                                 640,
>>                                 480,
>>                                NULL,
>>                                NULL,
>>                                hInstance,
>>                                NULL);
>>        //now hwnd has beet set to 0x0000000000

>>        //engine.initializeViewer(hwnd);

>>        //up until this point error = 0xccccccccccc
>>        error = GetLastError();
>>        //now error = 0x000000000

>>        if (hwnd == NULL) OutputDebugString("Yep, it's NULL all right.\0");

>>        ShowWindow(hwnd, nShowCmd);
>>        UpdateWindow(hwnd);

>>        //Get and dispatch messages
>>        while (GetMessage(&msg,NULL,0,0))
>>        {
>>                TranslateMessage(&msg);
>>                DispatchMessage(&msg);
>>        }

>>        //terminate the program
>>        return msg.wParam;
>>}

>>LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM
>lParam)
>>{

>>        switch(iMsg)
>>        {
>>        case WM_CREATE:
>>                return 0;
>>        case WM_PAINT:
>>                return 0;
>>        case WM_DESTROY:
>>                PostQuitMessage(0);
>>                return 0;
>>        }

>>        return DefWindowProc(hwnd, iMsg, wParam, lParam);
>>}



Sun, 10 Nov 2002 03:00:00 GMT  
 CreateWindow returns NULL in debug mode

Quote:
> ...  I didn't change anything.  Hmmmmm.

Sounds like you might have an unitialized variable that contains different
values each time your program runs.  Since your program came from Petzold,
it's probably small enough to set a few break points and examine the values
of variables (using Use | Debug windows | Watch or OutputDebugString) .

Hope this helps.
Richard



Fri, 15 Nov 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Only release mode: GetDocument() returns NULL

2. PrintDlg returns CDERR_MEMLOCKFAILURE in debugging mode

3. ActiveX control returns success from method when compiled in Debug mode but not in release

4. ItemHasChildren returns TRUE, then GetChildItem returns NULL

5. Works in Debug mode but not in Release mode

6. MIDL compiler fails during release mode but works fine in debug mode

7. Works in Debug Mode crahses in Release Mode

8. Run time Release mode error but no Debug mode error

9. VC 5.0 Relase Mode & Debug Mode problem

10. Quiet NaNs, release mode and debug mode

11. Complining one project in release mode and another in debug mode

12. Program not work at release mode but work at debug mode

 

 
Powered by phpBB® Forum Software