Closed mfeemster closed 1 month ago
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 OleSetClipboard(null)
, but this is not what is happening in our Clipboard.Clear(). Currently it is unclear why this had worked in .NET 8, but correcting this behavior looks to produce expected results.
@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 OleSetClipboard
is:
[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 Clipboard.Clear()
or if I use the code you provided in my own clearing function, then everything works fine.
So there is something within Clipboard.Clear()
that is either affecting, or being affected by a change that was made in .NET 9.
Verified the issue with .NET SDK 9.0.100-rc.2.24475.2 build version, the issue has been fixed that the value copied to the clipboard and read back from it is "Hello World!" on .NET 9 as below screenshot.
Verified this issue in the latest .NET 9.0 SDK build: 9.0.100-rc.2.24475.2 + dlls built from Winforms repo main branch, the issue has been fixed that the value copied to the clipboard and read back from it is "Hello World!" on .NET 9 as below screenshot.
@lonitra This remains broken in .NET 9 RC 2. Was it included as part of that release?
The fix is included in 9.0 GA. This did not make it in time for RC2.
Verified this issue with .NET 9.0 GA test pass .NET SDK 9.0.100 build, the issue has been fixed that the value copied to the clipboard and read back from it is "Hello World!" on .NET 9 as below screenshot.
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