Skip to content

Restore WPF Clipboard compatibility and native EMF support - #12036

Closed
Shyam-Gupta wants to merge 2 commits into
mainfrom
dev/shgu/wpfclipmain
Closed

Shyam-Gupta wants to merge 2 commits into
mainfrom
dev/shgu/wpfclipmain

Conversation

@Shyam-Gupta

@Shyam-Gupta Shyam-Gupta commented Oct 15, 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

Port the clipboard compatibility fixes, documentation, and regression coverage from the .NET 10 branch, including clipboard test worker cleanup.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 39b06362-230d-44dd-a59b-1c5b8ef73e85
@Shyam-Gupta Shyam-Gupta self-assigned this Oct 15, 2026
@Shyam-Gupta
Shyam-Gupta requested a review from a team October 15, 2026 21:21
@Shyam-Gupta Shyam-Gupta added the area-clipboard Issues related to DataObject and shared clipboard label Oct 15, 2026
@Shyam-Gupta
Shyam-Gupta requested a review from a team as a code owner October 15, 2026 21:21
Copilot AI balanced review requested due to automatic review settings October 15, 2026 21:21
@dotnet-policy-service dotnet-policy-service Bot added the PR metadata: Label to tag PRs, to facilitate with triage label Oct 15, 2026

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 pinned shared OLE dependency lacks newly referenced APIs, blocking compilation until the companion package update is included.

2 open findings
What changed in this PR

Restores WPF Clipboard compatibility after the shared OLE migration, addressing text-conversion and enhanced-metafile regressions.

Changes:

  • Restores OLE-backed retrieval, native format checks, and WPF bitmap mappings.
  • Adds native EMF transfer with independent handle ownership.
  • Expands compatibility tests and documents behavior differences.
File Description
src/​Microsoft.DotNet.Wpf/​tests/​UnitTests/​PresentationCore.Tests/​System/​Windows/​EmfTestData.cs Adds EMF fixtures and validation helpers.
src/​Microsoft.DotNet.Wpf/​tests/​UnitTests/​PresentationCore.Tests/​System/​Windows/​DataObjectTests.cs Tests metafile mappings and native media contracts.
src/​Microsoft.DotNet.Wpf/​tests/​UnitTests/​PresentationCore.Tests/​System/​Windows/​ClipboardTests.cs Expands Clipboard compatibility and ownership coverage.
src/​Microsoft.DotNet.Wpf/​src/​PresentationCore/​System/​Windows/​Ole/​WpfOleServices.cs Adds EMF handling, retries, and platform mappings.
src/​Microsoft.DotNet.Wpf/​src/​PresentationCore/​System/​Windows/​clipboard.cs Restores OLE proxy retrieval and native availability checks.
src/​Microsoft.DotNet.Wpf/​src/​Extensions/​PresentationFramework-SystemDrawing/​SystemDrawingExtension.cs Copies EMF handles and rejects WMF sources.
Documentation/​Clipboard-Behavior-Changes-9.0-to-10.0.md Documents regressions and coordinated compatibility fixes.

🧠 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 +214 to +216
ClipboardCore.GetDataObject<DataObject, IDataObject>(
out IDataObject? dataObject,
unwrapUserDataObject: false).ThrowOnFailure();
Pin the logical MemoryStream buffer span directly when it is exposable, retaining the ToArray fallback for non-exposable streams. Extend clipboard round-trip coverage for buffer visibility, nonzero offsets, and trailing capacity.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 39b06362-230d-44dd-a59b-1c5b8ef73e85

@JeremyKuhne JeremyKuhne left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please keep the changes that restore text recognition and vector-picture copying without requiring applications to change their copy settings. Do not make those repairs depend on the proposed source-retention cache or changes to WinForms' working text behavior. Update the shared dependency, test the final assembly combination, and make the documentation describe the specific behavior restored rather than claiming blanket historical compatibility.

The corresponding WinForms changes need to address the text and retention problems below before we take that shared implementation. The grid-copy example refers to WinForms, which also uses this shared code.

Text regressions

There are two concrete WinForms text changes to avoid. An application can publish a DataObject containing a string and then check whether the clipboard contains usable text before enabling Paste. Changing that check to require an explicitly stored Unicode-text entry can return false even though compatible text is available. This reintroduces the scenario reported in dotnet/winforms#14172.

Separately, when Unicode text is stored with format conversion enabled, asking whether "TEXT" is available still returns true, but retrieving "TEXT" now returns null. The same example returns the text on released .NET 10. Please preserve these working WinForms behaviors while keeping WPF's different conversion rules separate.

Data retention

Keeping the copied object alive while Windows needs it is correct. The problem is what happens after another application replaces the clipboard: Windows can release the old source, but the new static cache continues holding it. The cache is only cleared when a later managed read detects the replacement, or another managed set, clear, or flush removes it.

This is reachable through ordinary grid copying. A user can copy a large selection, copy something elsewhere, and leave the first application open. The old payload - and anything it references - can remain in memory for the rest of that process if no qualifying cleanup call occurs. This is one retained entry, not an ever-growing list, but that entry can be large. Garbage collection cannot free it while the static reference remains.

Lifetime followup

Lifetime changes are high risk without a demonstrated customer need. The .NET Framework and pre-.NET 9 clipboard implementations used built-in COM interop, with subtle interactions between reference counting, garbage collection, and finalization. Keep this follow-up separate and require a concrete failing customer scenario before changing lifetime handling. Any fix must preserve needed data and release obsolete sources without another clipboard call.

AI-generated review text, approved for posting by the reviewer.

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

Labels

area-clipboard Issues related to DataObject and shared clipboard 📭 waiting-author-feedback To request more information from author. PR metadata: Label to tag PRs, to facilitate with triage

Projects

None yet

3 participants