Click here to Skip to main content
15,905,781 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralToolBar close Pin
Burl D4-Jan-04 6:24
Burl D4-Jan-04 6:24 
GeneralRe: ToolBar close Pin
PJ Arends4-Jan-04 10:55
professionalPJ Arends4-Jan-04 10:55 
GeneralGDI drawing in layers Pin
mirano4-Jan-04 5:52
mirano4-Jan-04 5:52 
GeneralRe: GDI drawing in layers Pin
PJ Arends4-Jan-04 9:30
professionalPJ Arends4-Jan-04 9:30 
GeneralSending a packet using PostMessage Pin
__Cerb4-Jan-04 4:38
__Cerb4-Jan-04 4:38 
GeneralRe: Sending a packet using PostMessage Pin
Stefan Pedersen4-Jan-04 4:46
Stefan Pedersen4-Jan-04 4:46 
GeneralRe: Sending a packet using PostMessage Pin
__Cerb4-Jan-04 5:33
__Cerb4-Jan-04 5:33 
GeneralRe: Sending a packet using PostMessage Pin
Gary R. Wheeler4-Jan-04 5:30
Gary R. Wheeler4-Jan-04 5:30 
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;
GeneralRe: Sending a packet using PostMessage Pin
__Cerb4-Jan-04 5:45
__Cerb4-Jan-04 5:45 
GeneralRe: Sending a packet using PostMessage Pin
User 66584-Jan-04 6:21
User 66584-Jan-04 6:21 
GeneralRe: Sending a packet using PostMessage Pin
__Cerb4-Jan-04 6:33
__Cerb4-Jan-04 6:33 
GeneralRe: Sending a packet using PostMessage Pin
User 66584-Jan-04 6:50
User 66584-Jan-04 6:50 
GeneralRe: Sending a packet using PostMessage Pin
Gary R. Wheeler4-Jan-04 6:53
Gary R. Wheeler4-Jan-04 6:53 
GeneralRe: Sending a packet using PostMessage Pin
__Cerb4-Jan-04 11:06
__Cerb4-Jan-04 11:06 
GeneralRe: Sending a packet using PostMessage Pin
Gary R. Wheeler5-Jan-04 15:32
Gary R. Wheeler5-Jan-04 15:32 
QuestionHow to drag from control to Explorer Pin
Anonymous4-Jan-04 3:47
Anonymous4-Jan-04 3:47 
GeneralRemotely starting and stopping a service programmatically Pin
luyaz4-Jan-04 3:05
luyaz4-Jan-04 3:05 
GeneralRe: Remotely starting and stopping a service programmatically Pin
valikac4-Jan-04 6:28
valikac4-Jan-04 6:28 
GeneralRe: Remotely starting and stopping a service programmatically Pin
Renjith Ramachandran4-Jan-04 8:05
Renjith Ramachandran4-Jan-04 8:05 
GeneralWindows Copy to Clipboard problem Pin
impeham4-Jan-04 2:30
impeham4-Jan-04 2:30 
GeneralRe: Windows Copy to Clipboard problem Pin
valikac4-Jan-04 6:32
valikac4-Jan-04 6:32 
GeneralRe: Windows Copy to Clipboard problem Pin
impeham8-Jan-04 0:39
impeham8-Jan-04 0:39 
Generalhelp my project, please Pin
IT student4-Jan-04 1:27
IT student4-Jan-04 1:27 
GeneralRe: help my project, please Pin
Prakash Nadar4-Jan-04 4:21
Prakash Nadar4-Jan-04 4:21 
QuestionBug in CMonthCalCtrl class ?? Pin
Qadddd4-Jan-04 0:55
Qadddd4-Jan-04 0:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.