473,385 Members | 1,846 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,385 software developers and data experts.

Clipboard.Clear() problem

My Clipboard is getting into some very confused state where I can't even
clear it. No applications can access the clipboard in this state, and if I
run Clipboard.Clear() I get:

System.Runtime.InteropServices.ExternalException: Requested Clipboard
operation did not succeed.
at System.Windows.Forms.Clipboard.ThorwIfFailed(Int32 hr)
at System.Windows.Forms.Clipbard.SetDataObject(Object data, Boolean copy,
Int 32 retryTimes, Int32 retryDelay)
at System.Windows.Forms.Clipboard.Clear()

Is there some lower-level code or external procedure I can use to forcibly
clear or reset the clipboard?
Sep 5 '06 #1
7 19997
Some (all?) of the clipboard operations require an STA; are you perhaps
running as an MTA? Try adding [STAThread] to your Main() method - see if
this helps.

Marc
Sep 5 '06 #2
Yes, I'm running it in [STAThread]. (I had gotten a separate complaint for
that earlier on.)

Sep 5 '06 #3
Hi,

NET Clipboard.Clear() internally calls Win32 API OleSetClipboard() to
clear the clipboard content.

#OleSetClipboard
http://windowssdk.msdn.microsoft.com.../ms686623.aspx

When an application opens the clipboard (either directly or indirectly by
calling the Win32 OpenClipboard function), the clipboard cannot be used by
any other application until it is closed. If the clipboard is currently
open by another application, OleSetClipboard fails.

You can verify this by creating a simple program which opens the clipboard:

if (OpenClipboard(hWnd)) {
::MessageBox(hWnd, _T("Clipboard opened, no other application can
change the content of clipboard."), _T("Message"), 0);
CloseClipboard();
}

When the message box is shown, call Clipboard.Clear() from .NET will fail
with the exception you're experiencing.

From OpenClipboard()'s documentation:

#OpenClipboard
http://windowssdk.msdn.microsoft.com.../ms649048.aspx

You can also see this statement: OpenClipboard fails if another window has
the clipboard open.

So your issue seems some other application is opening the clipboard and
forget to call CloseClipboard() properly.

I hope this helps. Please feel free to post here if anything is unclear.

Sincerely,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Sep 6 '06 #4
OK, that is helpful, but since I can't figure out which process has stolen
the clipboard is there any way (doesn't have to be programmatic -- I'm that
desperate!) for me to forcibly reset or restart the clipboard so that I can
use it again?

Right now my only solution when the clipboard has been hijacked seems to be
to restart the computer!
Sep 6 '06 #5
Hi,

When an application opened the clipboard, as far as I know, other
applications can only open the clipboard when the application closes it (or
the application exits).

You can use following code to determine which process is currently opening
the clipboard:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace ConsoleApplication1
{
class Program
{
[DllImport("user32.dll")]
static extern IntPtr GetOpenClipboardWindow();

[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowThreadProcessId(IntPtr hWnd, out int
lpdwProcessId);

static void Main(string[] args)
{
IntPtr hwnd = GetOpenClipboardWindow();
if (hwnd != IntPtr.Zero)
{
int processId;
GetWindowThreadProcessId(hwnd, out processId);
Process p = Process.GetProcessById(processId);
Console.WriteLine(p.Modules[0].FileName);
}
}
}
}

I hope this helps. Please feel free to post here if anything is unclear.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Sep 7 '06 #6
Wow, perfect! I never would have found the rogue process without that code.
Thanks a million!
Sep 8 '06 #7
I'd just like to add my thanks for this.

Turns out a download manager called FlashGet was causing the problem, which caused my VS2005 app to fail when it tried to copy to the clipboard.

Telling FlashGet not to monitor the clipboard solved the problem.

I'd never have guessed that program was the culprit, so cheers :-)

From http://www.developmentnow.com/g/36_2...ar-problem.htm

Posted via DevelopmentNow.com Groups
http://www.developmentnow.com
Mar 30 '07 #8

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...
2
by: Sunny | last post by:
Hi I am using Data Objects in my Applications and need to clear the ClipBoard or Memory Buffer after some intervals How do i clear the contents of the System.ClipBoard With Regard Sunny
2
by: zbcong | last post by:
hello in my program,i have following method,it copy a jpg file to clipboard,and works well: Private Sub CopyImage() Dim file As System.IO.Stream =...
3
by: Patrick Porter | last post by:
I've been reading the recent posts about using the clipboard, and wanted to try something where i could copy and paste files and directroies using the clipboard instead of the copto() function. the...
7
by: lgbjr | last post by:
Hello All, I¡¯m using a context menu associated with some pictureboxes to provide copy/paste functionality. Copying the image to the clipboard was easy. But pasting an image from the clipboard...
0
by: sonali_reddy123 | last post by:
Hi all I have a problem regarding use of a clipboard functionality in .NET. As per my knowledge we can set the data in the clipboard of our own format. But is there any way to clear the data...
1
by: Robert Bravery | last post by:
Hi all, I'm using the following code to copy data from XL to the clipboard to be later usied in a datatable range = objSheet.get_Range("A22", "aa100"); range.Copy(Missing.Value).ToString();...
7
by: Newbie | last post by:
How do I clear the clipboard in VB.NET 2003? TIA Newbie
6
by: =?Utf-8?B?TWljaGFlbCAwMw==?= | last post by:
I need to disable the clipboard function in Windows XP. We are having a problem with users using CTRL+C in one program, then using CTRL+V in another. Specifically, they type their password into...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.