Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Receiving registered Windows-messages

Posted on 1998-12-18
3
Medium Priority
?
236 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 120 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

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 …
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

886 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