i am currently working with VC++ 6.0. i have just begun with it and wrote my first non-MFC code in it..however i have faced a problem in which my program terminates immediately on executing it..and i mean immediately..it doesnt even show the window that i create through that program..
however..later on i tested the ready made "Hello World" code..when i ran that code it worked fine and well...without any problem..
i dont know where i am stuck...my code is as follows :

#include <windows.h>

LRESULT CALLBACK WinProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain (HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR lpszCmdArgs, int nWinMode)
{
	MSG Msg;
	WNDCLASS WinClass;	
//	WNDCLASSEX WinClass;
	HWND hWnd;
	
	WinClass.hInstance = hThisInst;
	WinClass.lpszClassName = "Bhoot";
	WinClass.lpfnWndProc = WinProc;
	WinClass.style = 0;
//	WinClass.cbSize = sizeof (WNDCLASSEX);
	WinClass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);

	if (!RegisterClass (&WinClass)) 
		return 0;
	
	hWnd = CreateWindow ( "Bhoot", "Bhoot", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, HWND_DESKTOP, NULL, hThisInst, NULL);
//	hWnd = CreateWindowEx ( NULL, "Bhoot", "Bhoot", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, HWND_DESKTOP, NULL, hThisInst, NULL);
	ShowWindow (hWnd, nWinMode);
	UpdateWindow (hWnd);

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

	return Msg.wParam;

}


LRESULT CALLBACK WinProc ( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	switch (Msg)
	{
		case WM_DESTROY :
			PostQuitMessage (0);
		break;

		default :
			return DefWindowProc (hWnd, Msg, wParam, lParam);
	}
	return 0;
}

can anyone explain me the reason and the solution to it?? :|

Recommended Answers

All 2 Replies

Did you use your compiler's debugger and setp through each line of the program? What was the value of hWnd (NULL or something else)?

In WinProc() I think you need to add code for WM_CREATE. The Hello World program you compiled should have had one.

In WinProc() I think you need to add code for WM_CREATE.

You dont need anything there, it is simply for any extra things you want to do just before the window displays.

Ok, theres quite a few problems in this code, I will list a couple of them.

  • Your window class (WinClass) structure is missing alot of variables the need setting, to register the class you need every variable assigned with a value.
  • You should use RegisterClassEx, not RegisterClass as you should using the WNDCLASSEX structure.

and quite a few more...

I have modified your code so that it compiles and works ok. I hope this helps :)

#include <windows.h>

LRESULT CALLBACK WinProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain (HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR lpszCmdArgs, int nWinMode)
{
	MSG Msg;
	WNDCLASSEX WinClass;
	HWND hWnd;
	

	WinClass.hInstance = hThisInst;
	WinClass.lpszClassName = "Bhoot";
	WinClass.lpfnWndProc = WinProc;
	WinClass.style = 0;
	WinClass.cbSize = sizeof (WNDCLASSEX);
	WinClass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);

	// You missed out these
	WinClass.cbClsExtra = 0;
	WinClass.cbWndExtra = 0;
	WinClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	WinClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	WinClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
	WinClass.lpszMenuName = NULL;

	if (!RegisterClassEx(&WinClass)) 
		return 0;
	
	hWnd = CreateWindow("Bhoot",
			"Bhoot",
			WS_OVERLAPPEDWINDOW,
			CW_USEDEFAULT,
			CW_USEDEFAULT,
			CW_USEDEFAULT,
			CW_USEDEFAULT,
			HWND_DESKTOP,
			NULL,
			hThisInst,
			NULL);

	ShowWindow(hWnd, nWinMode);
	UpdateWindow(hWnd);

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

	return (int) Msg.wParam;

}


LRESULT CALLBACK WinProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	switch (Msg)
	{
		case WM_DESTROY:
			PostQuitMessage(0);
		break;

		default:
			return DefWindowProc (hWnd, Msg, wParam, lParam);
	}
	return 0;
}
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.