failed to call RegisterClassEx

Hi, all, Please help me out.
I run the following code. The compiling was right, neither error nor waring. However, it threw error box "failed: GetLastError returned 87".
It seems it failed to call RegisterClaserEx function.

I had tried WNDCLASS and RegisterClass functions,removed the two lines "wndclass.cbSize=sizeof(WNDCLASS);" and "wndclass.hIconSm = LoadIcon( wndclass.hInstance, MAKEINTRESOURCE( IDI_APPLICATION ));". This time I got the created window.

I don't know Why the RegisterClassEx function, is there anything wrong? Please help me! Please! Thanks for your attention and patience.



#include <windows.h>
#include<stdio.h>
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst,LPSTR lpszCmdLine,int nCmdShow)
{
HWND hwnd ;
MSG Msg ;
WNDCLASSEX wndclass ;
char lpszClassName[] = Example";
char lpszTitle[]= "My_Windows";

wndclass.cbSize=sizeof(WNDCLASS);
wndclass.style = 0;
wndclass.lpfnWndProc = (WNDPROC)WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon( NULL, IDI_APPLICATION) ;

wndclass.hCursor = LoadCursor( NULL,IDC_ARROW);
wndclass.hbrBackground =(HBRUSH) GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = lpszClassName;
wndclass.hIconSm = LoadIcon( wndclass.hInstance, MAKEINTRESOURCE( IDI_APPLICATION ));

if( !RegisterClassEx( &wndclass))
{
CHAR szBuf[80];
DWORD dw = GetLastError();

sprintf(szBuf, "failed: GetLastError returned %u\n", dw);
MessageBox(NULL, szBuf, "Error", MB_OK);
ExitProcess(dw);
}

hwnd=CreateWindow(
lpszClassName,
lpszTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL) ;

if(!hwnd)
{
MessageBox( NULL, "failed!", "create window", NULL ) ;
return 1;
}

ShowWindow( hwnd, nCmdShow);
UpdateWindow(hwnd);
while( GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage( &Msg) ;
DispatchMessage( &Msg) ;
}
return Msg.wParam;
}


LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
PostQuitMessage(0);
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
return (0);
}
Last edited on
Error between the two lines


1
2
3
4
5
6
7
8
 WNDCLASSEX wndclass ; //This line says WNDCLASSEX 
    char lpszClassName[] = "Example";
    char lpszTitle[]= "My_Windows";
    wndclass.cbSize=sizeof(WNDCLASS); //This line says WNDCLASS
    wndclass.style = 0;
    wndclass.lpfnWndProc = (WNDPROC)WndProc ;
    wndclass.cbClsExtra = 0 ;
    wndclass.cbWndExtra = 0



1
2
wndclass.hIcon = LoadIcon( NULL, IDI_APPLICATION) ;
wndclass.hIconSm = LoadIcon( wndclass.hInstance, MAKEINTRESOURCE( IDI_APPLICATION )); //why don't you do this the same way you did hIcon 
Topic archived. No new replies allowed.