Click here to Skip to main content
15,882,163 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C++
#include <Windows.h>

LRESULT CALLBACK WinProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam);

const LPWSTR szClassName = L"ClassName"; 
const LPWSTR szTitle = L"Window Tile"; 
int HEIGHT = 600;
int WIDTH = 800;


int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
	WNDCLASSEX wc;
	HWND hWnd = NULL;
	MSG msg = {0};
	wc.cbSize = sizeof ( WNDCLASSEX);
	wc.hInstance = hInstance;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hbrBackground = (HBRUSH) GetStockObject( NULL_BRUSH );
	wc.hCursor = LoadCursor ( hInstance, IDC_CROSS );
	wc.hIcon = LoadIcon ( NULL, MAKEINTRESOURCE(IDI_APPLICATION) );
	wc.hIconSm = LoadIcon ( NULL, MAKEINTRESOURCE(IDI_WINLOGO) );
	wc.lpfnWndProc = WinProc;
	wc.lpszClassName = szClassName;
	wc.lpszMenuName = NULL;
	wc.style = CS_HREDRAW | CS_VREDRAW;

	if ( !RegisterClassEx( &wc ) )
		{
        MessageBox(NULL,
            L"Class Registration Failed",
			L"Error:1",
            NULL);

        return 1;
    }

	hWnd = CreateWindow(
        szClassName,
        szTitle,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        500, 100,
        NULL,
        NULL,
        hInstance,
        NULL
    );

	if (!hWnd)
    {
        MessageBox(NULL,
            L"Call to CreateWindow failed!",
			L"Error: 2",
            NULL);

        return 1;
    }
	
	ShowWindow(hWnd,nCmdShow);
	UpdateWindow(hWnd);

	while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
	return (int)msg.wParam;
}
LRESULT CALLBACK WinProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
	switch( uMsg )
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
		break;
	case WM_CHAR:
			switch( wParam )
			{
			case VK_ESCAPE:
				PostQuitMessage(0);
				return 0;
				break;
			}
	default:
		DefWindowProc(hWnd,uMsg,wParam,lParam);

	}
	return 0;
}

Ok, now when I debug, it fails to create the window, I know this is a nooby one but I really have no idea how to solve it, tryed to copy the CreateWindow() function from msdn(didn't help), so maybe its in WNDCLASSEX structure (didn't get any error when registering it..) , ... Anyone has any idea?
Posted
Comments
Volynsky Alex 25-May-13 11:41am    
When an error occurs, most system functions return an error code, usually 0, NULL, or –1. Many system functions also set an additional error code called the last-error code. This error code is maintained separately for each running thread; an error in one thread does not overwrite the last-error code in another thread.

An application can retrieve the last-error code by using the GetLastError function; the error code may tell more about what actually occurred to make the function fail.

So, after following row : hWnd = CreateWindow(....);
you can add something like:
printf("hWnd = %.8x; GetLastError=%d\n", hWnd, GetLastError());

For more information, see WinError.h and System Error Codes.

Regards,
Alex

Try replacing "
C++
DefWindowProc(hWnd,uMsg,wParam,lParam);
with
C#
return DefWindowProc(hWnd,uMsg,wParam,lParam);
(or set a LRESULT value and return that at the end of the WinProc method)

Regards,
Ian.
 
Share this answer
 
WinProc should return the value from DefWindowProc.

Please modify default case in WinProc
C#
default:
        return DefWindowProc(hWnd,uMsg,wParam,lParam);

Here Window Procedure is not returning the value from default handler, and always returns 0. It created the error.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900