Solved

Receiving registered Windows-messages

Posted on 1998-12-18
3
201 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

773 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