How ownership of the Windows clipboard is tracked in Win32

Raymond Chen

In Win32, there is the concept of a clipboard owner. How does that work?

The intended rule is that the clipboard owner is the window that created the data that is currently on the clipboard.

The intended usage pattern for putting data on the clipboard is

  • Call Open­Clipboard(hwnd) passing the window that you want to become the new clipboard owner. (For the purpose of discussion, let’s say that the clipboard opener is the window handle that was passed to the Open­Clipboard function.)
  • Call Empty­Clipboard() to wipe out the previous contents.
  • Call Set­Clipboard­Data() once for each piece of data you want to put onto the clipboard.
  • Call Close­Clipboard() to indicate that you are done setting the clipboard data.
  • Congratulations, you are now the clipboard owner.

The clipboard owner receives a WM_RENDER­FORMAT message when somebody requests data from the clipboard that had been set as delay-rendered. It also receives a WM_RENDER­ALL­FORMATS message as part of the window destruction sequence if it is still the owner of the clipboard at the time it is destroyed. Delay-rendering lets you defer the creation of complicated clipboard data until the point it is requested. And if nobody ever requests it, then you saved yourself a lot of work.

Bonus reading: What is the proper handling of WM_RENDER­FORMAT and WM_RENDER­ALL­FORMATS?

The clipboard owner also receives a WM_DESTROY­CLIPBOARD message when the clipboard contents are subsequently emptied. This tells the clipboard owner that it can free any memory that it had been using to produce the delay-rendered data.

The intended usage pattern for reading data from the clipboard is

  • Call Open­Clipboard(hwnd) passing the window that is reading the clipboard.
  • Call Get­Clipboard­Data() to retrieve data from the clipboard.
  • Call Close­Clipboard() to indicate that you are done reading the clipboard data.

If everybody follows the rules, then it all works out.

Spoiler alert: Not everybody follows the rules.

Some programs open the clipboard with the intent of adding data to the clipboard, rather than replacing it with new data. Those programs call Open­Clipboard, followed immediately by Set­Clipboard­Data without an intervening Empty­Clipboard.

There was historically no enforcement that the caller of Set­Clipboard­Data is the clipboard owner. Back in the days of 16-bit Windows, the system assumed that applications were honest and played by the rules for the common good. (And for compatibility, these shenanigans need to continue to work, even though the world has become a more dangerous place.)

This “bonus clipboard data” scenario creates a bit of a problem, since there is only one clipboard owner (which you can interrogate by calling Get­Clipboard­Owner()), but there are now two windows who collaborated to put data onto the clipboard. Who is the owner?

Ownership of the clipboard changes as follows:

  • When Empty­Clipboard() is called, the current clipboard opener becomes the clipboard owner.
  • When the clipboard owner is destroyed, the clipboard owner resets to null.

Therefore, the clipboard owner can be summarized as “the window that most recently called Empty­Clipboard, if it still exists.”

Bonus chatter: There is also special handling of the case where somebody passes NULL to Open­Clipboard, indicating that “nobody” is opening the clipboard.

8 comments

Discussion is closed. Login to edit/delete existing comments.

  • cheong00 0

    It would be more neat if there could be multiple clipboard owners and each of them associated with their clipboard data.

    What’s the use of “clipboard owner” except in the “delayed rendering” scenario or to perform check on the “process integrity level”? Seems these all are related to the clipboard data themselves instead of the clipboard as a whole.

    • Alex Martin 0

      Under what valid conditions can data from multiple owners be on the clipboard simultaneously? It’s supposed to be cleared every time you copy something new, and you can only copy from one window at a time.

  • GL 0

    The bonus reading also explains why SetClipboardData does not change the ownership to the hwnd (“current opener”) passed into the last successful call to OpenClipboard — during WM_RENDERFORMAT, the current opener is the window requesting data, and the owner should still be the window rendering the data, which calls SetClipboardData.

  • Daniel Smith 0

    On the topic of copy/pasting, does anyone know why the selection behaviour when double clicking a word in a Windows text box always includes the whitespace after the word, rather than just the word itself? This really bugs me!

    I know there will be some obscure software that depends on this behaviour, but I wish there was a setting to exclude the whitespace.

    • Ed Jr 0

      I don’t have an answer to your question, but you can easily get rid of the space while selecting by double-clicking the word and moving the cursor to the right while still holding the button down. You can also select multiple words by doing the same thing, just moving to the last word (or special character) you want to select before releasing the button.

      • Daniel Smith 0

        That’s genius, I had no idea you could hold the mouse button down after a double click! I’ll definitely be using that trick going forward, thanks Ed 🙂

        • Ed Jr 0

          No problem! I wish someone had told me about this too 🙂

          You can similarly select entire paragraphs at a time by triple-clicking any part of the paragraph and dragging the cursor around.

          If you’d like to know about another obscure text selection trick: To select larger portions of text, there’s a much easier way than holding down the left button and dragging the cursor around. You can simply left-click the portion of the text just before the first character you want to select, then hold Shift and click just after the last character. If you want to change your selection, you can keep holding Shift and clicking (or dragging) until you get it right.

          By the way, quite a few of the tricks I know I learned completely by accident. It’s funny how many years, decades we let go by oblivious to these amazingly simple yet headache-saving tools. This reminded me to do something I’ve always thought about but keep forgetting: search for a compilation with more such tricks!

    • switchdesktopwithfade@hotmail.com 0

      Withoutspacestherereallyarenowords.

Feedback usabilla icon