Skip to main content
GameDev.net gamedev.net
🔒 Locked

Simple Code = huge prob

Started by Nikita21 Sep 20, 2006 at 11:17 AM 7 replies 1.1k views
Original Post
Nikita21
Nikita21
I have tryed to make a reguler exemple window. however i keep falling for my own error that sais, that if the window doesnt equal to window type defination dont register it, and also give error for it... i have no idea what`s wrong here, i am looking at this code for days and i am at a dead end...


//Mainwindow.cpp
//Main window defination


#define WIN32_LEAN_AND_MEAN
#define WINDOW_CLASS_NAME "WINCLASS1"
#include <windows.h>
#include <windowsx.h>
#include <stdio.h>
#include <math.h>



LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{

	switch(msg)
	{
	case WM_CREATE:
		MessageBox(NULL, "Error Text",
		"Header Text",
		MB_OK);
		break;
	case VK_ESCAPE:
	case WM_DESTROY:
	case WM_QUIT:
		PostQuitMessage(0);
		break;

	default:break;


	}

return DefWindowProc(hwnd, msg, wparam, lparam);
}



int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow)
{
	WNDCLASSEX illawind;
	HWND hwnd;
	MSG msg;


illawind.lpfnWndProc = WindowProc;
illawind.cbClsExtra = sizeof(WNDCLASSEX);
illawind.hIcon = LoadIcon(NULL, IDI_WINLOGO);
illawind.hInstance = hinstance;
illawind.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
illawind.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
illawind.hCursor = LoadCursor(NULL, IDC_ARROW);
illawind.cbClsExtra = 0;
illawind.cbWndExtra = 0;
illawind.lpszMenuName = NULL;
illawind.lpszClassName = WINDOW_CLASS_NAME;
illawind.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;

if(!RegisterClassEx(&illawind))
{
	MessageBox(NULL,
		       "Unable To register window",
			   "Illa Error #1",
			   MB_OK);
}
return (0);


if(!(hwnd = CreateWindowEx(NULL,
          "Ground Zero",
		  "Ground Zero",
		  WS_OVERLAPPEDWINDOW | WS_VISIBLE,
		  0, 0,
		  400, 400,
		  NULL,
		  NULL,
		  hinstance,
		  NULL)))
		  return (0);


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

return (msg.wParam);


}


reana1
reana1
You can use source tags to make your code a little more viewable:
//Mainwindow.cpp//Main window defination#define WIN32_LEAN_AND_MEAN#define WINDOW_CLASS_NAME "WINCLASS1"#include <windows.h>#include <windowsx.h>#include <stdio.h>#include <math.h>LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam){	switch(msg)	{	case WM_CREATE:		MessageBox(NULL, "Error Text",		"Header Text",		MB_OK);		break;	case VK_ESCAPE:	case WM_DESTROY:	case WM_QUIT:		PostQuitMessage(0);		break;	default:break;	}return DefWindowProc(hwnd, msg, wparam, lparam);}int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow){	WNDCLASSEX illawind;	HWND hwnd;	MSG msg;illawind.lpfnWndProc = WindowProc;illawind.cbClsExtra = sizeof(WNDCLASSEX);illawind.hIcon = LoadIcon(NULL, IDI_WINLOGO);illawind.hInstance = hinstance;illawind.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);illawind.hIconSm = LoadIcon(NULL, IDI_WINLOGO);illawind.hCursor = LoadCursor(NULL, IDC_ARROW);illawind.cbClsExtra = 0;illawind.cbWndExtra = 0;illawind.lpszMenuName = NULL;illawind.lpszClassName = WINDOW_CLASS_NAME;illawind.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;if(!RegisterClassEx(&illawind)){	MessageBox(NULL,		       "Unable To register window",			   "Illa Error #1",			   MB_OK);}return (0);if(!(hwnd = CreateWindowEx(NULL,          "Ground Zero",		  "Ground Zero",		  WS_OVERLAPPEDWINDOW | WS_VISIBLE,		  0, 0,		  400, 400,		  NULL,		  NULL,		  hinstance,		  NULL)))		  return (0);while(GetMessage(&msg, NULL, 0, 0)){	TranslateMessage(&msg);	DispatchMessage(&msg);}return (msg.wParam);
Tadd- WarbleWare
Nytegard
Nytegard
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow){WNDCLASSEX illawind;HWND hwnd;MSG msg;illawind.lpfnWndProc = WindowProc;illawind.cbClsExtra = sizeof(WNDCLASSEX); // This line is your problemillawind.hIcon = LoadIcon(NULL, IDI_WINLOGO);illawind.hInstance = hinstance;illawind.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);illawind.hIconSm = LoadIcon(NULL, IDI_WINLOGO);illawind.hCursor = LoadCursor(NULL, IDC_ARROW);illawind.cbClsExtra = 0;illawind.cbWndExtra = 0;illawind.lpszMenuName = NULL;illawind.lpszClassName = WINDOW_CLASS_NAME;illawind.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;


See how you have 2 cbClsExtra? You're overwriting that parameter, and not defining the size of the structure.

illawind.cbClsExtra = sizeof(WNDCLASSEX);

should read

illawind.cbSize = sizeof(WNDCLASSEX);
Nikita21
Nikita21
wow thank you, now i get you :)

however it seems to be that i have another problem registering the window perheps?

becuse now i dont have the failed to register window error, yet it doesnt work nothing happen
reana1
reana1
if(!RegisterClassEx(&illawind)){	MessageBox(NULL,		       "Unable To register window",			   "Illa Error #1",			   MB_OK);}return (0);

You just return after registering it.
Tadd- WarbleWare
Nytegard
Nytegard
Do what reana1 stated, but your window is still going to fail. You need to associate the WNDCLASSEX structure with your window via the lpszClassName.

Your WNDCLASSEX is labeled WINCLASS1 whereas your Window is labeled "Ground Zero". Make them the same.

*EDIT*

Sorry, forget that last part that I just deleted. I had some debug code in my test program causing that.
Nikita21
Nikita21
ok now its working :) thank you guys
however i wish to know , why when i remove the fail to register window error message , it started working, i mean the return still reminds (0);
so how come when i remove the message part its working ?
Nytegard
Nytegard
Quote:
Original post by Nikita21
ok now its working :) thank you guys
however i wish to know , why when i remove the fail to register window error message , it started working, i mean the return still reminds (0);
so how come when i remove the message part its working ?


You shouldn't remove that message. The main point reana1 was trying to make was that you were always calling a return right after that function. Thus, immediately finishing your program. The return should go inside the scope of the if test, not outside, so you'd only return when the function failed.

Because if for some reason the program crashes, and you don't return, you're going to most likely get into serious trouble with a lot of other errors appearing.

// Incorrect: I don't care what happens, I'm returning regardlessif(!RegisterClassEx(&illawind)){	MessageBox(NULL,		       "Unable To register window",			   "Illa Error #1",			   MB_OK);}return (0);//Better: I'll only return if registering failsif(!RegisterClassEx(&illawind)){	MessageBox(NULL,		       "Unable To register window",			   "Illa Error #1",			   MB_OK);	return (0);}
Nikita21
Nikita21
silly me, i dont even notice what am writing,

by putting the return after the

{
... // fail program error
}
return 0;


the program always self terminated

however when i put it in the the
{
...// fail program error
return 0;
}

then the program only get killed when the window fail to register and after giving an error, you are so right you guys, i havent noticed it when wrote it :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.