PostMsg - Post a message to any window on the desktop | CodeGuru

PostMsg – Post a message to any window on the desktop

Environment: Windows NT/2000, Visual C++ 6.0 Purpose: One day while running an optimization utility against a VC++ project, I had the need for a tool that would allow me to cleanly shut down an out-of-process COM server. Without modifying the original source code, the best way that came to mind would be to simply post […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 23, 2001
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Environment: Windows NT/2000, Visual C++ 6.0

Purpose:

One day while running an optimization utility against a VC++ project, I had the need for a tool that would allow me to cleanly shut down an out-of-process COM server. Without modifying the original source code, the best way that came to mind would be to simply post a quit message to the server’s main application window. I then opened up the handy little SPY++ tool and found my window in the list, but to my disappointment I was not able to do anything interactively, so I decided to write PostMsg.

Functionality:

PostMsg is a simple dialog application that mimics part of the functionality of SPY++, in that it gets a list of all windows on the current desktop and displays them in a child-parent manner via a standard tree control, exactly like SPY++ does. The next thing the user will need is a list of common windows messages to select from. PostMsg uses a msg.dat file that was basically grep’d from winuser.h, so it should be fairly complete, and includes both the messages defined in hex and decimal values. PostMsg will look for msg.dat in it’s startup directory. New messages can be used by adding them to msg.dat and restarting the application. PostMsg also provides a case-insensitive way to do a substring search for a particular window title.

Advertisement

Technology:

PostMsg uses a single call to the ::EnumChildWindows() API function on the current desktop window to build it’s list of windows. Given how ::EnumChildWindows() iterates through the window list, I used a reverse-dependency loop via CTreeCtrl::GetParentItem() to determine the correct insertion point in the tree for each window. The search routine I wrote ended up being a recursive function because they are easy to read, small in size, and…I guess I just like recursive solutions.

Author’s Notes:

I added a couple buttons Hide and Show that would allow a quick way to manipulate windows. I find them useful for those annoying applications that don’t have a taskbar icon, or prevent you from repositioning or minimizing the window on the desktop. Those free ISP application banner windows come to mind as ideal candidates for this.

I should also note that PostMsg is a “Use at Your Own Risk” tool, as it allows message posting to virtually any window on the system, and this could cause problems if you aren’t careful.

Downloads

Download source – 31 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.