Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

perfectly_dark

Program Not Shutting Down

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

I''ve began writing an application but a common problem comes up everytime I do. For some reason, everytime I try to shut down the program the window closes but if I CTRL+ALT+DELETE it I can see that the process is still running and I have to shut it down through there. Here''s my source:
  
//Includes

#include <stdio.h>		//IO Tools
#include <windows.h>	//Standard Windows Functions
#include <windowsx.h>	//Extended Windows Functions


//Defines

#define WIN32_LEAN_AND_MEAN		//Reduce Program Overhead


//Set Some Variables

const int WINDOW_X = 0;						//Window X Position

const int WINDOW_Y = 0;						//Window Y Position

const int WINDOW_WIDTH = 800;					//Window Width

const int WINDOW_HEIGHT = 600;					//Window Height

const char * CLASSNAME = "3DPONG";				//Window Class Name

const char * WINDOWNAME = "Super 3D Pong!";		//Window Name


//Other Structures and Classes

MSG msg;				//Message Structure - Holds our messages

WNDCLASSEX wc;

//Handles

HINSTANCE g_hInstance;	//Application Handle

HWND g_hWnd;			//Window Handle


//Function Declarations

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow); //Windows Entry Poing

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);	//Window Procedure


//Program Entry Point

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	//Store the application handle in our handle

	g_hInstance = hInstance;

	//Fill out the window class

	wc.cbSize			=		sizeof(WNDCLASSEX);						//Size of class

	wc.cbClsExtra		=		0;										//Extra Bytes for Class

	wc.cbWndExtra		=		0;										//Extra bytes for Window

	wc.hbrBackground	=		(HBRUSH)GetStockObject(BLACK_BRUSH);	//Fill Window with Black Brush

	wc.hCursor			=		LoadCursor(NULL, IDC_ARROW);			//Load the standard arrow Icon

	wc.hIcon			=		LoadIcon(NULL, IDI_APPLICATION);		//Load the standard Application Icon

	wc.hIconSm			=		NULL;									//Use Large Icon for Smaller

	wc.hInstance		=		g_hInstance;								//Give the class the app handle

	wc.lpfnWndProc		=		WndProc;								//Window Handler

	wc.lpszClassName	=		CLASSNAME;								//Class Name

	wc.lpszMenuName		=		NULL;									//No Menu Yet

	wc.style			=		CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;		//Redraw window, Device Context for each window, Double Clicks Messages



	if(RegisterClassEx(&wc)==0)		//Register the new class, if it fails send an error and exit

	{
		MessageBox(NULL, "Failed to Register the Window Class", "Error", MB_OK | MB_ICONERROR);
		return 0;
	}
	//Create a new window and store the handle in g_hWnd, if it fails send an error and quit

	if((g_hWnd = CreateWindowEx(		WS_EX_OVERLAPPEDWINDOW,				//Extended Styles (Overlapped / ClientEdge)

								CLASSNAME,							//Window Class

								WINDOWNAME,							//Window Name

								WS_VISIBLE | WS_CAPTION | WS_SYSMENU,//Make the window visible by default *duh!

								WINDOW_X, WINDOW_Y,					//Window Coordiantes

								WINDOW_WIDTH, WINDOW_HEIGHT,		//Window Width/Height

								NULL,								//Parent Window (NONE)

								NULL,								//Menu (NONE)

								g_hInstance,						//Application Instance

								NULL)								//Aditional Arguments (NONE)

								) == NULL)
	{
		MessageBox(NULL, "Failed to create the Main Window", "Error", MB_OK | MB_ICONERROR);	//Send Error

		return 0;																				//Quit

	}
	//Main Message Pump

    while(true)													//Continue Looping

	{
		if(PeekMessage(&msg, g_hWnd, 0, 0, PM_NOREMOVE))		//Check If there is a message

		{
			if(!GetMessage(&msg, g_hWnd, 0, 0))					//If there is then process it, if it''s a Quit Message then exit

			{
				//Game End Code in here

				return msg.wParam;								//Exit

			}else{												//Otherwise...

				TranslateMessage(&msg);							//Translate virtual-key messages to character key messages

				DispatchMessage(&msg);							//Send to procedure

			}
		}
			//Stick Looping Game Code in here

		

	}//End Message Pump


	return 0;			//Tell Windows We''re Done

}

//Main Window Message Handler

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	case WM_DESTROY:
		{
			PostQuitMessage(0);
			return 0;
		}break;
	}
	
	return DefWindowProc(hWnd, msg, wParam, lParam);
}
  
Sorry about the kind of long source, thanks in advance! PS. I think its something to do with the message loop

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Use the value NULL instead of g_hWnd in your PeekMessage and GetMessage calls.

Share this post


Link to post
Share on other sites

  • 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!