How can I find out which process has locked me out of the clipboard?

Raymond Chen

A customer was trying to figure out why their program ran into sporadic problems where its attempts to access the clipboard fail with the error CLIPBRD_E_CANT_OPEN. They were wondering if there was a way to figure out the source of the problem, or at least some data they could collect to include with their telemetry so they could try to analyze the problem on their back end.

There are two functions for getting handles to windows related to the clipboard. The functions have similar names, but the names are clear once you know what they mean.

The first function is Get­Clipboard­Owner. This is the window that had the clipboard open when it called Empty­Clipboard, presumably in preparation for calling Set­Clipboard­Data to put new data on the clipboard. The clipboard owner is the window which will receive messages like WM_RENDER­FORMAT and WM_DESTROY­CLIPBOARD to inform it about requests to produce or destroy clipboard data.

The clipboard owner is not the window that prevented you from accessing the clipboard.¹

The window that prevented you from opening the window is reported by the Get­Open­Clipboard­Window function. This is the window handle that was passed to Open­Clipboard, and for which there has yet to be a corresponding Close­Clipboard.

Note that if your call to Open­Clipboard fails, it’s possible that your call to Get­Open­Clipboard­Window might return NULL. There is of course a race condition where the clipboard opener closes the clipboard just before you call Get­Open­Clipboard­Window. Another possibility is that the code that opened the clipboard passed NULL as the window handle, so there is no handle to return to you.

But if you’re just calling it for diagnostic purposes, say, to log why you got locked out of the clipboard, then these cautions aren’t really a big deal. You’re not responding programmatically to the clipboard opener; you’re just doing some logging to help understand the problem.

¹ I mean, it might happen to be the window just by coincidence.

1 comment

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

  • Joe Beans 1

    A few years ago I wrote a clipboard viewer that shows the current owner and current locker, the only thing I didn’t like about it is that I have to poll GetOpenClipboardWindow() once a second or so because there’s no notification when it changes.

Feedback usabilla icon