Copyright 1999-2007 Thornsoft Development, Inc.
It is often desirable to monitor clipboard events, so that you can adjust your user interface based on data being available in a certain type, or to automatically “grab” information, such as URLs. But Clipboard monitoring is tricky business. Primarily due to the nature of the clipboard messages being sent to a “chain”, instead of directly to each application. When an update occurs, a message is sent from Windows to the first app in the chain. That app must then notify the next guy, and the next guy, etc.. If anybody neglects to pass along messages, or doesn’t properly keep track of “who’s next”, then the whole thing falls apart, and clipboard messages get lost. Or worse, they start looping! (system failure comes shortly thereafter).
Complete documentation exists in the Microsoft Platform SDK (see the Reference below). But in a nutshell, and in English, here it is:
Your app joins the clipboard chain with a SetClipboardViewer call. The result of this call, is the handle to the next app. Since each app joins at the “head” of the chain, this handle is the app that USED to be at the head. Anyway, you need to store this variable so that when you receive updates, you can pass them along.
You’ll receive notification of clipboard updates via the WM_DRAWCLIPBOARD message. You can “do your thing” here, such as pasting the data into yourself, examining formats present on the clipboard, or whatever. But you MUST MUST MUST pass the message along to the “next app”.
Occasionally, you’ll receive a notification that an application has left the chain via the WM_CHANGECBCHAIN message. This needs to be passed along like any other. But there is a special situation here, which you need to handle. If the app leaving, is the app that is next in line behind you, then you need to update your “handle to the next app”, to point to the “app after that”, which is conveniently provided as the 2ND handle in the message.
When you shut down, even if it is an abnormal termination, you must, must, MUST leave the chain properly. Failure to do so leaves a mess behind, and the only way to correct is for the user to reboot. But they won’t, they’ll curse everyone else’s program for not getting clipboard updates, or for refusing to paste data.
Here is Microsoft’s way of explaining it:
Creating a Clipboard Viewer Window
A clipboard viewer window displays the current content of the clipboard, and receives messages when the clipboard content changes. To create a clipboard viewer window, your application must do the following:
- Add the window to the clipboard viewer chain.
- Process the WM_CHANGECBCHAIN message.
- Process the WM_DRAWCLIPBOARD message.
- Remove the window from the clipboard viewer chain before it is destroyed.
Reference: MSDN Article at: http://msdn.microsoft.com/library/psdk/winbase/clipbrd_19dj.htm
We have an example clipboard viewer as part of our “Clipboard Viewer / Ignore Demo”, which also demonstrates the use of the “Clipboard Viewer Ignore” data format. It’s a Delphi program, and you are welcome to examine it, or use it as a basis for something else. There is a better description of it on the CF_CLIPBOARD_VIEWER_IGNORE page.
- Failure to Process WM_CHANGECBCHAIN – The most common mistake is failing to process the WM_CHANGECBCHAIN message. If you think you’re handling it right, consult the documentation, and check again! It’s common to ignore it, which will certainly result in a severed chain. Also common is to forget to compare the handle of the app leaving, with the handle of the “next app in the chain”. If you don’t update your handle when the next guy ducks out, you’ll be left holding a handle to nowhere, which of course, breaks the chain. Or, if an app leaves the chain, and comes back in at the front, and you don’t record the fact that he left in the first place, then you set up a LOOP in the chain.
- Exiting Without Removing From The Chain – It is common to exit the program without removing yourself from the chain. Remember – even if you terminate abnormally, you need to remove yourself from the chain.
- Failure to Pass Messages – It’s amazingly common to fail to pass along messages at all. This results in immediate corruption of the chain, and angry Email from your customers.
- See Also: Common (General) Clipboard Mistakes
- Microsoft Platform SDK – Base Services | Interprocess Communication | Clipboard | Clipboard References | Clipboard Messages
- Microsoft Platform SDK – Base Services | Interprocess Communication | Clipboard | Using The Clipboard | Creating A Clipboard Viewer Window
The above two topics, while available in the MSDN, are currently online at:
In case they’ve moved it, do a search on: “Clipboard Viewer Windows”