CreateWindow returns NULL and GetLastError returns 0

Started by
6 comments, last by Empirical 19 years, 8 months ago
The code:

ATOM WindowAtom = NULL;
HWND Window = NULL;

int main()
{
	WNDCLASSEX wndclassex;

	// open primary window
	ZeroMemory(&wndclassex, sizeof(WNDCLASSEX));
	wndclassex.cbSize = sizeof(WNDCLASSEX);
	wndclassex.style = CS_CLASSDC;
	wndclassex.lpfnWndProc = (WNDPROC)WindowProc;
	wndclassex.cbClsExtra = 0;
	wndclassex.cbWndExtra = 0;
	wndclassex.hInstance = (HINSTANCE)GetModuleHandle(NULL);
	wndclassex.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MAINICON));
	wndclassex.hCursor = LoadCursor((HINSTANCE)GetModuleHandle(NULL), MAKEINTRESOURCE(IDC_POINTER));
	wndclassex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	wndclassex.lpszMenuName = NULL;
	wndclassex.lpszClassName = CLASS_NAME;
	wndclassex.hIconSm = LoadIcon((HINSTANCE)GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MAINICON));
	WindowAtom = RegisterClassEx(&wndclassex);
	if (!WindowAtom)
	{
		printf("ERROR: !RegisterClassEx(...)\n");
		printf("ERROR: GetLastError() == %lu\n", GetLastError());
		goto exit;
	}
	Window = CreateWindow(CLASS_NAME, TITLE,
		WS_CAPTION | WS_SYSMENU,
		0, 0, 640, 480,
		NULL, NULL,
		(HINSTANCE)GetModuleHandle(NULL), NULL);
	if (!Window)
	{
		printf("ERROR: !CreateWindow(...)\n");
		printf("ERROR: GetLastError() == %lu\n", GetLastError());
		goto exit;
	}
	ShowWindow(Window, SW_NORMAL);
	UpdateWindow(Window);

	// message pump here

	// exit
	return 0;

exit:
	// wait for key press
	printf("\nHit any key to continue...");
	getch();
	return 0;
}

// window handler
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
	case WM_CLOSE:
		DestroyWindow(hWnd);
		return 0;
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}

	// not handled
	return DefWindowProc(hWnd, uMsg, lParam, wParam);
}


The output:

ERROR: !CreateWindow(...)
ERROR: GetLastError() == 0
I've done some testing and have found that RegisterClassEx doesn't fail, but GetLastError() after the call does return 2 (which has something to do with file not found). To see if GetLastError() was returning a previous value (not that it should) I had done a SetLastError(0), but GetLastError() still returned 2. I've tried comparing the code with other successful attempts, but I can't figure out what I'm doing wrong here. Thanks. [Edited by - bcome on July 29, 2004 4:24:03 PM]
<!-- ................................ -->u r what u bcomeMy (XHTML 1.0 Strict) Website
Advertisement
Post your WndProc() function.
Oops, edited the original.
// window handlerLRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){	switch (uMsg)	{	case WM_CLOSE:		DestroyWindow(hWnd);		return 0;	case WM_DESTROY:		PostQuitMessage(0);		return 0;	}	// not handled	return DefWindowProc(hWnd, uMsg, lParam, wParam);}
<!-- ................................ -->u r what u bcomeMy (XHTML 1.0 Strict) Website
Quote:Original post by bcome
Oops, edited the original.
// window handlerLRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){	switch (uMsg)	{	case WM_CLOSE:		DestroyWindow(hWnd);		return 0;	case WM_DESTROY:		PostQuitMessage(0);		return 0;	}	// not handled	return DefWindowProc(hWnd, uMsg, wParam, lParam);}


Oxyd
Oh, right, I'd messed up the WNDPROC method and forgot to change that. Thanks!!!
<!-- ................................ -->u r what u bcomeMy (XHTML 1.0 Strict) Website
Have you checked to make sure that hInstance, hIcon, hCursor, hbrBackground, and hIconSm are all non-NULL?
Probably you just need to call CreateWindowEx() instead of CreateWindow()

Edit: never mind - that shouldn't matter
I had the same problem. If i dont handle the WM_PAINT message the window refuses to be created. And returns error 0.

Also I have to at least call BeginPaint and EndPaint (Even if i dont draw anything)

This topic is closed to new replies.

Advertisement