Sending a packet using PostMessage
-
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, ~MikeWM_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
-
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, ~MikeI'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 usingPostMessage()
, the message is placed on the window's message queue, andPostMessage()
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 handlehwnd
. 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;
-
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
-
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 usingPostMessage()
, the message is placed on the window's message queue, andPostMessage()
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 handlehwnd
. 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;
-
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
-
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?
-
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
Yes, read my posting above. Sending packets to game (servers) has nothing to do with
PostMessage()
orSendMessage()
. 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 -
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
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;
-
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;
-
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
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;