CreateWindow失败,错误50(不支持该请求。)

标签 c windows winapi createwindow

我在这里做错了什么,但错误消息没有给我任何线索。对 CreateWindow 的调用总是失败(返回 NULL,并且 GetLastError() 返回 50)。我想要的只是一个简单的空白窗口,但显然我的请求“不支持”。

// gcc basic.c -o basic.exe -mwindows
#include <stdio.h>
#include <windows.h>

// Function prototypes.
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
BOOL InitApplication(HINSTANCE);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);

// Application entry point.
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    MSG msg;
    if (!InitApplication(hinstance))
        return FALSE;

    if (!InitInstance(hinstance, nCmdShow))
        return FALSE;

    BOOL fGotMessage;
    while ((fGotMessage = GetMessage(&msg, (HWND) NULL, 0, 0)) != 0 && fGotMessage != -1)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}

BOOL InitApplication(HINSTANCE hinstance)
{
    WNDCLASSEX wcx;
    wcx.cbSize = sizeof (wcx);          // size of structure
    wcx.style = CS_HREDRAW | CS_VREDRAW;
    wcx.lpfnWndProc = MainWndProc;     // points to window procedure
    wcx.cbClsExtra = 0;                // no extra class memory
    wcx.cbWndExtra = 0;                // no extra window memory
    wcx.hInstance = hinstance;         // handle to instance
    wcx.hIcon = NULL;
    wcx.hCursor = NULL;
    wcx.hbrBackground = GetStockObject(WHITE_BRUSH);                  // white background brush
    wcx.lpszMenuName =  NULL;    // name of menu resource
    wcx.lpszClassName = "MainWClass";  // name of window class
    wcx.hIconSm = NULL; // small class icon
    return RegisterClassEx(&wcx);
}

BOOL InitInstance(HINSTANCE hinstance, int nCmdShow)
{
    HWND hwnd;

    // Create the main window.
    hwnd = CreateWindow(
        "MainWClass",        // name of window class
        "Sample",            // title-bar string
        WS_OVERLAPPEDWINDOW, // top-level window
        CW_USEDEFAULT,       // default horizontal position
        CW_USEDEFAULT,       // default vertical position
        CW_USEDEFAULT,       // default width
        CW_USEDEFAULT,       // default height
        (HWND) NULL,         // no owner window
        (HMENU) NULL,
        hinstance,           // handle to application instance
        (LPVOID) NULL);      // no window-creation data

    if (!hwnd) {
        int error_code = GetLastError();
        char caption[256];
        snprintf(caption, sizeof caption, "CreateWindow: error %d", error_code);
        MessageBox(NULL, caption, "CreateWindow error", MB_OK);
        return FALSE;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
    return TRUE;
}

LRESULT CALLBACK
MainWndProc(HWND hwnd, UINT uMsg, WPARAM wparam, LPARAM lparam)
{
    switch (uMsg) {
    case WM_CLOSE:
        DestroyWindow(hwnd);
        return 1;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        DefWindowProc(hwnd, uMsg, wparam, lparam);
    }
    return 0;
}

最佳答案

您的窗口过程已损坏。而不是

DefWindowProc(hwnd, uMsg, wparam, lparam);

你必须写

return DefWindowProc(hwnd, uMsg, wparam, lparam);

此外,对于 WM_CLOSE 文档说:

If an application processes this message, it should return zero.

您不遵守该规则。

关于CreateWindow失败,错误50(不支持该请求。),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43515611/

相关文章:

c - 如何获取Windows中的用户数?

所有平台上的java

java - 在 Windows 上设置 hadoop.tmp.dir 会出现错误 : URI has an authority component

c++ - 设置 std::threads 的线程亲和性

c++ - win32 API在图片控件C++中打开jpg

c - 如何正确链接程序与 libperl.so

c - 链接libsrtp问题

windows - 如何批量获取子字符串在字符串中的位置

c++ - ESENT 二级索引已损坏,必须对数据库进行碎片整理

c - 确定 C 缓冲区中的多个空格分隔的输入