Sending Windows messages to objects


I think I understand how to send messages in MFC.

However, suppose I want to send a message but I only one particular object to receive it or do something with it. How would I do that?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

IssacJonesAuthor Commented:
For example, if I had three objects A, B and C (all of the same type), how could I send a message but only B receives it?
Set the first parameter to the message target window:
::SendMessage(HWND hWnd, UINT ...
..or if you're using MFC:
Using your example.
::SendMessage( B->GetSafeHwnd(), ...
or MFC way
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Janusz CzopowikSenior Software EngineerCommented:
You have to define object. Wht is it?

If your objects are not windows attached to MFC class’ object (derived from CWnd) or just Windows window having valid windows hadle, you cannot send a message.
However, you have ability to send a command message to any MFC object derived from CCmdTarget.
IssacJonesAuthor Commented:
I wasn't considering windows dervied objects initially. However, from what you say, it looks as if I just need to re-derive them from CCmdTarget?
the CCmdTarget is baseclass of all mfc classes that could handle messages but itself has no member PostMessage, SendMessage, ... .

instead it has virtual member function CCmdTarget::OnCmdMsg which is overridable but where the baseclass implementation does the lookup into the message maps which were used to call member functions of mfc derived classes.

Janusz CzopowikSenior Software EngineerCommented:
Well, CWinApp does not have Send/Post message either.
As I have mentioned:
"you have ability to send a command message to any MFC object derived from CCmdTarget"
This was just a general remark.
I was not sure what OOP meant by objects. Once I know, I can elaborate.
The CCmdTarget derived class (as I mention CWinApp or CDocument for example) has own message map.
However since this class does not wrap windows handle, messages cannot be Send/Post directly. CWinApp receives command messages because this is a part of the command routing implemented by MFC.
If you want to use CCmdTarget ocjects, you would have to insert them into a command routing chain.
Using object’s owner (must be CWnd derived) and add following:

BOOL CSomeView::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
	if(m_MsgReceiver.OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
		return TRUE;

	return CView::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);

Open in new window

I have a question: why are you trying to communicate with your objects using message sending mechanism.
Did you consider simply calling public member functions implemented by the object?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
IssacJonesAuthor Commented:
Hi John

I have indeed been using public member functions in the past. However, I'm learning MFC and C++ and it just occurred to me that sending messages to objects could be possible. Maybe I'm making life too complicated. I'm just curious I guess!

Janusz CzopowikSenior Software EngineerCommented:
You are not making it too complicated. Everything depends on your project design.
If you want your object to be able to respond directly to user commands, then use CCmdTarget.
For example: CDocument is derived from CCmdTarget. Save command could be handled by a view. Since by design, document keeps all necessary data it is logical to communicate Save command directly to the document object.
If you do not need user interaction but need serialization, use CObject as a base class and call public members.
If you do not need any of the above, do not use MFC class as a base and call public members.

Everything depends on you to decise. If you want to learn more about MFC and C++ it is a good exercise.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.