Program won´t exit after destroying the window

hey... as i post the WM_QUIT message my program does not exit... as it seems it does not quit the Message queue O_o...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#ifdef UNICODE
#undef UNICODE
#endif

#include <windows.h>

WNDCLASSEX wnd;
HWND hWnd;
char lpszClassName[] = "MyWndClass";
MSG msg;

LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg,WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	wnd.cbSize = sizeof(WNDCLASSEX);
	wnd.style = CS_HREDRAW | CS_VREDRAW;
	wnd.lpfnWndProc = WndProc;
	wnd.cbClsExtra = 0;
	wnd.cbWndExtra = 0;
	wnd.hInstance = hInstance;
	wnd.hIcon = LoadIcon(NULL, IDI_WINLOGO);
	wnd.hCursor = LoadCursor(NULL, IDC_ARROW);
	wnd.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
	wnd.lpszMenuName = NULL;
	wnd.lpszClassName = lpszClassName;
	wnd.hIconSm = LoadIcon(NULL, IDI_WINLOGO);

	if(!RegisterClassEx(&wnd))
	{
		MessageBox(NULL,"Error Registering Window Class", "Class Error", MB_ICONERROR | MB_OK);
		return -1;
	}

	hWnd = CreateWindowEx(NULL,lpszClassName,"MyWindow",WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT,
							NULL,NULL,hInstance,NULL);

	if(!hWnd)
	{
		MessageBox(NULL,"Error Creating Window", "Window Error", MB_ICONERROR | MB_OK);
		return -1;
	}

	ShowWindow(hWnd,nShowCmd);

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



	return 0;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hDC;

	switch(Msg)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	case WM_CLOSE:
		DestroyWindow(hWnd);
		break;
	case WM_PAINT:
		{
			hDC = BeginPaint(hWnd,&ps);
			EndPaint(hWnd,&ps);
			return 0;
		}
		break;
	}

	return DefWindowProc(hWnd,Msg,wParam,lParam);
}
Last edited on
Modify your code to this and see if it works...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hDC;

	switch(Msg)
	{
	case WM_PAINT:
		{
			hDC = BeginPaint(hWnd,&ps);
			EndPaint(hWnd,&ps);
			return 0;
		}
		break;
                case WM_CLOSE:
                case WM_DESTROY:
                       DestroyWindow(hWnd);
                       PostQuitMessage(0);
                       return 0;
	}

	return DefWindowProc(hWnd,Msg,wParam,lParam);
}
öhm...
1
2
3
case WM_DESTROY:
                       DestroyWindow(hWnd);
                       PostQuitMessage(0);


DestroyWindow(x) sends WM_DESTROY to the window... Sounds Like And Deadlock^^...

No need to say that it does not change everything... :/...
Last edited on
He's using "break" instead of "return 0" and I've had that hang a program before. Also, WM_DESTORY by itself does not always get processed, especially in a dialog box procedure. If you don't manually DestroyWindow() in some dialog box procedure your hwnd, when recalled, might -- and has done with me in some cases -- still retained certain characteristics from a previous call.
the return 0 and the break -thing does not solve the problem... i did try this before... the funny thing is, that my WM_QUIT is not defined as 0x0000000... it is 0x0000012 instead O_o...


and the program works under the express editions but not with my professional...

but i did load some symbols for the debugger from the microsoft website... may this cause the problem?... but i cant find the dialog where i did change this... and neither can i find the webpage with the instructions :D...
may that helps?^^...
Last edited on
You might want to simply delete the entire folder and project (make sure, of course, that you save all your .cpp, .h, .rc, etc. files) and then create a new solution/project and see if that solves the problem.

Perhaps someone with more experience can answer your questions about the other.
The cause of your problem is quite simple. As soon as you destroy your window you won't be able to get messages from that window anymore. Therefore you have to use GetMessage(&msg,NULL,0,0) instead of GetMessage(&msg,hWnd,0,0)

And by the way replace the switch to (I think that makes a little more sense):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	switch(Msg)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	case WM_CLOSE:
		DestroyWindow(hWnd);
		break;
	case WM_PAINT:
		{
			hDC = BeginPaint(hWnd,&ps);
			EndPaint(hWnd,&ps);
		}
		break;
	}
That's why we have experts around, to correct us newbies who know just enought to be dangerous. I don't think I would have ever caught that unless it was in my own code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
LRESULT CALLBACK WndProc(__in HWND hWnd, __in UINT uMsg, __in WPARAM wParam, __in LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);

	switch(uMsg)
	{
		case WM_PAINT:
			// do shit here
			break;

		case WM_DESTROY:
			PostQuitMessage(0);
			break;

		case WM_CLOSE:
			DestroyWindow(hWnd);
			break;

		default:
			return DefWindowProc;
			break;
	}

	return NULL;
}


Right, I've had some friends complain about my coding style. Here's a more 'simplified' version..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg)
	{
		case WM_PAINT:
			// do shit here
			break;

		case WM_DESTROY:
			PostQuitMessage(0);
			break;

		case WM_CLOSE:
			DestroyWindow(hWnd);
			break;

		default:
			return DefWindowProc;
			break;
	}

	return NULL;
}

Pretty much removed the SAL/stuff that makes code more readable(for me)..
Last edited on
closed account (z05DSL3A)
An aside:

As GetMessage() can indicate an error by returning -1, it is a good idea to test for this to help avoid fatal application errors:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
BOOL bRet;

while( (bRet = GetMessage( &msg, hWnd, 0, 0 )) != 0)
{ 
    if (bRet == -1)
    {
        // handle the error and possibly exit
    }
    else
    {
        TranslateMessage(&msg); 
        DispatchMessage(&msg); 
    }
}
insight?.... its from the msdn documentation?:P... joke^^... thanks for alle the answers... the problem was - what jmc said - that my window was already destroyed, so i could not recieve more messages for it^^...
closed account (z05DSL3A)
insight?....

Pardon, insight what?
I've never had that issue before; I just do it like this:
1
2
3
case WM_DESTROY:
         PostQuitMessage(0);       // Send WM_QUIT
         break;
Topic archived. No new replies allowed.