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. Sending a packet using PostMessage

Sending a packet using PostMessage

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
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.
  • _ Offline
    _ Offline
    __Cerb
    wrote on last edited by
    #1

    Let's say I have this piece of code... CString strWndName = "My wnd"; CString strClassName = "Wnd class"; HWND hwnd = ::FindWindow(strClassName,strWndName); Would it be possible to send a packet to hwnd using PostMessage? If yes, which type of message should I send? Thanks for help, ~Mike

    S G 2 Replies Last reply
    0
    • _ __Cerb

      Let's say I have this piece of code... CString strWndName = "My wnd"; CString strClassName = "Wnd class"; HWND hwnd = ::FindWindow(strClassName,strWndName); Would it be possible to send a packet to hwnd using PostMessage? If yes, which type of message should I send? Thanks for help, ~Mike

      S Offline
      S Offline
      Stefan Pedersen
      wrote on last edited by
      #2

      WM_COPYDATA[^] might fit your needs. And if the paths that I have followed/have tread against the flow/there is no need for sorrow I am coming home Return, Crüxshadows

      _ 1 Reply Last reply
      0
      • _ __Cerb

        Let's say I have this piece of code... CString strWndName = "My wnd"; CString strClassName = "Wnd class"; HWND hwnd = ::FindWindow(strClassName,strWndName); Would it be possible to send a packet to hwnd using PostMessage? If yes, which type of message should I send? Thanks for help, ~Mike

        G Offline
        G Offline
        Gary R Wheeler
        wrote on last edited by
        #3

        I'm going to assume you are asking how you should use PostMessage to send a packet of information. "Which type of message" you should send depends on what you are trying to accomplish, which you haven't described. I'm also going to assume you are using MFC, and that you are trying to send a packet to a window created by your application. If you are using PostMessage(), the message is placed on the window's message queue, and PostMessage() returns immediately. The window that receives the message may take some time before it processes the message. This means that the message will have to persist until the window gets around to looking at it. The easiest way to do this is to allocate your 'packet' on the heap, and deallocate it in the message handler. Here's an example:

        #define MY_MESSAGE (WM_APP + 1)

        class Packet {
        public:
        Packet();
        ~Packet();
        // ... data members
        int m1;
        };

        ...

        // allocate a packet, set its contents

        Packet *packet = new Packet;
        packet->m1 = 123456;

        // here's how you post a message

        CString strWndName = "My wnd";
        CString strClassName = "Wnd class";
        HWND hwnd = ::FindWindow(strClassName,strWndName);

        ::PostMessage(hwnd,MY_MESSAGE,0,(LPARAM)packet);

        This code sends your user-defined message MY_MESSAGE to the window with handle hwnd. Your handler for this message should be declared as follows:

        afx_msg LRESULT OnMyMessage(WPARAM wParam,LPARAM lParam);

        and it's implementation will look like the following:

        BEGIN_MESSAGE_MAP( MyWindowClass, <window base class> )
        ...
        ON_MESSAGE(MY_MESSAGE,OnMyMessage)
        END_MESSAGE_MAP()
        ...

        LRESULT MyWindowClass::OnMyMessage(WPARAM wParam,LPARAM lParam)
        {
        Packet *packet = (Packet *)lParam;

        // ... use information in packet
        
        delete packet;
        
        return (LRESULT)0;
        

        }

        I hope this helps. If you are trying to send messages between applications, there are other methods of interprocess communication (named pipes, sockets, and so on) that are simpler.


        Software Zen: delete this;

        _ 1 Reply Last reply
        0
        • S Stefan Pedersen

          WM_COPYDATA[^] might fit your needs. And if the paths that I have followed/have tread against the flow/there is no need for sorrow I am coming home Return, Crüxshadows

          _ Offline
          _ Offline
          __Cerb
          wrote on last edited by
          #4

          Ok thanks. Let's say I want to send the 0x68 packet. COPYDATASTRUCT cds; cds.lpData = NULL; cds.cbData = 1; cds.dwData = 0x68; Would this work? And what should I send as WParam? 0? Thanks a lot for your help ~Mike

          1 Reply Last reply
          0
          • G Gary R Wheeler

            I'm going to assume you are asking how you should use PostMessage to send a packet of information. "Which type of message" you should send depends on what you are trying to accomplish, which you haven't described. I'm also going to assume you are using MFC, and that you are trying to send a packet to a window created by your application. If you are using PostMessage(), the message is placed on the window's message queue, and PostMessage() returns immediately. The window that receives the message may take some time before it processes the message. This means that the message will have to persist until the window gets around to looking at it. The easiest way to do this is to allocate your 'packet' on the heap, and deallocate it in the message handler. Here's an example:

            #define MY_MESSAGE (WM_APP + 1)

            class Packet {
            public:
            Packet();
            ~Packet();
            // ... data members
            int m1;
            };

            ...

            // allocate a packet, set its contents

            Packet *packet = new Packet;
            packet->m1 = 123456;

            // here's how you post a message

            CString strWndName = "My wnd";
            CString strClassName = "Wnd class";
            HWND hwnd = ::FindWindow(strClassName,strWndName);

            ::PostMessage(hwnd,MY_MESSAGE,0,(LPARAM)packet);

            This code sends your user-defined message MY_MESSAGE to the window with handle hwnd. Your handler for this message should be declared as follows:

            afx_msg LRESULT OnMyMessage(WPARAM wParam,LPARAM lParam);

            and it's implementation will look like the following:

            BEGIN_MESSAGE_MAP( MyWindowClass, <window base class> )
            ...
            ON_MESSAGE(MY_MESSAGE,OnMyMessage)
            END_MESSAGE_MAP()
            ...

            LRESULT MyWindowClass::OnMyMessage(WPARAM wParam,LPARAM lParam)
            {
            Packet *packet = (Packet *)lParam;

            // ... use information in packet
            
            delete packet;
            
            return (LRESULT)0;
            

            }

            I hope this helps. If you are trying to send messages between applications, there are other methods of interprocess communication (named pipes, sockets, and so on) that are simpler.


            Software Zen: delete this;

            _ Offline
            _ Offline
            __Cerb
            wrote on last edited by
            #5

            Actually I am trying to send a packet to an online game. That's why both your codes didn't work. Thanks anyway, I hope you can help me ~Mike

            L 1 Reply Last reply
            0
            • _ __Cerb

              Actually I am trying to send a packet to an online game. That's why both your codes didn't work. Thanks anyway, I hope you can help me ~Mike

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              packet to an online game? You need to send such packets to the query port of the game per UDP. What do you want to do exactly?

              _ 1 Reply Last reply
              0
              • L Lost User

                packet to an online game? You need to send such packets to the query port of the game per UDP. What do you want to do exactly?

                _ Offline
                _ Offline
                __Cerb
                wrote on last edited by
                #7

                I want to send the 0x68 packet to my game. I believe the client receives packets from the game server on port 6112, if this can help. Thanks, ~Mike

                L G 2 Replies Last reply
                0
                • _ __Cerb

                  I want to send the 0x68 packet to my game. I believe the client receives packets from the game server on port 6112, if this can help. Thanks, ~Mike

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Yes, read my posting above. Sending packets to game (servers) has nothing to do with PostMessage() or SendMessage(). You need to use UDP (most games use UDP, don't know which ones use TCP). Take a look at this article and its links section. regards

                  1 Reply Last reply
                  0
                  • _ __Cerb

                    I want to send the 0x68 packet to my game. I believe the client receives packets from the game server on port 6112, if this can help. Thanks, ~Mike

                    G Offline
                    G Offline
                    Gary R Wheeler
                    wrote on last edited by
                    #9

                    WHOA :confused:. PostMessage() is used to send Windows messages to windows, those funny things on the screen you interactive with. The phrases 'online game', '0x68 packet', 'game server', and 'port 6112' tell me that you are trying to send a specific packet to an online game, and that the game communicates using TCP/IP sockets. That is a whole 'nother kettle of fish. For introductory articles on socket programming under Windows, try here[^], here[^], and here[^]. You will also need to know the format of the packets used by the game to communicate.


                    Software Zen: delete this;

                    _ 1 Reply Last reply
                    0
                    • G Gary R Wheeler

                      WHOA :confused:. PostMessage() is used to send Windows messages to windows, those funny things on the screen you interactive with. The phrases 'online game', '0x68 packet', 'game server', and 'port 6112' tell me that you are trying to send a specific packet to an online game, and that the game communicates using TCP/IP sockets. That is a whole 'nother kettle of fish. For introductory articles on socket programming under Windows, try here[^], here[^], and here[^]. You will also need to know the format of the packets used by the game to communicate.


                      Software Zen: delete this;

                      _ Offline
                      _ Offline
                      __Cerb
                      wrote on last edited by
                      #10

                      So basically I have to create a UDP socket on my game server's ip and write my packet onto it... Am I right? Thanks, ~Mike

                      G 1 Reply Last reply
                      0
                      • _ __Cerb

                        So basically I have to create a UDP socket on my game server's ip and write my packet onto it... Am I right? Thanks, ~Mike

                        G Offline
                        G Offline
                        Gary R Wheeler
                        wrote on last edited by
                        #11

                        That's right. Like I mentioned in my earlier message, you'll need to know the format of the packet (how many bytes, what each byte means, and so on). Good luck.


                        Software Zen: delete this;

                        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