By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,273 Members | 1,032 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,273 IT Pros & Developers. It's quick & easy.

Requested clipboard operation did not succeed

P: n/a
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

Jun 27 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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

Jun 28 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.