• Advertisement
Sign in to follow this  

Help with CreateWindowEx

This topic is 3004 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I feel dumb posting this, but the usual suspects aren't working and MSDN hasn't been much help. I'm porting some code I wrote back in school with VS2003 to VS2008. I've narrowed the problem down to this code:
[source code=cpp]
#include <windows.h>

HWND hWnd;

int BuildWindow(HINSTANCE hInstance);
LRESULT CALLBACK WinProc(HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCommandLine, int nCommandShow)
{
	MSG msg;

	if (!BuildWindow(hInstance)) 
	{
		MessageBox(NULL, L"BuildWindow is FALSE", NULL, MB_OK);
		return 0;
	}

}

int BuildWindow(HINSTANCE hInstance)
{
	WNDCLASSEX wc;

	memset(&wc, 0, sizeof(WNDCLASSEX));

	
	wc.cbSize			= sizeof(WNDCLASSEX);
	wc.style			= CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc		= (WNDPROC)WinProc;
	wc.cbClsExtra		= 0;
	wc.cbWndExtra		= 0;
	wc.hInstance		= hInstance;
	wc.hIcon			= NULL;
	wc.hCursor			= NULL;
	wc.hbrBackground	= (HBRUSH)GetStockObject(BLACK_BRUSH);
	wc.lpszMenuName		= L"NULL";
	wc.lpszClassName	= L"Slider32";
	wc.hIconSm			= NULL;

	if (!RegisterClassEx(&wc)) {

		MessageBox(NULL, L"false here", NULL, MB_OK);
		return FALSE;
	}

	hWnd = CreateWindowEx(
		NULL,
		L"Slider 1.0",
		L"Slider 1.0",
		WS_VISIBLE,
		0,
		0,
		1024,
		768,
		NULL,
		NULL,
		hInstance,
		NULL);

	if (GetLastError()) {
		WCHAR sz[206];
		wsprintf(sz, L"le = %d", GetLastError());

		MessageBox(NULL, sz, NULL, MB_OK);
	}

	if (!hWnd) return FALSE;

	return TRUE;

}

I'm getting GetLastError equal to 1407 or ERROR_CANNOT_FIND_WND_CLASS. It seems that this is mostly due to an improper registration, but RegisterClassEx is not failing and I haven't found any info as to why. Any ideas?

Share this post


Link to post
Share on other sites
Advertisement
I notice one error right off the bat, you don't keep the value returned by RegisterClassEx and use it as the second parameter of CreateWindowEx, in other words you are registering the class then trying to create the window with an string instead of the registered class value.

I have found it's much easier to generate a custom window using RegisterClassEx then simply using CreateEx to activate it.

Share this post


Link to post
Share on other sites
You set the class name to Slider32, but when you create the window, you use Slider 1.0. Try changing Slider 1.0 (in the lpClassName parameter in your CreateWindowEx function) to Slider32.

Share this post


Link to post
Share on other sites
Yep, that was it. Spot on!

Man, I'm rusty ... None of my textbooks even show doing it that way and the oldest copyright is 2003.

[EDIT] Actually, I was being really stupid and didn't see the relationship between those two members. Thanks guys ... You rock as always!

Share this post


Link to post
Share on other sites
You should only call GetLastError if the function previously called indicates there was an error. That is, if CreateWindowEx passes it might have not called SetLastError meaning whatever error code is stored might not be for what you think it is for. Your code should look more like this (personal coding style changed to fit yours of course):
if (!RegisterClassEx(&wc)) {
WCHAR sz[206];
wsprintf(sz, L"RegisterClassEx: le = %d", GetLastError());

MessageBox(NULL, sz, NULL, MB_OK);
return FALSE;
}

hWnd = CreateWindowEx(
NULL,
L"Slider 1.0",
L"Slider 1.0",
WS_VISIBLE,
0,
0,
1024,
768,
NULL,
NULL,
hInstance,
NULL);
if (!hWnd)
{
WCHAR sz[206];
wsprintf(sz, L"CreateWindowEx: le = %d", GetLastError());

MessageBox(NULL, sz, NULL, MB_OK);
}

Share this post


Link to post
Share on other sites
Quote:
Original post by nobodynews
You should only call GetLastError if the function previously called indicates there was an error.


Aye, I did actually do that. Just copy/pasted it down the line some :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement