Solved

Receiving registered Windows-messages

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Run Program using VBScript 3 73
Example of WebAPI 7 34
What are new names for Microsoft test frameworks once called Moles and Pex? 2 62
Authentication of Web Services 3 89
This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…

821 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