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. SendMessage

SendMessage

Scheduled Pinned Locked Moved C / C++ / MFC
question
11 Posts 4 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.
  • A Offline
    A Offline
    Anilkumar K V
    wrote on last edited by
    #1

    Hi The SendMessage function is used to send a message directly to a window procedure. Does this means that for messages received by SendMessage, the message loop( GetMessage & DispatchMessage) is not needed to process the message and call the windowproc. What happens internally on SendMessage? Welcome all suggestions Thanks Anil

    N R 2 Replies Last reply
    0
    • A Anilkumar K V

      Hi The SendMessage function is used to send a message directly to a window procedure. Does this means that for messages received by SendMessage, the message loop( GetMessage & DispatchMessage) is not needed to process the message and call the windowproc. What happens internally on SendMessage? Welcome all suggestions Thanks Anil

      N Offline
      N Offline
      Naveen
      wrote on last edited by
      #2

      anilFirst wrote:

      to send a message directly to a window procedure.

      no. It will go through the message loop. Why dont you try it for your self?

      nave

      P 1 Reply Last reply
      0
      • N Naveen

        anilFirst wrote:

        to send a message directly to a window procedure.

        no. It will go through the message loop. Why dont you try it for your self?

        nave

        P Offline
        P Offline
        prasad_som
        wrote on last edited by
        #3

        Naveen R wrote:

        no. It will go through the message loop.

        No, it wont. Thats why its SendMesssage. As opposed to PostMessage, which are posted to message loop.

        Prasad Notifier using ATL | Operator new[],delete[][^]

        N 1 Reply Last reply
        0
        • P prasad_som

          Naveen R wrote:

          no. It will go through the message loop.

          No, it wont. Thats why its SendMesssage. As opposed to PostMessage, which are posted to message loop.

          Prasad Notifier using ATL | Operator new[],delete[][^]

          N Offline
          N Offline
          Naveen
          wrote on last edited by
          #4

          ok ok.. So it directly call the window proc?

          nave

          P 1 Reply Last reply
          0
          • A Anilkumar K V

            Hi The SendMessage function is used to send a message directly to a window procedure. Does this means that for messages received by SendMessage, the message loop( GetMessage & DispatchMessage) is not needed to process the message and call the windowproc. What happens internally on SendMessage? Welcome all suggestions Thanks Anil

            R Offline
            R Offline
            Roger Stoltz
            wrote on last edited by
            #5

            anilFirst wrote:

            Does this means that for messages received by SendMessage, the message loop( GetMessage & DispatchMessage) is not needed to process the message and call the windowproc.

            It depends on from what thread the message is "sent". If the message is sent from the thread that created the window, the message handler will get called directly without taking the detour of the message loop. If the message is sent from another thread, the receiving thread must be running a message loop since a context switch will be made to the receiving thread while the sending thread is blocked. This is also the reason why a deadlock situation can occur when sending messages from different threads and the receiving thread doesn't process messages. Have a closer look at MSDN documentation for SendMessage() here[^] below the "Remarks" section.


            "It's supposed to be hard, otherwise anybody could do it!" - selfquote
            "High speed never compensates for wrong direction!" - unknown

            A 1 Reply Last reply
            0
            • R Roger Stoltz

              anilFirst wrote:

              Does this means that for messages received by SendMessage, the message loop( GetMessage & DispatchMessage) is not needed to process the message and call the windowproc.

              It depends on from what thread the message is "sent". If the message is sent from the thread that created the window, the message handler will get called directly without taking the detour of the message loop. If the message is sent from another thread, the receiving thread must be running a message loop since a context switch will be made to the receiving thread while the sending thread is blocked. This is also the reason why a deadlock situation can occur when sending messages from different threads and the receiving thread doesn't process messages. Have a closer look at MSDN documentation for SendMessage() here[^] below the "Remarks" section.


              "It's supposed to be hard, otherwise anybody could do it!" - selfquote
              "High speed never compensates for wrong direction!" - unknown

              A Offline
              A Offline
              Anilkumar K V
              wrote on last edited by
              #6

              In "Programming Windows" by Charles Petzold "Programs generally don't call window procedures directly. The window procedure is almost always called from Windows itself. A program can indirectly call its own window procedure by calling a function named SendMessage" So how the SendMessage and PostMessage invoke window proc's? What is internal working of both? Thanks Anil

              R 1 Reply Last reply
              0
              • A Anilkumar K V

                In "Programming Windows" by Charles Petzold "Programs generally don't call window procedures directly. The window procedure is almost always called from Windows itself. A program can indirectly call its own window procedure by calling a function named SendMessage" So how the SendMessage and PostMessage invoke window proc's? What is internal working of both? Thanks Anil

                R Offline
                R Offline
                Roger Stoltz
                wrote on last edited by
                #7

                What Charles Petzold meant with the statement you've quoted is probably that you seldom call a message handler directly as an ordinary function call. If you find yourself in a situation where you would like to call a message handler directly, you should call SendMessage() with appropriate message ID as argument. I guess this is what Petzold refers to as being "called from Windows itself" since your using the Win32 API method SendMessage().

                anilFirst wrote:

                So how the SendMessage and PostMessage invoke window proc's? What is internal working of both?

                This addresses the message management routines from a different perspective than the Petzold qoute. The qoute answers a question such as "how do I call a message handler from my code", but I understand your question to be more like "in what ways does a message handler get called when sending a message with SendMessage and PostMessage". A part of the answer for that question can be read in my previous post and also by following the MSDN link I provided. In addition: when posting a message with PostMessage(), the message is always placed in the message queue for the receiving window and retrieved with a call to GetMessage(). The matching message handler is then usually determined by a switch-case statement and called with the parameters that came with the message.


                "It's supposed to be hard, otherwise anybody could do it!" - selfquote
                "High speed never compensates for wrong direction!" - unknown

                A 1 Reply Last reply
                0
                • N Naveen

                  ok ok.. So it directly call the window proc?

                  nave

                  P Offline
                  P Offline
                  prasad_som
                  wrote on last edited by
                  #8

                  Naveen R wrote:

                  So it directly call the window proc?

                  No exactly, but its system(windows) does that.

                  Prasad Notifier using ATL | Operator new[],delete[][^]

                  1 Reply Last reply
                  0
                  • R Roger Stoltz

                    What Charles Petzold meant with the statement you've quoted is probably that you seldom call a message handler directly as an ordinary function call. If you find yourself in a situation where you would like to call a message handler directly, you should call SendMessage() with appropriate message ID as argument. I guess this is what Petzold refers to as being "called from Windows itself" since your using the Win32 API method SendMessage().

                    anilFirst wrote:

                    So how the SendMessage and PostMessage invoke window proc's? What is internal working of both?

                    This addresses the message management routines from a different perspective than the Petzold qoute. The qoute answers a question such as "how do I call a message handler from my code", but I understand your question to be more like "in what ways does a message handler get called when sending a message with SendMessage and PostMessage". A part of the answer for that question can be read in my previous post and also by following the MSDN link I provided. In addition: when posting a message with PostMessage(), the message is always placed in the message queue for the receiving window and retrieved with a call to GetMessage(). The matching message handler is then usually determined by a switch-case statement and called with the parameters that came with the message.


                    "It's supposed to be hard, otherwise anybody could do it!" - selfquote
                    "High speed never compensates for wrong direction!" - unknown

                    A Offline
                    A Offline
                    Anilkumar K V
                    wrote on last edited by
                    #9

                    MessageLoop ( GetMessage & DispatchMessage ) not needed in some cases. So it means that SendMessage() can call WindowProc directly. For a test case call UpdateWindow() before MessageLoop will invoke WindowProc directly. WindowProc will be called with WM_PAINT message. Then only the control comes to MessageLoop.

                    R 1 Reply Last reply
                    0
                    • A Anilkumar K V

                      MessageLoop ( GetMessage & DispatchMessage ) not needed in some cases. So it means that SendMessage() can call WindowProc directly. For a test case call UpdateWindow() before MessageLoop will invoke WindowProc directly. WindowProc will be called with WM_PAINT message. Then only the control comes to MessageLoop.

                      R Offline
                      R Offline
                      Roger Stoltz
                      wrote on last edited by
                      #10

                      anilFirst wrote:

                      MessageLoop ( GetMessage & DispatchMessage ) not needed in some cases. So it means that SendMessage() can call WindowProc directly.

                      Yes. In case the message is sent using SendMessage().

                      anilFirst wrote:

                      For a test case call UpdateWindow() before MessageLoop will invoke WindowProc directly. WindowProc will be called with WM_PAINT message. Then only the control comes to MessageLoop.

                      :confused: I don't understand what you mean. Is this a question? If you want to see how message handling works, I suggest you use your own user defined message ID. You cannot use WM_PAINT or WM_TIMER for this purpose since those messages are treated differently than other messages. Try sending with SendMessage() and posting with PostMessage() from the main thread and see how the call stack differs in the two scenarios. Then try sending the message from another thread and you'll find that the call stack looks the same as if the message was posted from the main thread.


                      "It's supposed to be hard, otherwise anybody could do it!" - selfquote
                      "High speed never compensates for wrong direction!" - unknown

                      A 1 Reply Last reply
                      0
                      • R Roger Stoltz

                        anilFirst wrote:

                        MessageLoop ( GetMessage & DispatchMessage ) not needed in some cases. So it means that SendMessage() can call WindowProc directly.

                        Yes. In case the message is sent using SendMessage().

                        anilFirst wrote:

                        For a test case call UpdateWindow() before MessageLoop will invoke WindowProc directly. WindowProc will be called with WM_PAINT message. Then only the control comes to MessageLoop.

                        :confused: I don't understand what you mean. Is this a question? If you want to see how message handling works, I suggest you use your own user defined message ID. You cannot use WM_PAINT or WM_TIMER for this purpose since those messages are treated differently than other messages. Try sending with SendMessage() and posting with PostMessage() from the main thread and see how the call stack differs in the two scenarios. Then try sending the message from another thread and you'll find that the call stack looks the same as if the message was posted from the main thread.


                        "It's supposed to be hard, otherwise anybody could do it!" - selfquote
                        "High speed never compensates for wrong direction!" - unknown

                        A Offline
                        A Offline
                        Anilkumar K V
                        wrote on last edited by
                        #11

                        I did try user defined message using SendMessage() before the message loop, soon after a call to CreateWindow() API. The message was send from main thread only. The windowproc received the User defined messages. This means that the message loop is not needed for SendMessage to deliver messages to WindowProc.

                        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