I have a problem with this simple Win32 application I am trying to make. As you can tell by the title, the function CreateWindow() always returns 0, which indicates failure. Here is my code.

int MessageLoop()
{
	MSG msg;
	ZeroMemory(&msg, sizeof(MSG));

	while (GetMessage(&msg, 0, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

return msg.wParam;
}

INT WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance, 
                   LPSTR lpCmdLine,
                   int nShowCmd)
{
	if (!MakeAWindow(hInstance, nShowCmd))
	{
		MessageBox(0, "Window Creation -- Failed", "Error", MB_OK);
		return 0;
	}

return MessageLoop();

}

bool MakeAWindow(HINSTANCE appInstance,int show)
{

	WNDCLASS wc;

	wc.style         = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc   = WndProc;
	wc.cbClsExtra    = 0;
	wc.cbWndExtra    = 0;
	wc.hInstance     = appInstance;
	wc.hIcon         = LoadIcon(0,IDI_APPLICATION);
	wc.hCursor       = LoadCursor(0,IDC_ARROW);
	wc.hbrBackground = static_cast<HBRUSH>(GetStockObject(BLACK_BRUSH));
	wc.lpszMenuName  = 0;
	wc.lpszClassName = "wc";

	if (!RegisterClass(&wc))
	{
		MessageBox(0, "Failed to register the window class", "Windows Sucks!", 0);
		return false;
	}
	
	MainWindow = CreateWindow("wc",
                                  "Hello",
                                  WS_OVERLAPPEDWINDOW,
				  CW_USEDEFAULT,
				  CW_USEDEFAULT,
				  CW_USEDEFAULT,
				  CW_USEDEFAULT,
				  0,
				  0,
				  appInstance,
				  0);

	if(MainWindow == NULL)
	{
		::MessageBox(0,"Function CreateWindow() Failed",0,0);
		return false;
	}

	::ShowWindow(MainWindow, show);
	::UpdateWindow(MainWindow);

	return true;
}

LRESULT CALLBACK WndProc(HWND window,UINT msg,WPARAM wParam,LPARAM lParam)
{
	switch(msg)
	{
	
	case WM_KEYDOWN:

			if (wParam == VK_ESCAPE )
			{
				::DestroyWindow(MainWindow);
				return 0;
			}
		
	case WM_DESTROY:
			::PostQuitMessage(0);
			return 0;

	}

	return DefWindowProc(MainWindow, msg, wParam, lParam);

}

Does anyone have any ideas as to why this will not work? I have searched for a while and cannot think of anything. I need some new eyes on this code.

Have you read this tutorial?

I used his code, and still, my only problem is the failure of CreateWindow(), or CreateWindowEx(), rather.

is MainWindow a global handle??? else you have to declare it before using it like this

HWND MainWindow

in WndProc procedure, what window handle is DestroyWindow() referring??? i suppose it should be like this

DestroyWindow(window)

The problem is your WinProc() is incorrect. Change it to the one shown in that tutorial link I posted and your program works.

The problem is your WinProc() is incorrect. Change it to the one shown in that tutorial link I posted and your program works.

MSDN said something about this function interacting with the window procedure, I just wans't looking hard enough in my code. Thank you so much to everyone! I'm back on track to making games now!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.