Common Mistakes
Jul 12th, 2007 by Chris Thornton
Copyright 1999-2007, Thornsoft Development, Inc.
Introduction
There are some ways of using the clipboard that seem ok while you’re developing the application, but which later on, cause trouble, either within your application, or when interacting with other apps or clipboard viewers. Some are errors, others are just things that you need to be careful about, so that compatibility problems are minimized.
Common Clipboard Abuses
- Multiple Updates – One common clipboard error is where an application updates the clipboard several times in a row, for the same operation. If the user copies something, then you should create one, and only one, clipboard update. You’re supposed to open the clipboard, erase the clipboard, add your data (for all formats), close the clipboard. It’s simple – open, clear, update, close. However, sometimes people (or large companies) write programs that open, clear, close, open, update, close. See those two open..close operations? That’s two WM_DRAWCLIPBOARD messages that go out on the chain. Even worse is when they use separate open,update,close operations for EACH FORMAT. There was one version of a popular spreadsheet (with X and L in the name) that would send over 20 clipboard updates when copying a graph. They would add a format each time.
You can use the “Clipboard Viewer / Ignore Demo” test application to see if you’re causing multiple updates. Download, unzip, run the included .EXE file, and turn on the clipboard monitoring. Watch the log, and the “update counter”. If you see it increment by 1, when you copy from your app, then you are ok. If you see it jumping by more than one, then you are causing multiple updates – that’s bad. Get the demo app here: http://www.thornsoft.com/dist/techsupport/ignoredemo.zip - Local Clipboarding – does your application cut ‘n’ paste locally, without using the clipboard? If so, then consider having a clipboard format available that will faithfully reproduce the data. And tell your users to enable this format in ClipMate, or other clipboard extender.
- Programmer Short-Cut – “Programs should not transfer data into our out of the clipboard without an explicit instruction from the user.” — Charles Petzold, Programming Windows 3.1, Microsoft Press, 1992
If you use the clipboard to move card faces, toolbar icons, or bits of text within a program, or between programs, without the user performing an explicit cut/copy or paste, then you are using the clipboard as a crutch. Remember – the clipboard is there for the convenience of the user, not the programmer. Do not do this! If you read chapter 16 “The Clipboard” of the book mentioned above, you’ll see that allowances are made for programs that are “specifically designed to manipulate the clipboard”, such as ClipMate.
Any time a program puts data onto the clipboard, without the user’s knowledge or consent, that’s an abuse. And it’s documented on the The n Habits of Highly Defective Windows Applications page. http://www.flounder.com/badprogram.htm#clipboard
Common Clipboard Implementation Mistakes
- Improper or Incomplete implementation of Clipboard Viewer.
While any app can use Clipboard Viewing to monitor the clipboard, not all apps implement this properly. If you don’t properly handle all clipboard message types, you will sever the clipboard chain, and cut off other apps from receiving messages. You need to do much more than just handle updates – you need to pass them along to the next app in the chain, and you need to TRACK who the “next app” actually is. It can change! And when you’re done, you need to gracefully exit the chain. If any of this is news to you, please review the Clipboard Viewer page. - Failure to CLEAR The Clipboard – If you don’t clear the clipboard before updating, any formats that you don’t explicitly overwrite, will be left over from before. This is bad. For example, let’s say that the user had copied this paragraph from his web browser. It’s on the clipboard in a variety of formats, such as CF_TEXT, Rich Text Format, CF_HTML, etc.. Now your app places some TEXT on the clipboard, without first erasing the clipboard. Works great if you paste into Notepad – you get the text from your app. But if the user pastes into Word, then he’ll get the Rich Text Format representation of the data from the web page. If he does a Paste Special | Unformatted Text, then he’ll get yours. He’ll be very confused, and angry. Be sure not to make this mistake.
- Non-Viable Clipboard Data – When data is copied to the clipboard, is it really ok to bring it back later? Sometimes there are private formats that have all of the information needed to faithfully reproduce the item copied, but it’s only good in the “here and now”. If you copy it today, make sure you can paste it tomorrow. That means no dependency on pointers to internal data structures, for example. There is nothing worse than pasting in an old clip from a clipboard viewer, and having your application GPF or go into a loop, because you didn’t think about having the data come back later on. You can test this easy with Windows. Just open the standard windows clipboard viewer. Copy something from your app. Save as TEST.CLP in the Windows clipboard viewer (clipbrd.exe or clipbook.exe). Reboot. Reload the clip in clipbrd.exe. Paste into your app. Did it work? Great. Did it crash? Ooops.
Solution? One approach would be that while pasting, check the clip before pasting the format in question. If it is invalid, don’t paste it. Warn the user to not bother capturing this format, as it isn’t useful later on. - See Also: Common Clipboard Viewer Mistakes
- Looking for ways to effectively build and deploy your application? You should consider learning about the Event Driven Application platform.
Reference
- 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:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/dataexchange/clipboard.aspIn case they’ve moved it, do a search on: “Clipboard Viewer Windows”
Is there any way to pull an “old clip” back? I created a long clip by copying some text I was entering as a journal entry on a web site. I then clipped something else to add to my entry. The next thing I knew I was “timed out of the site” and my long journal entry was gone. When I of course went to the “clip” it was not the long one of the journal entry which I had copied just in case something like that were to hapen; instead, it was the susequent one. Anywhere on my computer where that older clip would be?
Hi Larry,
Unless you’re running a clipboard manager such as ClipMate, your data is gone. The windows clipboard only holds one item, which is why I invented ClipMate back in 1991, to hold ALL data that has been copied.
Nice articles you have here about clipboard. Nice.
Could you please spare with me knowledge about how to overcome, that MS Ex*el “cannot empty the clipboard” erorr? Seems like your ClipMate catches this and ignores it, making some sound. It occurs when you try to move any text from one cell to another in that app. Well I made my own viewer and only need to catch any simple text, everything seems to work in windows, except that Ex*l . :(. It’s making me mad.
By the way, I will implement IGNORE_CLIPBOARD message in my viewer. Thank you.
My requirement is to “Add to Clipboard” mean i need to copy “A” object to clipboard from some program, then add “B” object to clipboard (given “A” and “B” are the same type data objects) then paste them to a place resulting in “A B”. There may be some different keyboard shortcut like ‘Ctrl-Alt-C’ or ‘Ctrl-Shift-C’ for the same.
Hi in my case i had a problem while doing files copy paste, the description will be bit lengthy, sorry for that. I have two folders: say 1 and 2. in 1 I have some no. of files. One application processes these files present in 1 and then paste it to 2. This is defined in process itself. Now after some time I copied and paste processed files from folder 2 to my commonly used folder let us say folder 3 then it copied the files from 1 instead of 2, though I copied from folder 2. Is this problem anyhow related with clipboard’s any limitation? Reply is warmly welcome…
This site really is the ultimate Clipboard resource. By far my favorite section of this page is:
“Programmer Short-Cut – “Programs should not transfer data into our out of the clipboard without an explicit instruction from the user.” — Charles Petzold, Programming Windows 3.1, Microsoft Press, 1992
If you use the clipboard to move card faces, toolbar icons, or bits of text within a program, or between programs, without the user performing an explicit cut/copy or paste, then you are using the clipboard as a crutch. Remember – the clipboard is there for the convenience of the user, not the programmer. Do not do this! […]Any time a program puts data onto the clipboard, without the user’s knowledge or consent, that’s an abuse. And it’s documented on the The n Habits of Highly Defective Windows Applications page. http://www.flounder.com/badprogram.htm#clipboard ”
Quoting Petzold’s “Programming Windows” and Habits of Highly Defective Windows Applications… now THAT’S telling ’em!! Sir, I would definitely nominate you for President of Worldwide Application Standards.
I wish all software was written with as much concern for the user as you show in these articles!! You hit the nail on the head when you talked about how there is nothing worse than a program that violates the trust of the user and destroys his data or interferes with another program improperly.