473,414 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Requested clipboard operation did not succeed

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
1 18598
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: xyz | last post by:
I have a menu item to handle clipboard actions (cut, copy, paste). When I paste text that I copied from a Hungarian web page, the display looks normal on my RichTextBox, but the text retrieved from...
0
by: Dennis | last post by:
Greetings, I am having a wierd problem with .net accessing the clipboard on certain machines. The code copies the RTF formatted data from the clipboard. I first try and retrieve the clipboard...
0
by: Christopher | last post by:
Hello, First, I appologize for the length but I want to get all the facts down so as not to waste any time :) I'm trying to allow the user of my C# app to copy a DataGrid full of double values...
0
by: geo24 | last post by:
Have a problem with clipboard. Using the standard way for copying the image data: read the image into "myBitmap" copy the image into clipboard: DataObject myData = new DataObject();...
8
by: Dinesh Jain | last post by:
Hi all, I encountered a serious problem while working with clipboard class in VB.NET. I want to simulate cut-copy-paste operation with my application and Windows Explorer. User can copy files...
15
by: Peter Duniho | last post by:
I'm trying to use .NET and C# to draw a metafile copied to the clipboard by another application (Word 2003 in this case, but it shouldn't matter). I naively thought that I'd be able to use the...
1
by: Ludwig Wittgenstein | last post by:
Hi, all. Here's the scenario: I embedd a .txt file in Word as OLE object, then I copy it (manually), so now it's on the clipboard. How do I programatically (in C#) get that OLE object file from...
5
by: exhuma.twn | last post by:
As many might know, windows allows to copy an image into the clipboard by pressing the "Print Screen" button on the keyboard. Is it possible to paste such an image from the clipboard into a "Text"...
1
by: info | last post by:
Hi All, I am struggling with this from 48 hours and cannot succeed, of course I've tried: DataObject dObj = new DataObject(); dObj.SetData(DataFormats.Bitmap, false, bitmap);...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.