Help with Win32: PostQuitMessage(0) doesn't post WM_QUIT apparently, because the message loop is not exited.

Ruby The Roobster michaeleverestc79 at gmail.com
Fri Aug 13 13:47:55 UTC 2021


On Friday, 13 August 2021 at 03:05:22 UTC, Mike Parker wrote:
> On Friday, 13 August 2021 at 00:30:59 UTC, Ruby The Roobster 
> wrote:
>
>>
>> When I run the program and close the window, the program still 
>> runs in background mode.  I don't know why this happens nor 
>> how to fix it.  Does anybody know what's going on?
>
> frame beat me to it, but it may well be that you're getting -1. 
> [The documentation][1] says that a window that has already been 
> destroyed will result in the `hWnd` parameter being invalid, 
> which will cause the function to return -1.
>
>
> [1]: 
> https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getmessage#return-value

So I edited the message loop:
```d
         	while((b = GetMessage(&msg,hwnd,0,0)) != 0)	{
         		if(exit || b == -1)	{
         			return msg.wParam;
         		}
         		TranslateMessage(&msg);
         		DispatchMessage(&msg);
   	}
```
And some of the WndProc:
```d
         extern(Windows)
         LRESULT WndProc(HWND hwnd, UINT msg, WPARAM wparam, 
LPARAM lparam) nothrow	{
         	switch(msg)	{
         		case WM_CREATE:
         			//...
         			return 0;
         			break;
         		case WM_CLOSE:
         			DestroyWindow(hwnd);
         			try	{
         				entity.terminate();
         				exit = true;
         			}
         			catch(Throwable e){
         			}
         			break;
         		case WM_DESTROY:
         			try	{
         				entity.terminate();
         				exit = true;
         			}
         			catch(Throwable e)	{
         				PostQuitMessage(1);
         			}
         			PostQuitMessage(0);
         			break;
         		default:
         		return DefWindowProc(hwnd,msg,wparam,lparam);
         	}
         		return 0;
         }
```

It still produces the same result(the program never exits), so it 
doesn't look like GetMessage() is returning -1...


More information about the Digitalmars-d-learn mailing list