-
Notifications
You must be signed in to change notification settings - Fork 968
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
.NET 9 appears to break Windows API clipboard functionality #12184
Comments
cc: @JeremyKuhne can you think of any changes in WinForms that may have broken this? |
@lonitra can you look into this and see if this is on our end? |
It seems as though we are incorrectly clearing the clipboard. Documentation mentions to clear clipboard we should be calling @mfeemster Could you try replacing calls to Clipboard.Clear() with the following code and see if there is any other issues? if (Application.OleRequired() != ApartmentState.STA)
{
throw new ThreadStateException();
}
int hresult;
int retry = 10;
while ((hresult = OleSetClipboard(null)) != 0)
{
if (--retry < 0)
{
// clipboard is being used by something else
throw new InvalidOperationException();
}
Thread.Sleep(millisecondsTimeout: 100);
} where [DllImport("ole32.dll", ExactSpelling = true)]
public static extern int OleSetClipboard(System.Runtime.InteropServices.ComTypes.IDataObject? pDataObj); We plan to change our Clipboard.Clear() to similar code above after some more investigation as to why it had worked in the past. |
Thanks for the code sample. Yes, if I either do not call So there is something within |
Description
Using the Windows API call
SetClipboardData()
to send data to the clipboard works fine with a project target framework ofnet8.0-windows
. However, when I change the project to 'net9.0-windows', the same code fails to properly set the clipboard data.I've read that Windows is very picky under the hood about locking memory when using the clipboard, but it seems to work fine on .NET 8 regardless. So I am wondering if .NET 9 changed how things are done with system memory or perhaps made the rules more strict? If so and this was intentional, this should be included in the release notes/what's new documents.
Reproduction Steps
Project file:
Code for Program.cs:
Expected behavior
The value copied to the clipboard and read back from it is
"Hello World!"
on both .NET 8 and 9.Actual behavior
The value copied to the clipboard and read back from it is
"Hello World!"
for .NET 8 and the empty string""
for .NET 9.Regression?
It worked in .NET 8.
Known Workarounds
None.
Configuration
.NET 8 and 9
Windows 10
x64
Visual Studio 2022 Community edition 17.11.4
Other information
No response
The text was updated successfully, but these errors were encountered: