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_
. 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_
and WM_
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.
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.