TangJinJian
TangJinJian

Reputation: 39

Why error 1400 Invalid window handle?

#include <stdio.h>
#include <stdbool.h>
#include <Windows.h>

HWND WindowHandle;
HINSTANCE Instance;
const wchar_t WindowClassName[] = L"Temp Projcet";

LRESULT CALLBACK WindowProc(HWND _windowHandle, UINT _msg, WPARAM _param, LPARAM _param1) {
    switch (_msg) {
    case WM_PAINT: {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(WindowHandle, &ps);
        FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));
        EndPaint(WindowHandle, &ps);
    } break;
    case WM_CLOSE: {
        if (IDOK == MessageBoxW(WindowHandle, L"Quit?", L"My application", MB_OKCANCEL)) {
            DestroyWindow(WindowHandle);
        }
        return false;
    } break;
    case WM_DESTROY: {
        PostQuitMessage(0);
    } break;
    default:
        break;
    }
    return DefWindowProcW(WindowHandle, _msg, _param, _param1);
}

ATOM RegisterWindowClass(void) {
    WNDCLASS wc = { 0 };
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = Instance;
    wc.lpszClassName = WindowClassName;
    return RegisterClassW(&wc);
}

int APIENTRY wWinMain(HINSTANCE _instance, HINSTANCE _prevInstance, PWSTR _cmdLine, int _cmdShow) {
    Instance = _instance;
    RegisterWindowClass();

    WindowHandle = CreateWindowExW(
        0,
        WindowClassName,
        L"This a window",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
        NULL,
        NULL,
        Instance,
        NULL
    );

    DWORD err = GetLastError();
    // Why error 1400 Invalid window handle?

    MSG msg = { 0 };
    while (GetMessageW(&msg, WindowHandle, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessageW(&msg);
    }

    return 0;
}

Why does it return 1400 invalid handles when you create a window? Shouldn't it?There's no window handle in the create window function, okay? How could it be an invalid handle? I've been searching through the search engine for a long time, but still can't solve this problem? Why does CreateWindowExW() return 1400? Code in line 44.

Upvotes: 3

Views: 8851

Answers (3)

Qaz
Qaz

Reputation: 61920

You're using WindowHandle in the window procedure before it's been set to the return value of CreateWindowEx. Part of that CreateWindowEx call is calling the window procedure with WM_NCCREATE and WM_CREATE. Your window procedure calls DefWindowProc with a null handle at that point.

The simple solution here is to use the _windowHandle parameter instead of your WindowHandle global.

Also note that in order for your window to be visible, you'll need a call to ShowWindow. In addition, the quit message you post isn't specific to that window, so your GetMessage call will not retrieve it and the application won't end.

Upvotes: 5

SoronelHaetir
SoronelHaetir

Reputation: 15162

Only call GetLastError after a failed CreateWindow (that is, if CreateWindow returns NULL).

the last error value is unspecified after the vast majority of successful windows API calls, you must check the return value before calling GetLastError.

Upvotes: 0

user2100815
user2100815

Reputation:

Here

DWORD err = GetLastError();

you haven't tested if there has actually been an error. You need to test the handle you get back from CreateWindowEx before you call GetLastError. Otherwise it will return some previous, unrelated error, or some random value.

Upvotes: 0

Related Questions