Name That Message
by Karl E. Peterson

March 2002 Issue  Download the original Classic VB code from this column.

Technology Toolbox: VB6, VB5, VB4/32

Q: Find the Registered Message Name, Given Its Number
I'm a Delphi developer, and am quite horrifically stuck on a program I'm putting together that involves subclassing areas of Windows and dealing with certain messages. My problem is that Windows is sending a seemingly unidentifiable message that falls into the registered messages range. I know that if I could send this message, my program would go from a state of "Nice idea, shame about the implementation," to "Woohoo—it works!" What I can't figure out is how to identify the string that has been registered for this message.

A: Some background for those not familiar with registered messages—Windows provides a mechanism by which applications can generate a unique message number for their own use. An app can call the RegisterWindowMessage API, passing an identification string such as "MyAppsSpecialMessage", and Windows registers this string with a value that any other app can then retrieve by attempting to register the same string. Registered messages fall into this range: &hC000& (49152) to &hFFFF& (65535). The trouble is, there is no obvious method by which an application can do the reverse—retrieve the string given a message number.

The clue needed here is the explicit range used by registered messages. Searching MSDN turns up a variety of other things that also fall into this exact range, among them global atom values, registered clipboard formats, and system-wide hotkeys. Two of these provide reverse lookups that offer potential means of locating what you need. Trying GlobalFindAtom reveals that Windows uses a different table to store these string values. But a quick call to GetClipboardFormatName shows you can retrieve a registered message name as if it were a clipboard format name.

In fact, Windows seems to cache a variety of different strings within this single table. To get an idea of how many different uses this core algorithm addresses, you can iterate the entire registered message range, calling GetClipboardFormatName against each possible value and adding the returned strings to a listbox (see Listing 1). You'll see quite a range of items, many unreadable, but many instantly recognizable. —K.E.P.

Updated Code Samples
•  EnumMsgs

About the Author
Karl E. Peterson is a GIS analyst with a regional transportation-planning agency and serves as a member of the VSM Technical Review and Editorial Advisory boards. Online, he's a Microsoft MVP and a section leader on several VSM forums. Find more of Karl's VB samples at