Registered Messages vs. WM_USER

When sending messages and notification from user-created controls, one has two options to define custom messages. The first is to send a WM_USER message. That is, take the WM_USER constant and add to it a number. For example:

This allows any window to capture the message or notification using normal ON_COMMAND/ON_NOTIFY macros. However, using WM_USER for normal messages is a mistake. Suppose I created a component called "Special" and another programmer from another side of the galaxy creates a component called "VerySpecial". Both of us could have several messages sent and both would use WM_USER messages. If by any chance both controls will use the same message, a windows program might behave in an unexpected way, since both use the exact same message. This problem exists only when using the ON_COMMAND macro and not ON_NOTIFY, since ON_NOTIFY uses the control id and not just the message code.

Therefore the conclusion is to use WM_USER messages only when sending notification messages and not normal windows messages.

One alternative to using custom Windows messages that are based on the WM_USER message is to register your own Windows messages. The idea behind registered messages is to create a unique Windows message based upon a string. Any window that knows the string can obtain the message code and therefore, respond to the predefined message. This allows several components to create custom messages, and by simply concatenating the component name to the message name, they can be sure that their string is unique, and therefore the message code is unique.

Here's an example of how to add the message handler of a registered message on the client side using the MFC message map.

static const UINT MsgSpecialCtrlChange = ::RegisterWindowMessage(SPECIAL_CTRL_CHANGE);



#define SPECIAL_CTRL_CHANGE _T("Special_Change")
Another nice advantage that registered message can provide, is the ability to capture the message sent from another application, again by only knowing the string.

The Windows Common Controls use this type of message routing from the DLL to the user application for several messages such as FindOrReplace command in RichEdit control. The intellimouse messages are also sent with registered messages, instead of creating new windows messages for them.

Date Last Updated: March 5, 1999



    Posted by Legacy on 07/24/2001 12:00am

    Originally posted by: zgabeata iftode

    Although I agree that there are obvious benefits to registered messages, I still don't get it: how do you use WM_USER messages with the ON_COMMAND macro? Or with ON_NOTIFY?

    As far as I know ON_COMMAND is limited to WM_COMMAND message and ON_NOTIFY is limited to WM_NOTIFY message. Can you post a piece of code that uses ON_COMMAND to catch any WM_USER message? Oh, and before you do that, make sure it actually works!

  • There is an excellent article on this...

    Posted by Legacy on 07/09/2001 12:00am

    Originally posted by: Breckin Loggins

    Joseph M. Newcomer has a most excellent article clearly describing when it is appropriate to use WM_APP and when to use registered messages. He also clearly explains why using WM_USER is a Bad Idea. I'm listening to him now. I was using WM_USER and my app worked fine on W2K but when I tried it on W98 some of my messages weren't working. Turns out it's because W98 has some controls installed that trampled over the WM_USER message space, so I changed them to WM_APP and haven't had a problem since. Note that in this case I'm not using registered messages because I'm not sending the messages across processes.

    Here's the link:

  • do they work across dlls?

    Posted by Legacy on 03/14/1999 12:00am

    Originally posted by: selom ofori

    I recently took parts of my application out and put them in an mfc extension dll. The registered messages didn't seem to work anymore. The values I was getting was totally wacked and all my switch statements wouldn't work. I eventually had to switch them to hard-coded numbers. Was I doing something wrong or do they just not work across dlls?

  • Performance of registered messages

    Posted by Legacy on 03/10/1999 12:00am

    Originally posted by: Stefan Niermann

    I think, WM_USER + xxxx must be somewhat faster than registered messages. Every time you send a registered message, Windows looks for the registration of it. The message will not be dispatched to the window if the OS doesn't find that registration. I realized that, when I tried to send a message with a constant code in the range of registered messages.

  • WM_USER or WM_APP is ok for interprocess messages

    Posted by Legacy on 03/05/1999 12:00am

    Originally posted by: BadDog

    If all you're doing is defining a message to be sent to another window in the same application, then using
    WM_USER or WM_APP messages is not a problem.

    ...for interprocess communication: Use WM_USER/WM_APP
    ...for broadcasting messages: Use RegisterWindowMessage
    ...for communicating with another application: depends...if you want to grab a specific application and send messages to it, then setting up something like a semaphore or finding a specific window class and then sending it messages instead of broadcasting messages to the whole system seems like it would be better.

    Most of the time when I've started down the path of using a registered message, I've come to the realization that it is more than I need.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

  • Recently, Forrester Consulting conducted a Total Economic Impact™ (TEI) study to better understand the impact of incorporating Polycom voice solutions with Microsoft Lync. Forrester interviewed a company that had already deployed the Lync/Polycom combination to determine its potential return on investment (ROI). Microsoft Lync provides presence information and promotes voice and video collaboration. Polycom extends the benefits of Lync with voice endpoints and further enables collaboration in multiple …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds