Public Group

Win32 Crashing on CreateWindow

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

Recommended Posts

Here is my window class registering and window creating code:
bool Initialize(HINSTANCE hInstance, int nCmdShow)
{
//Create new window class.
WNDCLASSEX wc;
wc.cbClsExtra = 0;
wc.cbSize = sizeof(WNDCLASSEX);
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)(WHITE_BRUSH);
wc.hIconSm = NULL;
wc.hInstance = hInstance;
wc.lpfnWndProc = WinProc;
wc.lpszClassName = L"WindowClass";
wc.style = CS_HREDRAW | CS_VREDRAW;

//Register the window class.
if(!RegisterClassEx(&wc))
return false;

HWND hWnd = CreateWindow(
L"WindowClass",
L"My Win32 Window",
WS_OVERLAPPED,
0,
0,
800,
600,
NULL,
NULL,
hInstance,
NULL);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
if(!Initialize(hInstance, nCmdShow))
{
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}


It compiles fine, but when I run it, it breaks on the "CreateWindow" call, with this error: Unhandled exception at 0x7c910370 in DirectX Application.exe: 0xC0000005: Access violation reading location 0xcccccccc. I have no idea what the problem is. I've compiled Win32 applications just fine before. I'm guessing it has something to do with me trying to contain that code in "Initialize()", because it worked fine when I didn't put code in there.

Share on other sites
You should try CreateWindowEx instead of CreateWindow.

Share on other sites
In debug mode, uninitialized local variables are set to 0xCCCCCCCC.

When you declare WNDCLASSEX, some local variables, i.e. lpszMenuName, hIcon, hCursor, etc are set to this value, causing the call to crash.

You can either explicitly set them to NULL, or use ZeroMemory() like so:
WNDCLASSEX wc;ZeroMemory( &wc, sizeof(WNDCLASSEX) );// rest of code here

Hope that helps.

Share on other sites
I'm not sure what the problem is, but I'm surprised it compiles. Not all paths in your Initialize function lead to a return value.

Share on other sites
That was it, I needed to ZeroMemory the structure.

Both CreateWindow and CreateWindowEx work fine. What's the difference?

And, what does the "Ex" stand for in all the Win32 stuff that ends in Ex?

Share on other sites
You might want to return the HWND instead of bool. It also appears that int nCmdShow doesn't serve any purpose.

Share on other sites
'Ex' means extended.

CreateWindowEx and CreateWindow are almost identical. CreateWindowEx allows you to use an extended window style, that is all - the parameter listing is the same.

If a Windows function ends with 'Ex', it usually just allows you more (or different, or 'ex'tended parameters), which can make the function potentially more useful (depending on what you use it for).

For example, if you are just creating a basic popup window, there is not really any need to use CreateWindowEx, but if you need some of the more advanced window styles, then it lets you do that.

Share on other sites
Edit: Doh, beaten to it!

I think it means "Extended". Looking at the MSDN documentation, the WNDCLASSEX structure includes two extra members compared to WNDCLASS.

Oh and just to clarify what I meant about the return values:

bool Initialize(HINSTANCE hInstance, int nCmdShow){	//Create new window class.	***snip*** 	//Register the window class.	if(!RegisterClassEx(&wc))		return false; 	HWND hWnd = CreateWindow(		L"WindowClass",		L"My Win32 Window",		WS_OVERLAPPED,		0,		0,		800,		600,		NULL,		NULL,		hInstance,		NULL);	if (NULL == hWnd)		return false;	// If we get this far, the function succeeded.	return true;}

Hope that helps.

Share on other sites
Quote:
 Original post by UphoreumBoth CreateWindow and CreateWindowEx work fine. What's the difference?

CreateWindow is a macro that wraps CreateWindowEx. Back in the days of 16 bit windows there was an actual CreateWindow function. When 32 bit Windows came out, they crafted macro that wrapped CreateWindowEx so that it would take less work to update 16 bit aps for 32 bit windows. Why they chose a macro instead of a function I don't know. The CreateWindow macro supplies a 0 as the first argument to CreateWindowEx. The rest of the arguments are the same.

Quote:
 Original post by UphoreumAnd, what does the "Ex" stand for in all the Win32 stuff that ends in Ex?

Ex -> extended

The Ex functions are typically newer and provide more options. The non-Ex functions are maintained for backwards compatibility (so old programs don't break). Many non-Ex functions wrap calls to their Ex companions.

Share on other sites
My original post was slightly incorrect, but my internet died as I tried to change it, sorry.
Quote:
 Original post by jpbbCreateWindowEx and CreateWindow are almost identical. CreateWindowEx allows you to use an extended window style, that is all - the parameter listing is the same.

The parameter listing is not the same - CreateWindowEx has one extra parameter, dwExstyle which is the extended window style.

See the MSDN documentation for the full function prototype.

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 14
• 45
• 22
• 27
• Forum Statistics

• Total Topics
634042
• Total Posts
3015204
×