请求的剪贴板操作失败

问题描述:

我已经使用vb.net 2003以及出现此错误的一些时间。任何人都可以知道这个错误是如何发生的以及如何解决它?请求的剪贴板操作失败

错误:请求的剪贴板操作失败

+0

你使用什么代码? – 2010-08-05 03:48:42

+1

如果有更多详细信息,这将是一个更好的问题。 – David 2010-08-05 04:00:32

+0

你想要做什么剪贴板操作?你正在复制,粘贴,清除,监控等?用户发起还是自动?帮我帮助你。改善你的问题,我会upvote它,并试图帮助你。 – 2010-08-08 16:00:11

我用Google搜索这个问题,看看有什么我会看到,很多人都问这个问题,他们都没有得到一个坚实的答案...

所以我去了MSDN文档,发现了一个说明,解释了大多数问过这个问题的人描述了什么......当用户在代码运行时切换到另一个应用程序时,症状通常会出现。音符下面引用,以链接的文档以下内容:

All Windows-based applications share the system Clipboard, so the contents are subject to change when you switch to another application.

An object must be serializable for it to be put on the Clipboard. If you pass a non-serializable object to a Clipboard method, the method will fail without throwing an exception. See System.Runtime.Serialization for more information on serialization. If your target application requires a very specific data format, the headers added to the data in the serialization process may prevent the application from recognizing your data. To preserve your data format, add your data as a Byte array to a MemoryStream and pass the MemoryStream to the SetData method.

The Clipboard class can only be used in threads set to single thread apartment (STA) mode. To use this class, ensure that your Main method is marked with the STAThreadAttribute attribute.

Special considerations may be necessary when using the metafile format with the Clipboard. Due to a limitation in the current implementation of the DataObject class, the metafile format used by the .NET Framework may not be recognized by applications that use an older metafile format. In this case, you must interoperate with the Win32 Clipboard application programming interfaces (APIs). For more information, see article 323530, "Metafiles on Clipboard Are Not Visible to All Applications," in the Microsoft Knowledge Base at http://support.microsoft.com .

http://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.aspx

有趣的是,这使得一个奇怪的行为,我在自己的应用程序中的一个注意到的感觉。我有一个应用程序写入Excel电子表格(实际上,其中数百个,每个修改数百个单元格)。我根本不使用剪贴板,只使用Interop API for excel,但在运行时,每次创建新电子表格时,剪贴板都会清除。就我而言,Excel正在搞乱剪贴板,即使没有明显的理由要这样做。我会把它记录下来,这是我们凡人永远不会理解的那些神秘的Windows现象之一。

无论如何,感谢您的问题,我想我理解我的问题,所以+1帮助我。

UltraVNC有没有机会运行。当该应用程序在客户端PC的后台运行时遇到问题。当我关闭VNC时,我可以成功复制到剪贴板。这不是一个令人满意的解决方案,但至少我知道我的情况是问题的根源。

我有一个错误,而试图:

Clipboard.Clear(); 
... 
Clipboard.SetText(...); 

为了解决它,我从user32.dll pinvoking一些方法替代Clipboard.Clear()

[DllImport("user32.dll")] 
static extern IntPtr GetOpenClipboardWindow(); 

[DllImport("user32.dll")] 
private static extern bool OpenClipboard(IntPtr hWndNewOwner); 

[DllImport("user32.dll")] 
static extern bool EmptyClipboard(); 

[DllImport("user32.dll", SetLastError=true)] 
static extern bool CloseClipboard(); 

... 

IntPtr handleWnd = GetOpenClipboardWindow(); 
OpenClipboard(handleWnd); 
EmptyClipboard(); 
CloseClipboard(); 

... 

Clipboard.SetText(...); 

我使用C#在这里,但VB版本可能是轻松创建它。