MessageQ inside member function
-
Hi All, I have created a window using the Win32 api functions. Now I created a message loop inside a member function of the class like so:
{ UINT uMsg = 0; // Start another thread??? while (true) { if (::PeekMessage ((LPMSG) (&uMsg), m_hWnd, 0, 0, PM_REMOVE)) { ::TranslateMessage((LPMSG) (&uMsg)); ::DispatchMessage((LPMSG) (&uMsg)); } else { if (MessageQueueEmpty () == FAILURE) { return FAILURE; } } } return SUCCESS; }
However after the first iteration,this
becomesNULL
. If I makeuMsg
static, it works fine. Why?? :confused: regards, Rich "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far the Universe is winning." -- Rich Cook -- modified at 10:24 Thursday 2nd March, 2006 -
Hi All, I have created a window using the Win32 api functions. Now I created a message loop inside a member function of the class like so:
{ UINT uMsg = 0; // Start another thread??? while (true) { if (::PeekMessage ((LPMSG) (&uMsg), m_hWnd, 0, 0, PM_REMOVE)) { ::TranslateMessage((LPMSG) (&uMsg)); ::DispatchMessage((LPMSG) (&uMsg)); } else { if (MessageQueueEmpty () == FAILURE) { return FAILURE; } } } return SUCCESS; }
However after the first iteration,this
becomesNULL
. If I makeuMsg
static, it works fine. Why?? :confused: regards, Rich "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far the Universe is winning." -- Rich Cook -- modified at 10:24 Thursday 2nd March, 2006This code has a few problems. Here’s is an edited down version of your code which shows the biggest:
UINT uMsg = 0; ::PeekMessage((LPMSG)(&uMsg), m_hWnd, 0, 0, PM_REMOVE)
The first parameter to
PeekMesage
is aLPMSG
but your variable is of typeUINT
. You would get a compiler error if you tried the following code:::PeekMessage(&uMsg, m_hWnd, 0, 0, PM_REMOVE)
You "solve" this by adding the following C-style cast:
(LPMSG)
This will never work, all you've done with this cast is say to the compiler, "Yeah I know a
LPMSG
isn't the same as aUINT*
and you normally wouldn't compile this code but I'm the man, just blindly push ahead and compile it anyway". The problem is aUINT
isn't aMSG
, cast or no cast. Here's what it should look like (edited down version):MSG m; ::PeekMessage(&m, m_hWnd, 0, 0, PM_REMOVE)
A word of advice: Never use C-style casts (a type in brackets). Always use function style casts instead. When you find yourself tempted to write
(UINT)i
writestatic_cast<UINT>(i)
instead. If the cast doesn't make sense the compiler will complain (you can still get into trouble but the chances are a lot less). If you want to make the compiler do what you say as a C-style cast does use areinterpret_cast
. Casts are ugly (but occasionally necessary) and generally indicate a design flaw. The function style casts syntax allows you to spot casts more easily. Also C-style casts are not fine grained enough where as function style casts come in four types:dynamic_cast
,static_cast
,reinterpret_cast
,const_cast
so it is clearer and harder to abuse. Steve -
This code has a few problems. Here’s is an edited down version of your code which shows the biggest:
UINT uMsg = 0; ::PeekMessage((LPMSG)(&uMsg), m_hWnd, 0, 0, PM_REMOVE)
The first parameter to
PeekMesage
is aLPMSG
but your variable is of typeUINT
. You would get a compiler error if you tried the following code:::PeekMessage(&uMsg, m_hWnd, 0, 0, PM_REMOVE)
You "solve" this by adding the following C-style cast:
(LPMSG)
This will never work, all you've done with this cast is say to the compiler, "Yeah I know a
LPMSG
isn't the same as aUINT*
and you normally wouldn't compile this code but I'm the man, just blindly push ahead and compile it anyway". The problem is aUINT
isn't aMSG
, cast or no cast. Here's what it should look like (edited down version):MSG m; ::PeekMessage(&m, m_hWnd, 0, 0, PM_REMOVE)
A word of advice: Never use C-style casts (a type in brackets). Always use function style casts instead. When you find yourself tempted to write
(UINT)i
writestatic_cast<UINT>(i)
instead. If the cast doesn't make sense the compiler will complain (you can still get into trouble but the chances are a lot less). If you want to make the compiler do what you say as a C-style cast does use areinterpret_cast
. Casts are ugly (but occasionally necessary) and generally indicate a design flaw. The function style casts syntax allows you to spot casts more easily. Also C-style casts are not fine grained enough where as function style casts come in four types:dynamic_cast
,static_cast
,reinterpret_cast
,const_cast
so it is clearer and harder to abuse. SteveHi Steve, Thanks for the answer. Very helpful even though I can't believe I didn't see it. thanks again, Rich "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far the Universe is winning." -- Rich Cook