Sign in to follow this  

Whats wrong with my HWND?

This topic is 4597 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

Whats wrong with my code? It looks fine to me, I put a break point after the CreateWindowEx function and the HWND is always NULL.
#include <windows.h>

WNDCLASSEX	g_wcex;
HWND		g_hWnd;
MSG		g_msg;
char	       *g_strClassName = "window";
char	       *g_strTitle     = "title";

LRESULT CALLBACK WndProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{

	g_wcex.cbClsExtra	= 0;
	g_wcex.cbSize		= sizeof(WNDCLASSEX);
	g_wcex.cbWndExtra	= 0;
	g_wcex.hbrBackground	= (HBRUSH)GetStockObject(0);
	g_wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	g_wcex.hIcon		= LoadIcon(NULL, IDI_APPLICATION);
	g_wcex.hIconSm		= LoadIcon(NULL, IDI_APPLICATION);
	g_wcex.hInstance	= hInstance;
	g_wcex.lpfnWndProc	= WndProc;
	g_wcex.lpszClassName	= g_strClassName;
	g_wcex.lpszMenuName	= NULL;
	g_wcex.style		= CS_CLASSDC;

	if(!RegisterClassEx(&g_wcex))
	{
		return false;
	}

	g_hWnd = CreateWindowEx(WS_EX_WINDOWEDGE, g_strClassName, g_strTitle,  WS_OVERLAPPEDWINDOW, 0, 0, 800, 600, NULL, NULL, hInstance, NULL);

	if(!g_hWnd)
	{
		return false;
	}

	UpdateWindow(g_hWnd);
	ShowWindow(g_hWnd, SW_NORMAL);

	ZeroMemory(&g_msg, sizeof(MSG));

	while(g_msg.message != WM_QUIT)
	{
		if(PeekMessage(&g_msg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&g_msg);
			DispatchMessage(&g_msg);
		}
	}

	UnregisterClass(g_strClassName, hInstance);

	return 0;

}

LRESULT CALLBACK WndProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{

	switch(iMsg)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
		break;
	default:
		return 0;
		break;
	}

	return DefWindowProc(hWnd, iMsg, wParam, lParam);

}


Share this post


Link to post
Share on other sites
I can't figure this out. I even went and made a new project. Oh by the way I am using VS2003. I went to new project, c++, Win32 Windows Application, then made it an empty application, used the same code and nothing.

Share this post


Link to post
Share on other sites
Why don't you ask it? ;] GetLastError()

btw:

DefWindowProc() never gets called because return 0 is default, you really want this instead.
I had a quick look in msdn & immediately saw this:
Quote:
The WM_NCCREATE message is sent just after your window is created, but if an application responds to this message by returning FALSE, CreateWindowEx function fails


If you're returning zero to EVERY message you're also returning zero to this so your window will ALWAYS fail.

only return zero for messages you know about & return DefWindowProc by default

Share this post


Link to post
Share on other sites
Is this return in your default case

default:
return 0;
break;



It keeps the default window proc from processing the WM_CREATE and other message you're not handling on the window create. Remove the return 0; and it will work.

Share this post


Link to post
Share on other sites
Thanks alot that was it, so it was never getting to the default procedure because it was returning in that default. I can't believe I did that.

Share this post


Link to post
Share on other sites
ProPuke: good advice but in this case it did not help.

The first thing I did was call GetLastError and FormatMessage and got the error string: "The operation completed sucessfully." :-)

Share this post


Link to post
Share on other sites

This topic is 4597 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this