Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Windows
#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 25-May-13 5:42am
Comments
Volynsky Alex at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try replacing "
DefWindowProc(hWnd,uMsg,wParam,lParam);
with
return DefWindowProc(hWnd,uMsg,wParam,lParam);
(or set a LRESULT value and return that at the end of the WinProc method)
 
Regards,
Ian.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

WinProc should return the value from DefWindowProc.
 
Please modify default case in WinProc
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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 195
1 ProgramFOX 130
2 Maciej Los 105
3 Sergey Alexandrovich Kryukov 105
4 Afzaal Ahmad Zeeshan 82
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 25 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100