Skip to content

Add documentation for RCW marshalling - #2512

Open
Sergio Pedri (Sergio0694) wants to merge 1 commit into
user/sergiopedri/comwrappers-reentrant-marshallingfrom
user/sergiopedri/rcw-marshalling-docs
Open

Add documentation for RCW marshalling#2512
Sergio Pedri (Sergio0694) wants to merge 1 commit into
user/sergiopedri/comwrappers-reentrant-marshallingfrom
user/sergiopedri/rcw-marshalling-docs

Conversation

@Sergio0694

Copy link
Copy Markdown
Member

Summary

Adds docs/rcw-marshalling.md, a conceptual guide to how CsWinRT 3.0 creates RCWs when marshalling native Windows Runtime objects to managed, and links it from the existing COM interop guide.

Note

This targets #2511 rather than staging/3.0, because the documentation describes the re-entrancy behaviour as fixed by that PR. It should be merged after it.

Motivation

Nothing currently documents the decision logic in WindowsRuntimeComWrappers.CreateObject, which is the part people most often ask about: why a native object sometimes comes back as a fully typed projection and sometimes as an opaque wrapper, when GetRuntimeClassName is consulted, and how the answer depends on the static type at the callsite. The logic is well commented in the source, but those comments are spread across several files and assume a lot of surrounding context.

This came out of exactly that question being asked, and is written to be shareable with people who want to understand the model without reading the implementation.

Changes

  • docs/rcw-marshalling.md: new document covering the marshaller entry points and which of the two static callbacks each one supplies (including the fact that ordinary generated non-generic interfaces go through the callback-free entry point, while generic instantiations and selected manually-projected interfaces have callbacks); the CCW unwrap fast path and the existing-wrapper cache hit, which short-circuit the ladder entirely; the decision ladder itself as a Mermaid diagram plus a walkthrough of each rung; when GetRuntimeClassName is and is not called and what its result is used for; how the infrastructure behaves for missing, unrecognised and wrong runtime class names; and four worked scenarios.

  • docs/interop.md: link to the new document from the "Create RCW" section.

Notes

The resilience section deliberately separates two cases that are easy to conflate. Names we cannot resolve always degrade safely, costing specialization rather than correctness. Names we can resolve are trusted, so a native object reporting a wrong but known runtime class name can throw or yield an unexpected type — which is precisely why TryCreateObject is consulted before the type map, and why the IStringable / Windows.Foundation.Uri override exists.

The document is documentation only and does not change any behaviour.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@Sergio0694
Sergio Pedri (Sergio0694) force-pushed the user/sergiopedri/rcw-marshalling-docs branch from cf654a4 to 9c97feb Compare August 5, 2026 22:56
Adds `docs/rcw-marshalling.md`, explaining how CsWinRT 3.0 creates RCWs when
marshalling native Windows Runtime objects to managed, and links it from the
existing COM interop guide.

Nothing so far documented the decision logic in
`WindowsRuntimeComWrappers.CreateObject`, which is the part people most often
ask about: why an object sometimes comes back as a fully typed projection and
sometimes as an opaque wrapper, and how much of that depends on the static type
at the callsite.

The document covers:

- The marshaller entry points, and which of the two static callbacks each one
  supplies, including the fact that ordinary generated non-generic interfaces
  are marshalled through the callback-free entry point.
- The CCW unwrap fast path and the existing-wrapper cache hit, both of which
  short-circuit the ladder entirely.
- The decision ladder itself, as a diagram plus a walkthrough of each rung.
- When 'GetRuntimeClassName' is and is not called, and what its result is used
  for.
- How the infrastructure behaves for missing, unrecognised, and wrong runtime
  class names, separating the cases that always degrade safely from the
  recognised-but-wrong names that are trusted by design.
- Four worked scenarios.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 15b56f71-7292-4049-bf75-17f6198d7446
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CsWinRT 3.0 documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant