1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

Sending messages to non-windowed applications
Page 1: How Delphi dispatches messages in windowed applications
 Win prizes by sharing code!
Do you have some Delphi code you want to share? Are you interested in winning a prize for your work?
Delphi Programming Quickies Contest
 More of this Feature
• Page 2: How to send messages to non-windowed applications
• Page 3: Obtaining a unique message identifier. DeallocateHWND bug
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
 Related Resources
• Message handing with Delphi
• Windows API and Delphi
• Threading in Delphi
• Console Delphi application

Article submitted by Catalin Ionescu for the Delphi Programming Quickies Contest - Round #2 Winner!

Learn how to send signals to non-windowed applications by using AllocateHWND and DefWindowProc. In this article we also briefly describe what Delphi does in the background to intercept Windows messages, how can we write our own message handler for a windowed application and how to obtain a unique message identifier that we can safely use in our applications. We'll also discover and fix a small bug in the Delphi DeallocateHWND procedure along the route.

How Delphi dispatches messages in windowed applications
In the Windows environment a lot of messages flow in the background. Each time the mouse is moved, a key is pressed, a window is moved or needs to be redrawn and in many other cases one or more messages is generated and hopefully finds it's way to the appropriate message handler code that understands and reacts appropriately to that very specific message. To see some of the messages that are generated automatically by Windows or as a response to your input you can fire up WinSight and peek around. If we need to receive and react to one of these messages in our application we need to write a message handler. For example if we need to respond to Windows signaling our main form that it needs to erase the background (WM_ERASEBKGND message) the typical code that we should place in the interface may be:

procedure WMEraseBkgnd(var Message: TWMEraseBkgnd); message WM_ERASEBKGND;

Then in the implementation part we would write the actual code that erases the form background. This is called a message handler. Delphi needs to do a lot of work in the background to call the above code for each WM_ERASEBKGND message generated by Windows. First it needs to intercept all Windows messages targeted at our particular form. Then it needs to sort them out based on the message identifier (WM_ERASEBKGND in the above example) and find if we implemented a message handler for any of them. In our above code we signal Delphi that we're only interested in receiving background erase notifications and not, for instance, mouse clicks. So Delphi calls our message handler for all background erase notifications and one of its own message handlers for all other messages received by the form.

Fortunately Windows has a very convenient way of intercepting all messages passed to a form. Each form has a window procedure that gets called for each and every message the form receives. As a new form is created the operating system provides a default window procedure that can be changed later by the application that owns the form if it needs to react on one or more messages. This is exactly what Delphi does. As each new form is created Delphi overrides the default window procedure with its own message interceptor and dispatcher. It is then a simple matter of comparison to find the appropriate message handler, either internal or written by us, for each specific message identifier.

If we write a non-windowed application, for instance a console application or a non-interactive service, then Delphi doesn't have a default window procedure to intercept and thus we can't rely on him to call our message handler. We need to write our own window procedure. But, as our application doesn't have a window, neither do Windows provide us with a window procedure to intercept.

Isn't there a way to trick Windows in thinking we have a window? Find the answer on the next page...

Next page > How to send messages to non-windowed applications > Page 1, 2, 3

©2017 About.com. All rights reserved.