Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ CreateWindowEx returns NULL

Tags:

c++

windows

I'm trying to set up a simple window using C++, but my call to CreateWindowEx returns NULL. Most of the code I'm using comes from the example on the MSDN website. Nothing that I've tried has worked, and any help would be appreciated.

Here is the code:

//Include the windows header
#include <Windows.h>

//Forward declaration of the WndProc function
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

//Main entry point
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) {
    //Window class name
    const wchar_t windowName[] = L"Window Class";

    //Set up window class
    WNDCLASS wnd;
    wnd.lpfnWndProc = WndProc;
    wnd.hInstance = hInstance;
    wnd.lpszClassName = windowName;

    //Register window class
    RegisterClass(&wnd);

    //Create window
    //! This returns NULL
    HWND hWnd = CreateWindowEx(
        0,
        windowName,
        L"Windows Programming",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        NULL,
        NULL,
        hInstance,
        NULL
        );

    //Simple check to see if window creation failed
    if(hWnd == NULL) {
            //Pause
        system("PAUSE");
        return -1;
    }

    //Show the window
    ShowWindow(hWnd, nCmdShow);

    //Main message loop
    MSG msg;
    while(GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

//WndProc function
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch(msg) {
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hDc = BeginPaint(hWnd, &ps);

            FillRect(hDc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW + 1));

            EndPaint(hWnd, &ps);

            return 0;
        }
    case WM_DESTROY:
        {
            PostQuitMessage(0);
            return 0;
        }
    }

    return DefWindowProc(hWnd, msg, wParam, lParam);
}
like image 748
Biostreem Avatar asked Jul 21 '26 22:07

Biostreem


1 Answers

Note that the sample from MSDN zeros out all the fields of the WNDCLASS before setting the ones it cares about.

WNDCLASS wnd = { };  // from MSDN example

The empty braces are a C and C++ shorthand for initializing the entire structure to 0. It's also common to write this as { 0 }, which technically is slightly different but has the same net effect.

In your code, you dropped the initialization:

WNDCLASS wnd;  // your code

Thus you are likely getting some garbage value in one of the other important fields, like cbClsExtra or cbWndExtra, that rendered the class impossible to register. Since the class wasn't registered, you couldn't create a window of that class.

like image 121
Adrian McCarthy Avatar answered Jul 23 '26 12:07

Adrian McCarthy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!