#### Archived

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

# PeekMessage

This topic is 5589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok, First I know how to use Win32 but ive never done a message loop using PeekMessage. I just started learning some GL and just changed my message loop to use peek message now instead it looks like this:

BOOL bRet = FALSE;
while(!bRet)
{	::PeekMessage(&msg, hWnd, NULL, NULL, PM_REMOVE);
if(msg.message == WM_QUIT)
{	bRet = TRUE;
}
else
{	glWin.Render();
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

The problem is that when I exit the program the applications process is still active. This happens no matter what I have tried, I know that with GetMessage this happens if you use a HWND for the window handle instead of null, so I tried that and the problem still ocurs. If needed I can post more code. Thanx in advance [edited by - xds4lx on August 26, 2002 10:29:24 PM]

##### Share on other sites
Yeah, post your WndProc, at least the WM_CLOSE and WM_DESTROY handlers.

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//

##### Share on other sites
Actually I just found out what the problem was. It occured if i trped the WM_DESTROY message. So I just changed my WM_CLOSE handler to this:

  case WM_CLOSE:{    if(MessageBox(NULL,"Sure you want to exit?","Close?",MB_YESNO) == IDYES)     {    PostQuitMessage(0);     }     return(0);}

From:

  case WM_CLOSE:{	if(::MessageBox(NULL,"Sure you want to Quit?","Close?", MB_YESNO) == IDYES)	{	::DestroyWindow(hWnd);	}	return(0);}case WM_DESTROY:{    PostQuitMessage(0);     return(0);}

##### Share on other sites
That''ll do it Glad you found it.

Peace,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//

##### Share on other sites
Im just wondering if anyone knows why the origional way I was handling shut down didnt work? It works for all other windows applications ive done (non 3D related)

##### Share on other sites
WM_CLOSE default action is to call DestroyWindow(), and WM_DESTROY is sent when window is being destroyed.

"The PostQuitMessage function indicates to Windows that a thread has made a request to terminate (quit). It is typically used in response to a WM_DESTROY message." - MSDN

I guess is your OpenGL thingie (your glWin?) that gives you this problem.

##### Share on other sites
You answered the wrong question there man. I want to know why the propper code fails (calling DestroyWindow from a WM_CLOSE message and PostQuitMessage from a WM_DESTROY message) and calling PostQuitMessage works when calling it from the WM_CLOSE.

EDIT: I can post all the code if that would help.

[edited by - xds4lx on August 28, 2002 11:30:58 PM]

##### Share on other sites
I was also quite surprise that your orginal code didn''t work. I followed MSDN ways and so far no problem, even with Directxxxxxx (oh ok, no yet try OpenGL).
That''s why I suspect something is wrong... in other places.

##### Share on other sites
Theres is nothing wrong anywhere else in the program. Ive been doing Win32 for a few years, but this is my first dive into the world of 3D graphics. Everything was fine untill I had changed the message loop from using GetMessage to PeekMessage. I know everything is fine with the Win32 wrapper because it is one that I have used in lots of other Win32 applications. Could it possibly be the window styles used? Maybe I should use WM_PARENTNOTIFY?

##### Share on other sites
Actually, unless you didn't post all your code, there is something wrong. Here we go:
What most Win32 programs should do is what xDS4Lx had originally: on WM_CLOSE, do any processing for saving and such, then call DestroyWindow(). When DestroyWindow() is called, the window is destroyed, then a WM_DESTROY message is sent to the WindowProc for the window that was destroyed (or maybe the message is sent first; I'm not completely certain). When WM_DESTROY is received, then any last-minute processing such as freeing variables can be done, and lastly PostQuitMessage(0) should be called. The PeekMessage() loop, upon detecting that message (the Quit message), exits -- and then what? The program will continue to run until the main function, inside which the PeekMessage() loop resides, returns a value (usually msg.wParam, which is 0 if there were no errors). So, unless you already have this, put a line in your program at the end of your WinMain() function saying:
return msg.wParam;
and go back to the original WindowProc layout.

Twilight Dragon
Win32 API Programmer
www.freewebz.com/j-world

[edited by - TDragon on August 29, 2002 4:41:54 PM]

##### Share on other sites
PeekMessage( NULL, 0, 0, PM_REMOVE );

The replacement of a specific window handle with NULL allows PeekMessage to check all message queues associated with the application thread. WM_QUIT is not sent to the application, so this alteration is necessary to process the message in this fashion.

Um I already know what im doing in win32 TDragon. Im not some stupid newbie to win32. I already was returning msg.wParam! Also I didnt post all my code b/c im not going to blow up one post to this thread with a $hit ton of code. And Oluseyi I tried using NULL instead of the windows HWND and the problem still occurs. [edited by - xds4lx on August 29, 2002 5:16:06 PM] #### Share this post ##### Link to post ##### Share on other sites quote: Original post by xDS4Lx Um I already know what im doing in win32 TDragon. Im not some stupid newbie to win32. I already was returning msg.wParam! Also I didnt post all my code b/c im not going to blow up one post to this thread with a$hit ton of code. And Oluseyi I tried using NULL instead of the windows HWND and the problem still occurs.

Yep, I agreed not to bloat the forum with codes.

Maybe some handle isn''t being released yet, eg. other Thread?? ...some 3D function lock-up your handles? Or something wierd is in the msg loop.

... I suggest try using PeekMessage() just to peek on the msg queue, and GetMessage to really get the msg... just see if it still the same??
 BOOL res;  // BOOL???for ( ; ; ){  if (PeekMessage(...PM_NOREMOVE))  {    res = GetMessage(...);    if (res == -1)      throw "Some error";    else if (res == WM_QUIT)      break;    else    {       ... do something here    }}

##### Share on other sites
Well wouldnt that defeat the purpose of using PeekMessage? Anyways I came up with a solution after looking at the OONeHe code on his page where it is actually called after the message loop, so I put it into my class''s destructor and all works great! Thanx for the help.

##### Share on other sites
quote:
Original post by xDS4Lx
Well wouldnt that defeat the purpose of using PeekMessage? Anyways I came up with a solution after looking at the OONeHe code on his page where it is actually called after the message loop, so I put it into my class''s destructor and all works great! Thanx for the help.

What did you add?? Just want to know.

##### Share on other sites
All I did was not handle the WM_DESTROY message, but call PostQuitMessage from WM_CLOSE then call DestroyWindow from the class''s destructor.