Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. MessageQ inside member function

MessageQ inside member function

Scheduled Pinned Locked Moved C / C++ / MFC
jsonquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RichardS
    wrote on last edited by
    #1

    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 becomes NULL. If I make uMsg 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

    S 1 Reply Last reply
    0
    • R RichardS

      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 becomes NULL. If I make uMsg 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

      S Offline
      S Offline
      Stephen Hewitt
      wrote on last edited by
      #2

      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 a LPMSG but your variable is of type UINT. 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 a UINT* and you normally wouldn't compile this code but I'm the man, just blindly push ahead and compile it anyway". The problem is a UINT isn't a MSG, 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 write static_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 a reinterpret_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

      R 1 Reply Last reply
      0
      • S Stephen Hewitt

        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 a LPMSG but your variable is of type UINT. 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 a UINT* and you normally wouldn't compile this code but I'm the man, just blindly push ahead and compile it anyway". The problem is a UINT isn't a MSG, 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 write static_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 a reinterpret_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

        R Offline
        R Offline
        RichardS
        wrote on last edited by
        #3

        Hi 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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups