Skip to content

Clipboard.SetDataObject method regression in .NET 9.0.100-rc.2 #12789

Closed

Description

@akhromushkin

Description

Clipboard.SetDataObject method doesn't place a custom DataObject into the clipboard.

Reproduction Steps

  1. Create a .NET Framework WinForms (or WPF) project.
  2. Create a custom DataObject class descendant:
    public class SomeDataObject : DataObject {
        public static string Format = "SomeDataObjectId";
        public override string[] GetFormats() {
            return new string[] { Format };
        }
    }
  1. Call the Clipboard.SetDataObject(new SomeDataObject()); method to add the SomeDataObject's class instance into the clipboard.

Expected behavior

SomeDataObject is added into the clipboard

Image

And Clipboard.GetDataObject().GetDataPresent(SomeDataObject.Format) is true

Actual behavior

The clipboard doesn't contain the SomeDataObject instance

Image

And Clipboard.GetDataObject().GetDataPresent(SomeDataObject.Format) is false

Regression?

Yes, in .NET 8 everything works.

Known Workarounds

No response

Impact

No response

Configuration

$ dotnet --info
.NET SDK:
 Version:           9.0.100-rc.2.24474.11
 Commit:            315e1305db
 Workload version:  9.0.100-manifests.4872d5d5
 MSBuild version:   17.12.0-preview-24473-03+fea15fbd1

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.22631
 OS Platform: Windows
 RID:         win-x64
 Base Path:   C:\Program Files\dotnet\sdk\9.0.100-rc.2.24474.11\

Host:
  Version:      9.0.0-rc.2.24473.5
  Architecture: x64
  Commit:       990ebf52fc

.NET SDKs installed:
  9.0.100-rc.2.24474.11 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 9.0.0-rc.2.24474.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 9.0.0-rc.2.24473.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 9.0.0-rc.2.24474.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other information

No response

Activity

  1. miloush commented on Nov 11, 2024

    @miloush

    Please include a repro project. This seems like an unusual way to use data objects. Perhaps the regression is because of the binary formatter removal? https://devblogs.microsoft.com/dotnet/binaryformatter-removed-from-dotnet-9/.

  2. Kuldeep-MS commented on Nov 11, 2024

    @Kuldeep-MS

    @akhromushkin After following your reproduction steps, I encountered the error shown below. Could you please provide more detailed steps?
    Image

  3. akhromushkin commented on Nov 11, 2024

    @akhromushkin
    Author

    Perhaps is more related to WinForms. So I have attached several projects:
    Issue_10049_WPF.zip
    Issue_10049_WinForms.zip

  4. BlueBSH commented on Nov 21, 2024

    @BlueBSH

    This issue still exists in the RTM GA version of .NET9.0.0

  5. miloush commented on Nov 21, 2024

    @miloush

    This is all Winforms classes and should be moved to the Winforms repo.

  6. transferred this issue fromdotnet/wpfon Jan 15, 2025
  7. added
    💥 regression-release
    area-Clipboard
    on Jan 15, 2025
  8. JeremyKuhne commented on Jan 15, 2025

    @JeremyKuhne
    Member

    The WPF repro given is actually using the WinForms Clipboard class. You can't derive the DataObject class in System.Windows.

    I'm not seeing this with the latest fix for the Clipboard, but haven't root caused it yet.

    @lonitra fyi

  9. JeremyKuhne commented on Jan 16, 2025

    @JeremyKuhne
    Member

    @akhromushkin

    There are two things going on here. The first is an issue with how we're setting up the data to put it on the clipboard. Our constructor for DataObject isn't doing the right thing when you construct from another DataObject. That said, there is a secondary problem that you should have hit if we hadn't made that mistake.

    What was intended was related to the change in .NET 9 to use ComWrappers for OLE interop. We made the switch as built-in COM isn't available when trimming or using AOT. Changing to ComWrappers breaks implicit casting back to your original DataObject (which only built-in COM handles). To make these scenarios still work we manually extract the original DataObject provided and return it. Which is what will happen when the February servicing release for .NET 9 goes out.

    Doing so does give different behavior unfortunately as talking to a DataObject through its native interface gives different results than talking to it directly. You can see this in this case by creating the derived DataObject and call GetDataPresent directly on it, which will return false.

    To make the repro work in the current .NET 9 bits and after February:

    SomeDataObject data = new();
    // This next line works around the current issue.
    data.SetData(SomeDataObject.Format, data);
    Clipboard.SetDataObject(data);
    
    public class SomeDataObject : DataObject
    {
        public static string Format => "SomeDataObjectId";
        public override string[] GetFormats() => [Format];
    
        // This is needed to mimic what happens calling back through the native interfaces.
        // The other advantage is that you should get the same results no matter how you
        // interact with this DataObject.
        public override bool GetDataPresent(string format, bool autoConvert)
            => format == Format || base.GetDataPresent(format, autoConvert);
    }

    Please tag me directly to help get my attention on responses.

  10. added a commit that references this issue on Jan 16, 2025
    f2d300d
  11. added
    🚧 work in progress
    on Jan 16, 2025
  12. added
    servicing-consider
    on Jan 16, 2025
  13. added a commit that references this issue on Jan 16, 2025
    c00d772
  14. added a commit that references this issue on Jan 18, 2025
    2bfb292
  15. added
    servicing-approved
    and removed
    servicing-consider
    on Jan 21, 2025
  16. akhromushkin commented on Jan 21, 2025

    @akhromushkin
    Author

    @JeremyKuhne
    Thanks for explaining the cause of the problem and WA. I think it should work for our purposes.

  17. locked and limited conversation to collaborators on Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

💥 regression-release🚧 work in progressarea-Clipboardservicing-approved

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions