Skip to content

Restore WPF Clipboard compatibility and native EMF support in .NET 10 - #12003

Draft
Shyam-Gupta wants to merge 5 commits into
release/10.0from
dev/shgu/clipboardTests10.0
Draft

Shyam-Gupta wants to merge 5 commits into
release/10.0from
dev/shgu/clipboardTests10.0

Conversation

@Shyam-Gupta

@Shyam-Gupta Shyam-Gupta commented Oct 6, 2026 •

Copy link
Copy Markdown
Member

Fixes #11968
Fixes #11986

Summary

This change restores established .NET 9 behavior for WPF Clipboard APIs after the move to the shared OLE implementation in System.Private.Windows.Core. It also adds comprehensive compatibility coverage for the public Clipboard APIs and restores native enhanced-metafile support.

Compatibility fixes

  • Return an OLE-backed data object from Clipboard.GetDataObject() so Windows-generated formats and delayed rendering remain observable.
  • Restore native format checks for Contains*, including MTA-safe behavior and Windows-synthesized formats.
  • Restore ANSI Text to UnicodeText Clipboard conversion.
  • Restore WPF-specific BitmapSource format mappings without exposing them from WinForms.
  • Preserve existing Clear, Flush, IsCurrent, and SetDataObject ownership semantics.
  • Retry transient CLIPBRD_E_CANT_OPEN failures when retrieving platform-specific bitmap and EMF data.

Enhanced metafile support

  • Opt WPF into native TYMED_ENHMF rendering and retrieval.
  • Support both Metafile and stream-backed EMF payloads.
  • Correctly assign native EMF handles to returned STGMEDIUM values.
  • Copy native handles before releasing the source medium.
  • Preserve caller ownership and source-object validity.
  • Support delayed rendering, Flush, and copy: true.
  • Return independently disposable results from repeated retrievals.
  • Reject WMF payloads incorrectly published as enhanced metafiles.
  • Restore mapped-format auto-conversion behavior.

Test coverage

The branch adds exhaustive coverage for:

  • Threading and apartment-state behavior.
  • Argument validation and exception contracts.
  • Text, audio, file-drop, bitmap, custom-format, and managed-object data.
  • Windows-generated and managed format conversion.
  • Clipboard ownership, replacement, persistence, and retries.
  • Legacy and typed Clipboard APIs.
  • Native EMF rendering, retrieval, mapping, streams, ownership, and disposal.
  • EMF versus WMF validation.

Final validation:

  • PresentationCore.Tests: 1,003 passed, 29 intentional skips, 0 failed.
  • Clipboard tests: 65 passed, 4 intentional framework-difference skips, 0 failed.
  • Repeated final runs completed without automatic retries or process crashes.

Dependency

This requires the corresponding System.Private.Windows.Core changes from dotnet/winforms, including:

  • Managed Clipboard owner lifetime fixes.
  • Stale-owner and exited-STA handling.
  • Platform-opted native Clipboard media.
  • Shared IDataObject retry handling.
  • Native EMF medium support and format enumeration.

For these changes we will need to update this PR with an updated version of Microsoft.Private.WinForms package once it is available.

Microsoft Reviewers: Open in CodeFlow

Use the OLE proxy and native format availability checks to restore
Windows-generated formats, MTA-safe Contains APIs, and legacy
BitmapSource mappings.

Add native enhanced-metafile rendering and retrieval with correct handle
ownership, transient clipboard retries, and comprehensive compatibility
tests.
@Shyam-Gupta Shyam-Gupta self-assigned this Oct 6, 2026
@dotnet-policy-service dotnet-policy-service Bot added the PR metadata: Label to tag PRs, to facilitate with triage label Oct 6, 2026
@Shyam-Gupta

Copy link
Copy Markdown
Member Author

Note: Build failures in this PR are expected as they need updated version of Microsoft.Private.WinForms NuGet package which will be available after corresponding WinForms PR gets merged.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The required System.Private.Windows.Core package has not been flowed, current builds fail, and documentation inconsistencies remain.

3 open findings
What changed in this PR

Restores .NET 9 WPF clipboard compatibility and native enhanced-metafile support through the shared OLE layer.

Changes:

  • Restores OLE proxy behavior, native format checks, and WPF format mappings.
  • Adds native EMF rendering, retrieval, copying, and ownership handling.
  • Expands clipboard and metafile compatibility tests and documentation.
File Description
Documentation/​Clipboard-Behavior-Changes-9.0-to-10.0.md Documents clipboard compatibility differences.
SystemDrawingExtension.cs Adds safe native EMF handle copying.
WpfOleServices.cs Implements native EMF media and WPF mappings.
clipboard.cs Restores OLE-backed retrieval and native presence checks.
ClipboardTests.cs Adds comprehensive clipboard compatibility coverage.
DataObjectMetafileTests.cs Tests managed metafile mappings.
DataObjectTests.cs Tests typed metafile retrieval behavior.
EmfTestData.cs Provides deterministic EMF/WMF test data.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +94 to +96
public static bool IsNativeTymedSupported(string format, TYMED tymed) =>
IsEnhancedMetafileFormat(format)
&& tymed.HasFlag(TYMED.TYMED_ENHMF);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. This remains an intentional open dependency: the corresponding dotnet/winforms changes must be merged, published in Microsoft.Private.Winforms, and flowed into this PR before merge. No WPF-only source change can satisfy this dependency.

Comment thread Documentation/Clipboard-Behavior-Changes-9.0-to-10.0.md Outdated
Add deterministic production retry coverage, update compatibility documentation, and consolidate duplicate metafile mapping tests.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: f02ed50a-b99c-49d8-af4c-55b817a5fc61

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The documentation misstates tested conversion and apartment behavior, and the required WinForms dependency update remains pending.

1 open finding
2 resolved since last review
Previously missed (1)

In code that hasn't changed since last review

Low severity Align coverage summary with current tests and threading behavior

Documentation/​Clipboard-Behavior-Changes-9.0-to-10.0.md:236

This coverage summary contradicts the tests added in this PR: the ANSI synthesis and GetText conversion tests use the live default copy: false object, and Contains* APIs are explicitly verified as MTA-safe rather than STA-only. Update these bullets so the documentation does not describe the regressions that this change restores.

🧠 Review effort: Balanced


Give feedback about Copilot approvals in this survey to enter a drawing for a $150 gift card.

Update the coverage summary to reflect live OLE-proxy text conversion and MTA-safe Contains APIs.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: f02ed50a-b99c-49d8-af4c-55b817a5fc61
@Shyam-Gupta

Copy link
Copy Markdown
Member Author

Addressed the latest review summary in 46f726e. The coverage section now documents live default copy:false ANSI-to-Unicode synthesis, OLE-proxy GetText conversion, and the intentional split between STA-only OLE-backed APIs and MTA-safe Contains* checks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

draft PR metadata: Label to tag PRs, to facilitate with triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants