Repository navigation
Restore WPF Clipboard compatibility and native EMF support - #12036
Shyam-Gupta wants to merge 2 commits into
Conversation
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
There was a problem hiding this comment.
🟡 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.
| 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
left a comment
There was a problem hiding this comment.
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.


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
Clipboard.GetDataObject()so Windows-generated formats and delayed rendering remain observable.Contains*, including MTA-safe behavior and Windows-synthesized formats.TexttoUnicodeTextClipboard conversion.BitmapSourceformat mappings without exposing them from WinForms.Clear,Flush,IsCurrent, andSetDataObjectownership semantics.CLIPBRD_E_CANT_OPENfailures when retrieving platform-specific bitmap and EMF data.Enhanced metafile support
TYMED_ENHMFrendering and retrieval.Metafileand stream-backed EMF payloads.STGMEDIUMvalues.Flush, andcopy: true.Test coverage
The branch adds exhaustive coverage for:
Final validation:
PresentationCore.Tests: 1,003 passed, 29 intentional skips, 0 failed.Dependency
This requires the corresponding
System.Private.Windows.Corechanges fromdotnet/winforms, including:IDataObjectretry handling.For these changes we will need to update this PR with an updated version of
Microsoft.Private.WinFormspackage once it is available.Microsoft Reviewers: Open in CodeFlow