JuiceBox
JuiceBox

Reputation: 13

Why is this call to CreateWindow failing, and how can I fix it?

I'm following a tutorial for directx12 and can't even get over the creation of window D: There is an implemented error window which tells me that there was an error when creating the window i actually want to create. It's supposed to mean that it was "unsuccessful in getting a window handle"... I don't have anything as much as a clue of what that would mean but i would GREATLY appreciate some help about that.

#include "stdafx.h"

//Handle to the window
HWND hwnd = NULL;

//Name of the window
LPCTSTR WindowName = L"GameEngineApp";

//Title of the window
LPCTSTR WindowTitle = L"My Game Engine";

//Width and height of the window
int Width = 800;
int Height = 600;

//Toggle for fool screen
bool FullScreen = false;

//Toggle for window creation
bool InitializeWindow(HINSTANCE hInstance,
    int ShowWnd, int width, int height, bool fullscreen);

//Main application loop
void mainLoop()
{
    //The message var hold any windows message received through the 
    //PeekMessage function
    MSG msg;
    ZeroMemory(&msg, sizeof(MSG));

    while (true)
    {
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            //If there is a message, it is translated
            //then dispatched so Windows 
            //knows the program hasn't stopped working
            if (msg.message = WM_QUIT) {
                break;

                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
            else {
                //If there is no message the game 
                //code will run and the loop will continue
            }
        }
    }
}

//Callback function for windows messages
LRESULT CALLBACK WndProc(HWND hWnd,
    UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
    case WM_KEYDOWN:
        if (wParam == VK_ESCAPE) {
            if (MessageBox(0, L"Are you sure you want to exit?",
                L"Really?", MB_YESNO | MB_ICONQUESTION) == IDYES)
                DestroyWindow(hwnd);
        }
        return 0;

    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd,
        msg, wParam, lParam);
}

//Main Windows function
int WINAPI WinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nShowCmd)
{
    //Toggle window creation
    if (!InitializeWindow(hInstance, nShowCmd, Width, Height, FullScreen))
    {
        MessageBox(0, L"Window Initialization - Failed",
            L"Error", MB_OK);
        return 0;
    }

    mainLoop(); //Call main loop to start

    return 0;
}

bool InitializeWindow(HINSTANCE hInstance,
    int ShowWnd, int width, int height, bool fullscreen)
{
    //Check if fullscreen is needed and set to fullscreen if so
    if (fullscreen)
    {
        HMONITOR hmon = MonitorFromWindow(hwnd,
            MONITOR_DEFAULTTONEAREST);
        MONITORINFO mi = { sizeof(mi) };
        GetMonitorInfo(hmon, &mi);

        width = mi.rcMonitor.right - mi.rcMonitor.left;
        height = mi.rcMonitor.bottom - mi.rcMonitor.top;
    }

    //Window class structure
    WNDCLASSEX wc;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = NULL;
    wc.cbWndExtra = NULL;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 2);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = WindowName;
    wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

    //Registering the window class
    if (!RegisterClassEx(&wc))
    {
        MessageBox(NULL, L"Error registering class",
            L"Error", MB_OK | MB_ICONERROR);
        return false;
    }

    //Create the window with the now registered window class
    hwnd = CreateWindowEx(NULL,
        WindowName,
        WindowTitle,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        width, height,
        NULL,
        NULL,
        hInstance,
        NULL);

    //Return error msg if unsuccessful in getting a window handle
    if (!hwnd)
    {
        MessageBox(NULL, L"Error creating window",
            L"Error", MB_OK | MB_ICONERROR);
        return false;
    }

    //Removing the style from the window when fullscreen
    if (fullscreen)
    {
        SetWindowLong(hwnd, GWL_STYLE, 0);
    }

    //Showing and updating the window
    ShowWindow(hwnd, ShowWnd);
    UpdateWindow(hwnd);

    return true;
}

I've tried to change CreateWIndowEx to CreateWindowA and that's really the only thing that seemed to possibly have an effect but i did not see any results. I've tried printing the error but there is no error.

Creating a window in Windows with the C++ code so i can make a game engine.

Upvotes: 0

Views: 1525

Answers (1)

WhozCraig
WhozCraig

Reputation: 66244

Though shalt not abuse global variables.

You're calling DefWndProc in your WndProc with hwnd as the handle argument. But that isn't set until the return of CreateWindowEx, and there are plenty very important messages that are rifled through WndProc during window stand-up. Those messages need the window handle, and you're not passing it along. Instead you're just passing NULL (the startup value of hwnd).

Change all hwnd to hWnd (the parameter name) in your WndProc

The remainder of the problems mentioned in comments I leave for you to address.

Upvotes: 6

Related Questions