-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clipboard.SetText fails with CLIPBRD_E_CANT_OPEN #9901
Comments
I get the same exception while using |
Here is possible answer for this question: https://stackoverflow.com/questions/68666/clipbrd-e-cant-open-error-when-setting-the-clipboard-from-net#comment1287439_69081 |
We retry too, same values as WinForms (10 times 100 ms), both for set data and flush: wpf/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/clipboard.cs Lines 484 to 515 in 9fe50c7
wpf/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/clipboard.cs Lines 141 to 163 in 9fe50c7
Introduced in .NET FW 4.5 |
Yes, that works for me as well. Setting |
For me the text isn't being retained after the app is closed even if the text is quite short. |
That sounds likes a noteworthy difference. @josefblaha can you check who is calling you with WM_RENDERFORMAT or WM_RENDERALLFORMATS? |
I made a small app to test various methods of copying to clipboard: https://github.com/josefblaha/clipboard-playground @miloush I'm not sure how to check for the caller. I tried handling the Edit: Apparently I mismatched clipboard owner and the app that opened the clipboard to request the format. Owner equal to my window is expected. |
|
In fact maybe you could try See also https://devblogs.microsoft.com/oldnewthing/20240410-00/?p=109632 |
For the records, I get the I tried calling private async Task<bool> CopyFromClipboard()
{
while (Clipboard.ContainsText())
{
try
{
Console.WriteLine(WinApi.GetOpenClipboardWindow()); // Prints 0
string text = Clipboard.GetText(); // Can throw ExternalException exception, i.e., CLIPBRD_E_CANT_OPEN
return true;
}
catch (ExternalException ex)
{
Console.WriteLine(WinApi.GetOpenClipboardWindow()); // Prints 0
await Task.Delay(5).ConfigureAwait(true);
}
}
return false;
} |
For me, What's interesting: as of today, the WPF With that being said, my current theory is that something is reading contents of the clipboard whenever it's set. Sometimes, it manages to render the data and release the clipboard before the flush or after it. It such cases, the operation succeeds. But sometimes it gets there first and locks the clipboard for a second or more. In those cases, the flush fails. I wonder how the OLE API set data render until the flush is called. Does it go through WM_RENDERFORMAT message? Maybe the flush kind-of locks itself. Suppose the following sequence:
|
Then I am afraid this is not going to be easy to figure out without a repro. If you are on Windows 11, I would expect it to behave similar as mine, i.e. the touch keyboard trying to render it, but that does report the handle correctly. Either way it doesn't explain why it would work for WinForms, unless it's just a race condition. |
This is becoming a fun problem; I just had It uses
|
In the search of the mysterious clipboard listener, I also tried to call |
I find the tool, https://github.com/walterlv/ClipboardViewer , which can view the Clipboard |
Description
Most of the time, the call to
Clipboard.SetText
takes several hundred milliseconds and then fails with CLIPBRD_E_CANT_OPEN in the flush operation.The WinForms implementation works fine. Why is that? What is special about the WPF implementation? What is this "flushing" concept anyway? In Win32 API, there is no flushing. You just allocate a buffer and handover its ownership by calling
SetClipboardData
.Reproduction Steps
Attempt to copy text to clipboard in a WPF app like this:
Expected behavior
It should place the text in clipboard immediately.
Actual behavior
Most of the time, it takes several hundred milliseconds and then fails with the following exception:
Regression?
No response
Known Workarounds
The same code using
System.Windows.Forms.Clipboard
instead ofSystem.Windows.Clipboard
works without a problem.Impact
No response
Configuration
.NET 8.0.400, Windows 11 23H2, x64
I don't think I have anything unusual installed that would monitor the clipboard and block it.
I tried disabling Windows clipboard history with no effect.
Other information
It doesn't look like a widespread issue, perhaps it's configuration specific.
Possibly related to #9106.
The reason I'm involved is that I'm trying to fix the clipboard issue in PowerToys: microsoft/PowerToys#25437
The text was updated successfully, but these errors were encountered: