#1
  1. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    12

    win32 window process keeps running


    When I close it, the window closes, but the process keeps running and I have to end task it:

    Code:
    #define WIN32_LEAN_AND_MEAN 1
    
    #include <windows.h> 
    //#include <gl/gl.h> 
    //#include <gl/glu.h> 
    //#include "MyGL.h" 
    
    char szApp[]       = "First OpenGL ****"; 
    char szClassname[] = "FirstOpenGL"; 
    char szFilename[MAX_PATH]; 
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 
    {
    	if(uMsg==WM_DESTROY) 
    	{
    		PostQuitMessage(0); 
    		return 0; 
    	}
    	else if(uMsg==WM_CREATE)
    	{
    		//
    	}
    	else if(uMsg==WM_SIZE)
    	{ /*
    		int height, width; 
    		height=HIWORD(lParam); 
    		width = LOWORD(lParam); 
    		if(height==0)
    		{
    			height=1; 
    		} */
    		/*
    		glViewport(0,0,width,height); 
    		glMatrixMode(GL_PROJECTION); 
    		glLoadIdentity(); 
    		
    		gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,1.0f,1000.f); 
    
    		glMatrixMode(GL_MODELVIEW); 
    		glLoadIdentity(); 
    		*/
    	}
    	else 
    		return DefWindowProc(hwnd,uMsg,wParam,lParam); 
    	return 0; 
    }
    
    WPARAM WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR CmdLine, int CmdShow)
    {
    	WNDCLASSEX	wc; 
    	MSG			msg; 
    	HWND		hwnd; 
    	
    	/* 
    		OpenGL specific
    	*/
    	HDC		hDc=NULL; 
    	HGLRC	hRc=NULL; 
    
    	wc.cbSize			= sizeof(WNDCLASSEX); 
    	wc.cbClsExtra		= 0; 
    	wc.cbWndExtra		= 0; 
    	wc.hbrBackground	= (HBRUSH)(COLOR_BTNFACE+1); 
    	wc.hCursor			= LoadCursor(NULL,IDC_ARROW); 
    	wc.hIcon   = wc.hIconSm = LoadIcon(NULL,IDI_APPLICATION); 
    	wc.hInstance		= hInst; 
    	wc.lpfnWndProc		= WndProc; 
    	wc.lpszClassName	= szClassname; 
    	wc.lpszMenuName		= NULL; 
    	wc.style			= CS_OWNDC; 
    
    	if(!RegisterClassEx(&wc)) 
    	{
    		MessageBox(NULL,"Son... an error occured at RegisterClassEx()",szApp,MB_OK); 
    		return 0; 
    	} 
    
    	hwnd = CreateWindowEx(
    					0, 
    					szClassname, 
    					szApp, 
    					WS_OVERLAPPEDWINDOW, 
    					CW_USEDEFAULT, 
    					CW_USEDEFAULT, 
    					800, 
    					500, 
    					NULL, 
    					NULL, 
    					hInst, 
    					NULL); 
    
    	if(hwnd==NULL)
    	{
    		MessageBox(NULL,"Son... an error occured at CreateWindowEx()",szApp,MB_OK); 
    		return 0; 
    	} 
    
    	ShowWindow(hwnd,CmdShow); 
    	UpdateWindow(hwnd); 
    
    	while(1)
    	{
    		if(PeekMessage(&msg,hwnd,NULL,NULL,PM_REMOVE))
    			if(msg.message == WM_QUIT) 
    				break; 
    			else
    			{
    				//RenderScene(); 
    				//SwapBuffers(); 
    				TranslateMessage(&msg); 
    				DispatchMessage(&msg); 
    			}
    	} 
    	return msg.wParam; 
    }
    What's wrong with this? It worked until I changed GetMessage to PeakMessage
  2. #2
  3. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12

    Re: win32 window process keeps running


    I guess I have no answer to your question, except some notes on your program:

    I'd like to know where you got the code for your message loop from:

    Code:
    	while(1)
    	{
    		if(PeekMessage(&msg,hwnd,NULL,NULL,PM_REMOVE))
    			if(msg.message == WM_QUIT) 
    				break; 
    			else
    			{
    				//RenderScene(); 
    				//SwapBuffers(); 
    				TranslateMessage(&msg); 
    				DispatchMessage(&msg); 
    			}
    	} 
    	return msg.wParam; 
    }
    because that is not normal. This is what the standard message loop looks like:

    Code:
    BOOL bRet;
    
    while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
    { 
        if (bRet == -1)
        {
            // handle the error and possibly exit
        }
        else
        {
            TranslateMessage(&msg); 
            DispatchMessage(&msg); 
        }
    }
    even though most people do NOT check for the possibility of an error from GetMessage, so they incorrectly use this:

    Code:
    while( (GetMessage( &msg, NULL, 0, 0 )) )
    { 
            TranslateMessage(&msg); 
            DispatchMessage(&msg); 
    }
    which is ok, if you want a version of your program that doesn't bother checking for errors to make things simplier to understand.

    Notice how the look ends without a call to break; This is because GetMessage() returns 0 (false) when it retrieves the WM_QUIT message.

    Although... PeekMessage() always retrieves WM_QUIT messages, so there seems like there is nothing wrong with your code...

    Another note: You could use a switch statement in the WndProc() function, instead of all those if ... else if ... else if.
    Last edited by Jason Doucette; July 16th, 2003 at 07:28 PM.
  4. #3
  5. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    12
    I got it from a directX book, getMessge() is too slow for games, so it uses PeekMessage().


    I commented out the code below and changed it to a standard messagebox, and it doesn't hang on exit.. So the problem is with the msgloop, what is wrong with it though? In my WndProc, I only handle WM_DESTROY as PostQuitMessage(0).

    Code:
    /*
    	while(1)
    	{ 
    		if(PeekMessage(&msg,hwnd,NULL,NULL,PM_REMOVE))
    			if(msg.message == WM_QUIT) 
    				break; 
    			else
    			{
    				//RenderScene(); 
    				//SwapBuffers(); 
    				TranslateMessage(&msg); 
    				DispatchMessage(&msg); 
    			}
    	}  
    */
    while( (GetMessage( &msg, NULL, 0, 0 ))>0)
    { 
            TranslateMessage(&msg); 
            DispatchMessage(&msg); 
    }
    Last edited by movEAX_444; July 16th, 2003 at 07:32 PM.
  6. #4
  7. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    The only difference I can see between the two is that PeekMessage is documented as follows:

    "During this call, the system delivers pending messages that were sent to windows owned by the calling thread using the SendMessage, SendMessageCallback, SendMessageTimeout, or SendNotifyMessage function. The system may also process internal events. Messages are processed in the following order:

    Sent messages
    Posted messages
    Input (hardware) messages and system internal events
    Sent messages (again)
    WM_PAINT messages
    WM_TIMER messages
    "

    I cannot test the code at the moment, so I am sorry that I cannot help you much. Perhaps the above has something to do with your problem.
  8. #5
  9. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    12
    I just fixed it, here is how it looks now:

    Code:
    	while(1)
    	{ 
    		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
    		{
    			if(msg.message == WM_QUIT) 
    				break; 
    
    			//RenderScene(); 
    			//SwapBuffers(); 
    			TranslateMessage(&msg); 
    			DispatchMessage(&msg); 
    		}
    	}
  10. #6
  11. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    Aha. Good job! I did not notice that you were calling it with the window handle before. I didn't think to compare the arguments with arguments in GetMessage() to see what the differences / similarities are.

IMN logo majestic logo threadwatch logo seochat tools logo