开发者

Why am I getting error code 87 (Invalid parameter) from RegisterClassEx?

开发者 https://www.devze.com 2023-03-31 05:27 出处:网络
Here\'s the code #include <windows.h> const wchar_t g_szClassName[] = L\"myWindowClass\"; // Step开发者_运维知识库 4: the Window Procedure

Here's the code

#include <windows.h>
const wchar_t g_szClassName[] = L"myWindowClass";
// Step开发者_运维知识库 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{/*...*/
    return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;
    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, L"Window Registration Failed!", L"Error!",
        MB_ICONEXCLAMATION | MB_OK);
    return 0;
    }
    // Step 2: Creating the Window...
    return Msg.wParam;
}

This code is straight from Forgers Win32 Tutorial (with L"" and wchar_t where needed). Yet I can't make it work on WinXP SP3 x86 and VC2008Express SP1.


You didn't set style member, for instance (taken from wizard created code):

wc.style = CS_HREDRAW | CS_VREDRAW;


After the declaration of WNDCLASSEX wc; the memory will just contain random values.

You can do this

WNDCLASSEX wc = { 0 };

or you can do that

ZeroMemory(&wc, sizeof(WNDCLASSEX));

to ensure that every field is set to at least zero.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号