Skip to content

Fix providing EnhancedMetafile to other applications through the clip… - #11988

Open
justinswork wants to merge 1 commit into
dotnet:mainfrom
justinswork:fix/11986-enhmf-clipboard
Open

justinswork wants to merge 1 commit into
dotnet:mainfrom
justinswork:fix/11986-enhmf-clipboard

Conversation

@justinswork

@justinswork justinswork commented Oct 1, 2026 •

Copy link
Copy Markdown

Fixes #11986

Depends on dotnet/winforms#15218.

This PR has to merge before that change flows into WPF, or WPF won't build.

Description

On .NET 10, the shared OLE code in System.Private.Windows.Core rejects every TYMED_ENHMF request with DV_E_TYMED, so WPF can't provide CF_ENHMETAFILE to other applications even though the format is still advertised.

  • Add WpfOleServices.AllowedTymeds, which also allows TYMED_ENHMF. It implements the new IOleServices.AllowedTymeds from dotnet/winforms#TBD.
  • Fix the TYMED_ENHMF branch of WpfOleServices.GetDataHere, which was unreachable until now. For a MemoryStream it never assigned the handle to the medium, so it returned S_OK with a null handle. It now sets tymed and hEnhMetaFile, and returns E_FAIL when there is no handle, as .NET 9 did.
  • Add a clipboard test that reads the EMF back with GetClipboardData, the way another application would. It's skipped until the winforms change flows in.

I'd like to request a backport to release/10.0 after this merges.

Customer Impact

EMF that WPF apps put on the clipboard can't be pasted into other applications, for example Word's Paste Special → Picture (Enhanced Metafile). It fails silently, and there's no workaround.

Regression

Yes. It works on .NET 9 and is broken on .NET 10.

Testing

Risk

Low. It only affects TYMED_ENHMF requests for EnhancedMetafile data, which always fail today, and restores the .NET 9 behavior.

…board

The shared OLE code in System.Private.Windows.Core only accepts
TYMED_HGLOBAL, TYMED_ISTREAM and TYMED_GDI, so every request for
CF_ENHMETAFILE (always TYMED_ENHMF) fails before WPF's handler runs.

- Add WpfOleServices.AllowedTymeds, which also allows TYMED_ENHMF. It
  implements IOleServices.AllowedTymeds once the companion
  dotnet/winforms change flows in, and must be in place before then.
- Fix the TYMED_ENHMF branch of WpfOleServices.GetDataHere, which never
  assigned the handle created from a MemoryStream and returned S_OK with
  a null handle. It now sets tymed and hEnhMetaFile, and returns E_FAIL
  when there is no handle, as .NET 9 did.
- Add a clipboard test that reads the EMF back the way another
  application would. It is skipped until the winforms change flows in.

Fixes dotnet#11986

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@justinswork
justinswork requested review from a team and a balanced review from Copilot October 1, 2026 02:11
@justinswork
justinswork requested a review from a team as a code owner October 1, 2026 02:11
@dotnet-policy-service dotnet-policy-service Bot added PR metadata: Label to tag PRs, to facilitate with triage Community Contribution A label for all community Contributions labels Oct 1, 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.

Copilot review overview

🟡 Changes recommended

Non-publicly-visible MemoryStream buffers still throw during enhanced-metafile retrieval.

Review effort: Balanced
Findings: 1 High severity

Open (1)
What changed in this PR

Restores WPF enhanced-metafile clipboard interoperability on newer .NET versions.

Changes:

  • Allows TYMED_ENHMF in WPF OLE services.
  • Correctly populates enhanced-metafile storage media.
  • Adds native clipboard interoperability coverage.
File Description
WpfOleServices.cs Enables and returns enhanced-metafile handles.
ClipboardTests.cs Tests native retrieval of clipboard EMF data.

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

@justinswork

Copy link
Copy Markdown
Author

May be implemented by #12036 instead, although this seems like a simpler, more targeted solution in my opinion

Shyam-Gupta added a commit to dotnet/winforms that referenced this pull request Oct 28, 2026
…nhanced metafiles (#15218)

Fixes dotnet/wpf#11986

Companion WPF PR: dotnet/wpf#11988

## Proposed changes

- Add `IOleServices.AllowedTymeds`, following the existing `#if NET
static abstract` pattern.
- `Composition` reads the allowed media types from the platform instead
of the hard-coded `TYMED_HGLOBAL | TYMED_ISTREAM | TYMED_GDI`, in both
the managed-to-native and native-to-managed adapters.
- `WinFormsOleServices` returns the old value, so WinForms behavior is
unchanged. WPF adds `TYMED_ENHMF` in the companion PR.

## Customer Impact

- On .NET 10, WPF apps can't provide `CF_ENHMETAFILE` to other
applications: every `TYMED_ENHMF` request fails with `DV_E_TYMED` before
WPF's handler runs. Pasting EMF into apps like Word fails silently, with
no workaround.

## Regression?

- Yes. .NET 9 WPF allowed `TYMED_ENHMF`, and the shared mask dropped it.

## Risk

- Low. WinForms gets the same value as before, and `IOleServices` is
internal.
- The value is per platform rather than added to the shared constant,
because WinForms can't supply or read EMF. A shared value would make
WinForms report EMF as available when it isn't.

## Test methodology

- Added `ManagedToNativeAdapterTests`, which fails with `DV_E_TYMED`
without this change.
- `System.Private.Windows.Core.Tests` passes on net11.0 and net481.
- Validated end-to-end with a WPF app on .NET 11 RC 1, using WPF built
with the companion change: EMF paste now works for delayed and flushed
data.

## Test environment(s)

- .NET SDK 11.0.100-rc.1.26420.103, Windows 11 (10.0.26200)
###### Microsoft Reviewers: [Open in
CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/dotnet/winforms/pull/15218)
@Shyam-Gupta

Copy link
Copy Markdown
Member

@dipeshmsft, @pranav-gupta-msft, @subhajitm - We validated this change and didn't find any regressions. Could you please review this PR? Also we plan to take it for servicing in 10.0 and 11.0 release. Thanks.

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

Labels

Community Contribution A label for all community Contributions PR metadata: Label to tag PRs, to facilitate with triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clipboard: EnhancedMetafile can no longer be provided to other applications in .NET 10 (TYMED_ENHMF rejected)

3 participants