CLIPBRD_E_CANT_OPEN error when setting the Clipboard from .NET

.NetWpfClipboard

.Net Problem Overview


Why does the following code sometimes causes an Exception with the contents "CLIPBRD_E_CANT_OPEN":

Clipboard.SetText(str);

This usually occurs the first time the Clipboard is used in the application and not after that.

.Net Solutions


Solution 1 - .Net

This is caused by a bug/feature in Terminal Services clipboard (and possible other things) and the .NET implementation of the clipboard. A delay in opening the clipboard causes the error, which usually passes within a few milliseconds.

The solution is to try multiple times within a loop and sleep in between.

for (int i = 0; i < 10; i++)
{
    try
    {
        Clipboard.SetText(str);
        return;
    }
    catch { }
    System.Threading.Thread.Sleep(10);
} 

Solution 2 - .Net

Actually, I think this is the fault of the Win32 API.

To set data in the clipboard, you have to http://msdn.microsoft.com/en-us/library/ms649048(VS.85).aspx">open it first. Only one process can have the clipboard open at a time. So, when you check, if another process has the clipboard open for any reason, your attempt to open it will fail.

It just so happens that Terminal Services keeps track of the clipboard, and on older versions of Windows (pre-Vista), you have to open the clipboard to see what's inside... which ends up blocking you. The only solution is to wait until Terminal Services closes the clipboard and try again.

It's important to realize that this is not specific to Terminal Services, though: it can happen with anything. Working with the clipboard in Win32 is a giant race condition. But, since by design you're only supposed to muck around with the clipboard in response to user input, this usually doesn't present a problem.

Solution 3 - .Net

I know this question is old, but the problem still exists. As mentioned before, this exception occurs when the system clipboard is blocked by another process. Unfortunately, there are many snipping tools, programs for screenshots and file copy tools which can block the Windows clipboard. So you will get the exception every time you try to use Clipboard.SetText(str) when such a tool is installed on your PC.

Solution:

never use

Clipboard.SetText(str);

use instead

Clipboard.SetDataObject(str);

Solution 4 - .Net

Actually there could be another issue at hand. The framework call (both the WPF and winform flavors) to something like this (code is from reflector):

private static void SetDataInternal(string format, object data)
{
    bool flag;
    if (IsDataFormatAutoConvert(format))
    {
        flag = true;
    }
    else
    {
        flag = false;
    }
    IDataObject obj2 = new DataObject();
    obj2.SetData(format, data, flag);
    SetDataObject(obj2, true);
}

Note that SetDataObject is always called with true in this case.

Internally that triggers two calls to the win32 api, one to set the data and one to flush it from your app so it's available after the app closes.

I've seen several apps (some chrome plugin, and a download manager) that listen to the clipboard event. As soon as the first call hits, the app will open the clipboard to look into the data, and the second call to flush will fail.

Haven't found a good solution except to write my own clipboard class that uses direct win32 API or to call setDataObject directly with false for keeping data after the app closes.

Solution 5 - .Net

I solved this issue for my own app using native Win32 functions: OpenClipboard(), CloseClipboard() and SetClipboardData().

Below the wrapper class I made. Could anyone please review it and tell if it is correct or not. Especially when the managed code is running as x64 app (I use Any CPU in the project options). What happens when I link to x86 libraries from x64 app?

Thank you!

Here's the code:

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

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

    [DllImport("user32.dll")]
    private static extern bool SetClipboardData(uint uFormat, IntPtr data);

    private const uint CF_UNICODETEXT = 13;

    public static bool CopyTextToClipboard(string text)
    {
        if (!OpenClipboard(IntPtr.Zero)){
            return false;
        }

        var global = Marshal.StringToHGlobalUni(text);

        SetClipboardData(CF_UNICODETEXT, global);
        CloseClipboard();

        //-------------------------------------------
        // Not sure, but it looks like we do not need 
        // to free HGLOBAL because Clipboard is now 
        // responsible for the copied data. (?)
        //
        // Otherwise the second call will crash
        // the app with a Win32 exception 
        // inside OpenClipboard() function
        //-------------------------------------------
        // Marshal.FreeHGlobal(global);

        return true;
    }
}

Solution 6 - .Net

Use the WinForms version (yes, there is no harm using WinForms in WPF applications), it handles everything you need:

System.Windows.Forms.SetDataObject(yourText, true, 10, 100);

This will attempt to copy yourText to the clipboard, it remains after your app exists, will attempt up to 10 times, and will wait 100ms between each attempt.

Ref. https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.clipboard.setdataobject?view=netframework-4.7.2#System_Windows_Forms_Clipboard_SetDataObject_System_Object_System_Boolean_System_Int32_System_Int32_

Solution 7 - .Net

This happen to me in my WPF application. I got OpenClipboard Failed (Exception from HRESULT: 0x800401D0 (CLIPBRD_E_CANT_OPEN)).

i use

ApplicationCommands.Copy.Execute(null, myDataGrid);

solution is to clear the clipboard first

Clipboard.Clear();
ApplicationCommands.Copy.Execute(null, myDataGrid);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRobert WagnerView Question on Stackoverflow
Solution 1 - .NetRobert WagnerView Answer on Stackoverflow
Solution 2 - .NetTadmasView Answer on Stackoverflow
Solution 3 - .Netpr0gg3rView Answer on Stackoverflow
Solution 4 - .NetYishai GalatzerView Answer on Stackoverflow
Solution 5 - .NetMarView Answer on Stackoverflow
Solution 6 - .NetBret PehrsonView Answer on Stackoverflow
Solution 7 - .NetEllix4uView Answer on Stackoverflow