Wednesday, March 21, 2012

Just how does the Windows Clipboard work, really?

Ntdebugging Blog - How the Clipboard Works, Part 1

Recently I had the opportunity to debug the clipboard in Windows, and I thought I’d share some of the things I learned. The clipboard is one of those parts of Windows that many of us use dozens (hundreds?) of times a day and don’t really think about. Before working on this case, I had never even considered how it works under the hood. It turns out that there’s more to it than you might think. In this first article, I’ll describe how applications store different types of data to the clipboard and how it is retrieved. My next post will describe how applications can hook the clipboard to monitor it for changes. In both, I’ll include debug notes to show you how to access the data from the debugger.

Let’s start by discussing clipboard formats. A clipboard format is used to describe what type of data is placed on the clipboard. There are a number of predefined standard formats that an application can use, such as bitmap, ANSI text, Unicode text, and TIFF. Windows also allows an application to specify its own formats. For example, a word processor may want to register a format that includes text, formatting, and images. Of course, this leads to one problem, what happens if you want to copy from your word processor and paste into Notepad, which doesn’t understand all of the formatting and pictures?

The answer is to allow multiple formats to be stored in the clipboard at one time. When I used to think of the clipboard I thought of it as a single object (“my text” or “my image”), but in reality the clipboard usually has my data in several different forms. The destination program gets a format it can use when I paste.



We've only used the Windows Clipboard for decades, but how well do we know how it really works? This looks like a great "behind the music/inside baseball" series...

1 comment:

Anonymous said...

This little program shows what happens with the clipboard: