Jump to content
  • Advertisement
Sign in to follow this  
argonaut

RegisterClassEx problem

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

Heya, I posted a similar issue a few days ago, but this one just started happening during a refactor and again MSDN isn't giving me any help. I don't think I changed anything affecting this part of the code, but I must have. I'm getting the error ERROR_FILE_NOT_FOUND when I try to register my class. This doesn't make sense to me, since I'm not trying to do any file stuff. Any Windows programmers know what's the poop here:
#ifndef _WINMAIN_H_
#define _WINMAIN_H_

#include "Globals.h"

#include "GameEngine.h"
#include "debug.h"

#include <windows.h>



GLOBAL PGAMEENGINE	g_Engine;
GLOBAL HWND			g_hWnd;

BOOL 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;

	g_hWnd = (HWND)GlobalAlloc(NULL, sizeof(HWND));

	if (!BuildWindow(hInstance)) 
	{
		MsgBxLastError(26);
		return 0;
	}

	g_Engine = new GameEngine(g_hWnd);

	while (true)
	{
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

		//if (!RunGame(g_hWnd)) return 0;
				
	}

	return 0;

}

BOOL 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;

	ATOM am;
	am = RegisterClassEx(&wc);

	// Getting Error ERROR_FILE_NOT_FOUND
	if (GetLastError()) MsgBxLastError(70);

	g_hWnd = CreateWindowEx(
		NULL,
		L"Slider32",
		L"Slider32",
		WS_VISIBLE,
		0,
		0,
		SCREEN_WIDTH,
		SCREEN_HEIGHT,
		NULL,
		NULL,
		hInstance,
		NULL);

	if (GetLastError()) {
		MsgBxLastError(1);
	}

	if (!g_hWnd) return FALSE;

	return TRUE;

}

LRESULT CALLBACK WinProc(HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam)
{

	switch (uMessage) 
	{
		case WM_DESTROY:
			break;
		case WM_LBUTTONDOWN:
			break;
		case WM_CHAR:
			break;
		default:
			break;
	}

	return DefWindowProc(hWnd, uMessage, wParam, lParam);
}


#endif



As always, thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement
There is nothing wrong with that part of the code, but rather with your error checking. The Windows API functions aren't guaranteed to set the error code returned from GetLastError to zero if they complete correctly, and therefore if(GetLastError()) is not a correct way to see if an error has occured. In your case the window class registration succeeds, but the error code says something else (it might be because you set the menu-name to L"NULL" instead of just NULL, if the function tries to find a menu called "NULL").

The correct way to check for errors is to check the return code, that is am. So you would do it like this:

if(am == 0) {
Error(GetLastError());
}


The same is necessary for CreateWindowEx and other functions. Read the documentation on MSDN under Return Value to see what they return on failure, and only if they return failure call GetLastError to find out what actually went wrong.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!