views:

472

answers:

3

Under what circumstances will the Win32 API function OleGetClipboard() fail and return CLIPBRD_E_CANT_OPEN?

More background: I am assisting with a Firefox bug fix. Details here:

bug 444800 - cannot retrieve image data from clipboard in lossless format

In the automated test that I helped write, we see that OleGetClipboard() sometimes fails and returns CLIPBRD_E_CANT_OPEN. That is unexpected, and the Firefox code to pull image data off the Windows clipboard depends on that call succeeding.

A: 

Is your test running over Terminal Services? See CLIPBRD_E_CANT_OPEN error when setting the Clipboard from .NET.

Tadmas
A: 

From what I see in MSDN it seems to imply that the problem originates with whomever tried to actually put the data in the clipboard, i,.e. the source of the data. If their call to OleSetClipboard() failed, for whatever reason, then you won't be able to extract stuff out. I would take a look at how the data is being put into the clipboard, and see if there's a test case that performs this (copying the data to the clipboard), and then causes the problem you're talking about.

Jim Crafton
+2  A: 

The documentation says that OleGetClipboard can fail with this error code if OpenClipboard fails. In turn, if you read that documentation, it says:

"OpenClipboard fails if another window has the clipboard open."

It's an exclusive resource: only one window can have the clipboard open at a time. Basically, if you can't do it, wait a little while and try again.

Mike Dimmick
This seems like the closest answer so far. We discovered that if we allow a context switch to occur between the time to copy our test BMP to the clipboard and the point at which we call OleGetClipboard() (by adding JavaScript setTimeout(..., 0) in the test code), the problem goes away.