[C++] CreateWindowEx() failed...

Started by
7 comments, last by emiel1 15 years, 9 months ago
Hi, I've created code to register a window class and make a window out of it. CreateWindowEx() Failed with error code (GetLastError()) 0 (ERROR_SUCCESS); What am I doing wrong?
	WNDCLASSEX wc;
	ZeroMemory(&wc, sizeof(WNDCLASSEX));

	wc.cbSize = sizeof(WNDCLASSEX);
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = (WNDPROC)WindowProc;
	wc.hInstance = GetModuleHandle(NULL);
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH) COLOR_WINDOW;
	wc.lpszClassName = TEXT("WindowClass");

	RegisterClassEx(&wc);

	FullScreen = false;//CSettings::Get().GetIntSetting("FullScreen") == 1;
	GameHeigth = 800;//CSettings::Get().GetIntSetting("GameHeigth");
	GameWidth = 600;//CSettings::Get().GetIntSetting("GameWidth");

	if (FullScreen)
	{
		hWnd = CreateWindowEx(NULL, 
							  TEXT("WindowClass"), 
							  TEXT("Window"), 
							  WS_POPUP | WS_EX_TOPMOST,
							  0, 0, 
							  GameWidth, 
							  GameHeigth,
							  NULL, 
							  NULL, 
							  GetModuleHandle(NULL), 
							  NULL);
	}
	else
	{
		hWnd = CreateWindowEx(NULL, 
							  TEXT("WindowClass"), 
							  TEXT("Window"), 
							  WS_POPUP, 
							  0, //(GetSystemMetrics(SM_CXSCREEN) - GameWidth) / 2, 
							  0, //(GetSystemMetrics(SM_CYSCREEN) - GameHeigth) / 2, 
							  GameWidth, 
							  GameHeigth, 
							  NULL, 
							  NULL, 
							  GetModuleHandle(NULL), 
							  NULL);
	}

	if (hWnd == NULL)
	{
		DWORD error = GetLastError();
		std::stringstream message; message << "CreateWindowEx() Failed... - Error Code = " << error;
		Log_L1(T_ERROR, message.str());
		return false;
	}

	ShowWindow(hWnd, SW_SHOW);

Thanks in advance... Emiel1 [Edited by - emiel1 on July 26, 2008 10:10:22 AM]
Advertisement
Quote:Original post by emiel1
	WNDCLASSEX wc;...	wc.lpfnWndProc = (WNDPROC)WindowProc;...


Emiel1


Never cast a function pointer. If a assignment wont work then the signature of you WindowProc is wrong. The rest look Ok so far.
Quote:Original post by megamoscha
Never cast a function pointer. If a assignment wont work then the signature of you WindowProc is wrong. The rest look Ok so far.


Hi megamoscha,

Done, still the same error. My changed code is now:

	wc.lpfnWndProc = /*(WNDPROC)*/WindowProc;


Signature of WindowProc =

LRESULT CALLBACK WindowProc(HWND hWnd,			    UINT message,			    WPARAM wParam,			    LPARAM lParam);


Emiel1

[Edited by - emiel1 on July 26, 2008 10:03:06 AM]
Check the return value of RegisterClassEx(), and GetLastError() after that function if necessary.
Ra
Quote:Original post by Ra
Check the return value of RegisterClassEx(), and GetLastError() after that function if necessary.


Hi Ra,

New Code

	if (RegisterClassEx(&wc) == NULL)	{		DWORD error = GetLastError();		std::stringstream message; message << "RegisterClassEx() Failed... - Error Code = " << error;		Log_L1(T_ERROR, message.str());		return false;	}


No difference: RegisterClassEx() runned and returned no error.

Emiel1

[Edited by - emiel1 on July 26, 2008 10:28:15 AM]
Post your WindowProc hanlder also. I suspect that your WindowProc hanlder just returns zero for (almost) everything at the moment, including the initial WM_NCCREATE message. That message is sent while creating the window, and if the hander returns zero from it, the creation of the window is aborted and no handle is returned. This is, as far as I know, not an error, hence GetLastError not indicating an error.
Another thing you can try is passing in DefWindowProc instead of your window hander as the lpfnWndProc member of the WNDCLASSEX struct.
Your also missing a few things out from your WNDCLASSEX structure, try filling all the structure, I remember CreateWindowEx failing for me when I didn't set hIcon:

wc.cbSize = sizeof(WNDCLASSEX);wc.style = CS_HREDRAW | CS_VREDRAW;wc.lpfnWndProc = WindowProc;wc.cbClsExtra = wc.cbWndExtra = 0;wc.hInstance = GetModuleHandle(NULL);wc.hIcon = wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);wc.hCursor = LoadCursor(NULL, IDC_ARROW);wc.lpszClassName = TEXT("WindowClass");wc.lpszMenuName = NULL;wc.hbrBackground = (HBRUSH) COLOR_WINDOW;
Quote:Original post by Brother Bob
Post your WindowProc hanlder also. I suspect that your WindowProc hanlder just returns zero for (almost) everything at the moment, including the initial WM_NCCREATE message. That message is sent while creating the window, and if the hander returns zero from it, the creation of the window is aborted and no handle is returned. This is, as far as I know, not an error, hence GetLastError not indicating an error.


Thanks Brother Bob,

I implemented an pseudo WindowProc:

LRESULT CALLBACK WindowProc(HWND hWnd,			 UINT message,			 WPARAM wParam,			 LPARAM lParam){	return 0;}


Now I have:

LRESULT CALLBACK WindowProc(HWND hWnd, 			 UINT message, 			 WPARAM wParam, 			 LPARAM lParam){	switch (message)	{	default:		return DefWindowProc (hWnd, message, wParam, lParam);	}}


(Switch in case of futher sorting of messages)

Noobc:

I'll do that from now on everywhere. It's always nice to fill in all the data, allthough setting vars to 0 is unneeded (because of ZeroMemory())

Now my program works perfect. Thanks all for your help.

Emiel1

This topic is closed to new replies.

Advertisement