Solved

Receiving registered Windows-messages

Posted on 1998-12-18
3
198 Views
Last Modified: 2013-12-03
Hi, I'm trying to make two applications send custom windows-messages to eachother. I have managed registering and sending messages (I've seen the message beeing sent, using SPY), but I can't seem to find out how to receive them :)

Here is some relevant code:

registering..
UINT setSelected =
RegisterWindowMessage("TREE_SELECTION_PERFORMED");

sending..
PostMessage(setSelected, 0, 0);

Now.. how can I detect and apprehend this message from another application? (or even the same one..)

Assert(Christmas == 'Merry!')
0
Comment
Question by:toblo
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
Answers2000 earned 60 total points
ID: 1417475
OK

1. the sending code is wrong.  You need to use Send/PostMessage( hWnd, setSelected, 0, 0 ) ; where hWnd is the HWND of the target app, or HWND_BROADCAST.  Use HWND_BROADCAST to do a broadcast to all top level Windows in the system.

2. The top level Window of the receiving apps will  receive this message.  However as the defualt behaviour is to do nothing with non-understood messages, nothing happens.

What you need to do is check for the particular message

In the target app, ALSO register the message, and compare this against the message parameter received in the top level window (you must use if, not switch/case)


3. A tip :
because you probably don't want to overhead of broadcasting messages all the time, it's a good idea to use a hand-shaking protocol :-

App1 -> SendMessage HWND_BROADCAST, with custom message #1, with it's own HWND stuffed into WPARAM

App2 -> Receives custom message #1.  Pulls out WPARAM to get the send, does SendMessage back using custom message #2  to tell App #1 it got it, and it's HWND (which it stuffs into WPARAM)

App1 -> Receives custom message #2.  Unpacks WPARAM.  It now knows who it it communicating with.

At this point both apps know the HWND of the app to talk to, so can do SendMessage with that HWND, rather than doing broadcasts
0
 

Author Comment

by:toblo
ID: 1417476
Hi!

Problem: I still don't know how to receive the message.. should it be done in CTesterDlg::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo) ??

 my app is dialog-based, by the way. Just for testing I'm trying to make it both send and retreive the test-message

When I send it, using ::PostMessage(HWND_BROADCAST, MyMessage, 0,0) I can see that it has been sent (using Spy++), but CTesterDlg::OnCmdMsg doesn't seem to react to it at all! -It reacts to my clicking the buttons in the dialog, though..


perplexedly yours :)
/Torbjörn Josefsson

0
 

Author Comment

by:toblo
ID: 1417477

Very useful information, although not all that I needed..
-If anybody else wonders how to receive them custom-messages in MFC:

In the message-map section, do this..
ON_REGISTERED_MESSAGE(MY_VERY_OWN_MESSAGE, OnMyMessage)

(MY_VERY_OWN_MESSAGE is the id I got from RegisterWindowMessage)

Declare it in the .h file as..
afx_msg LRESULT OnMyMessage(WPARAM, LPARAM);

and then just write the OnMyMessage-function (the handler)
Oh.. and you have to use the send/post-method that includes the recipient window (or broadcasts the message)

good luck :)
/Toblo

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now