Requested clipboard operation did not succeed

F

Figmo

Wow.....this is darned odd....

I have an app that integrates with other applications.

It registers a global hotkey with Windows. When the hotkey executes
it sends a CTRL-C to the active window (any application) to copy
whatever text is selected to the clipboard. It then issues a C#
Clipboard.GetText(...) and does some processing with this string.

All is good, no errors thus far.

My application has an option to then copy the processed text back to
the clipboard and send a CTRL-V to the original window to paste it
back in. Here is where the wierdness begins to happen. This optional
"PASTE BACK TO ORIGINAL WINDOW" code works without error. All I do is
issue a Clipboard.SetText(...) and then send the keystroke CTRL-V to
the original window. And when this code executes all behaves as
planned.

BUT....where the problem comes in is the NEXT TIME I try to activate
my hotkey. When my app tries to send the CTRL-C and issue a
Clipboard.GetText(...) again. I receive the exception "Requested
clipboard operation did not succeed". (from Clipboard.GetText() )

Through trial and error I have determined it's Clipboard.SetText()
that causes my problem. In other words, this happens even if I
comment out the CTRL-V lines of code. And the problem doesn't happen
if I do not execute Clipboard.SetText() but still send the CTRL-V
anyway.

I've added lots of diagnostic code to try and track this down. Added
some code to issue a Win32 GetOpenClipboardWindow() and then report
the process that currently has the clipboard open. It's NOTEPAD.EXE
(the "other" application I've been using for my testing).

It is NOT specific to NOTEPAD. I've tried other applications with the
same results. Anytime I execute my "paste" code, the next time I try
to read the clipboard it's locked by whatever the other application
is.

This ONLY happens when I perform the optional "paste back to original
window" code. I can repeatedly do the "copy from original window"
code over and over again without failure. So it seems to be
something I am doing - not something the other app is doing.

This does not last FOREVER. I've added a loop in my "read the
clipboard" method that waits until GetOpenClipboardWindow() returns
null before trying to read the clipboard. After a few seconds of
retrying...the clipboard eventually frees up and I can read no
problem. I would just leave this code in there and call it "fixed"
but this long delay really detracts from the usefullness of my
application.

Here is the "paste to original window" code I am using (the problem
only occurs after I execute this code)...

if (hwndActiveWindow == IntPtr.Zero)
return; //...no original active window to paste to

// Copy translated text from active tab to clipboard...
Clipboard.SetText("Some text here"); // <<<APPEARS TO BE
THE OFFENDING LINE OF CODE

// Make sure original active window is back to being
active again...
SetForegroundWindow(hwndActiveWindow);

// Send a CTRL-V to paste clipboard data to orignal active
window...
keybd_event(0x11, 0, 0, (IntPtr)0); //...CTRL key down
keybd_event(0x56, 0, 0, (IntPtr)0); //...V key down
keybd_event(0x56, 0, 0x02, (IntPtr)0); //...V key up
keybd_event(0x11, 0, 0x02, (IntPtr)0); //...CTRL key up

That's it. Nothing too complicated.

I've searched the web in vain for a clue as to what is going on
here.

Can somebody offer some insight? I value what's left of my hair too
much to pull it out over something that should be very simple...

-Figmo
 
F

Figmo

Wow - this just keeps getting wierder. I've simplified the problem
and it still makes no sense to me.

The problem is basically that Windows apps such as Notepad and
IExplorer (the only 2 I've tested with) seem to "hang on" to the
clipboard after a use keybd_event() to send a CTRL-C to that
application. But only if I've previously done a paste using CTRL-V.

I've eliminated my code to the the paste. I can now make the problem
come and go just by pressing CTRL-V on the keyboard myself. If I
don't paste, my code that sends the CTRL-C using keybd_event() works
every time. If I paste text into notepad (either by pressing CTRL-V
myself, or using keybd_event() to do it) then after issuing a CTRL-C
using keybd_event() the clipboard stays locked by notepad for several
seconds before I can access it.

What's odd is if I eliminate my keybd_event() CTRL-C code and just
manually press the CTRL-C and CTRL-V when needed - all works fine. My
program can read the clipboard and write the clipboard with zero
delays.

So this suggests it's in my "copy" code. But my "copy" code is about
as simple as it can get. Here it is...

keybd_event(0x11, (byte)NativeWIN32.MapVirtualKey(0x11,0), 0,
(IntPtr)0); //...CTRL key down
keybd_event(0x43, (byte)NativeWIN32.MapVirtualKey(0x43, 0), 0,
(IntPtr)0); //...C key down
keybd_event(0x43, (byte)NativeWIN32.MapVirtualKey(0x43, 0), 0x02,
(IntPtr)0); //...C key up
keybd_event(0x11, (byte)NativeWIN32.MapVirtualKey(0x11, 0), 0x02,
(IntPtr)0); //...CTRL key up
string strClip = Clipboard.GetText(); //<<<THROWS EXCEPTION IF I"VE
PREVSIOUSLY DONE A PASTE

That's it. I've tried putting Sleep()'s betwee the copy and my
GetText(). I've tried clearing the clipboard before doing the CTRL-
C. I've tried using SendInput() instead of keybd_event(). The ONLY
thing I've found that has an effect is to comment out my keybd_event()
lines and manually pressing CTRL-C before pressing the hotkey that
invokes this code. That works EVERY TIME NO PROBLEM.

So what the hell is the difference between me pressing CTRL-C on the
keyboard, and using keybd_event() as above??

-Bill
 
F

Figmo

BWAAA HAH HA HAH HAH........KNEEL C#! KNEEL BEFORE YOUR COMPUTING
GAWD!!!

I fixed it!

I had previously thought that perhaps if I sent my CTRL-C key using a
different method this might have an effect. I tried substituting the
keybd_event() code with the new SendInput() that (according to the
docs) is the future for .NET environments. But doing this had no
effect on my problem. So I took it out in favor of the much easier to
read keybd_event() code.

So today, out of desperation, I tried substituting my keybd_event()
code with the Window.Forms.SendKeys() function. And low and behold.
It works. No more hanging up and waiting for the other application to
release the clipboard. I had considered this also a few days ago but
in my mind I thought SendKeys() was just a wrapper for keybd_event()
so never bothered to try it. Thank gawd for desperation.

Do not ask my why it works. There must be something fundementally
different behind the screens of SendKeys() than either keybd_event()
or SendInput() does. Whatever it is - I LIKE IT.

I post my solution here in hopes that some future engineer reading the
archives can benefit from my (now) half bald head.

-Figmo
 
Joined
Jul 26, 2010
Messages
30
Reaction score
0
Thank you.

The solution appears to have worked for me as well as soon as started using Forms.SendKeys().

I'm doing something similar where I would send either Ctrl+V or Ctrl+C to another application and then retrieve obtained data from clipboard.

Anonymous
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top