Skip to content

Generate generic <T> overloads for IID_PPV_ARGS pattern on COM methods - #1720

Merged
Jevan Saks (jevansaks) merged 1 commit into
mainfrom
user/jevansa/iid-ppv-args-generic-overloads
Jun 11, 2026
Merged

Jevan Saks (jevansaks) merged 1 commit into
mainfrom
user/jevansa/iid-ppv-args-generic-overloads

Conversation

@jevansaks

@jevansaks Jevan Saks (jevansaks) commented Jun 10, 2026 •

Copy link
Copy Markdown
Member

Summary

Auto-detect COM methods with the IID_PPV_ARGS pattern (a Guid* parameter immediately followed by a void** [ComOutPtr] parameter) and generate generic <T> overloads where the GUID is derived from typeof(T).GUID and the output pointer is typed as T.

Closes #374
Fixes #1604

Example

Before — callers must construct and pass the IID manually:

shellItem.BindToHandler(pbc, in bhid, typeof(IStream).GUID, out object streamObj);
IStream stream = (IStream)streamObj;

After — the generic overload handles it:

shellItem.BindToHandler<IStream>(pbc, in bhid, out IStream stream);

Generated overload shapes

Mode Signature Constraint
Marshaling (AllowMarshaling=true) BindToHandler<T>(..., out T ppv) where T : class
Non-marshaling (AllowMarshaling=false) BindToHandler<T>(..., out T* ppv) where T : unmanaged

Changes

  • GeneratorOptions.cs — Added FriendlyOverloadOptions.ComOutPtrGenericOverloads (default true)
  • settings.schema.json — Added friendlyOverloads.comOutPtrGenericOverloads for opt-out
  • Generator.cs / Generator.Invariants.cs — Extract IUnknownHelperMethods template method names to avoid duplicating QueryInterface<T>
  • Generator.FriendlyOverloads.cs — Pattern detection (DeclareComOutPtrGenericOverloads) and code generation (BuildComOutPtrGenericOverload)
  • COMTests.cs — 5 new tests for marshaling/non-marshaling, opt-out, template precedence, and IntPtr mode

Design decisions

  • Only matches void** (not typed pointers like ID2D1Effect**) — typed outputs already have their specific type
  • One IID_PPV_ARGS pair per method max to avoid overload explosion
  • CoCreateInstance<T> template retained for co-creatable class CreateInstance<T> integration
  • Template-based overloads take precedence over pattern-based ones
  • Skipped when UseIntPtrForComOutPointers is active (ppv is IntPtr, not castable)

@jevansaks Jevan Saks (jevansaks) added the enhancement New feature or request label Jun 10, 2026
@jevansaks
Jevan Saks (jevansaks) force-pushed the user/jevansa/iid-ppv-args-generic-overloads branch 5 times, most recently from d327d2b to 422814f Compare June 10, 2026 23:51
Auto-detect COM methods with the IID_PPV_ARGS pattern (a Guid* parameter
immediately followed by a void** [ComOutPtr] parameter) and generate
generic <T> overloads where the GUID is derived from typeof(T).GUID and
the output pointer is typed as T.

For marshaling mode: out T ppv where T : class
For non-marshaling mode: out T* ppv where T : unmanaged

Added friendlyOverloads.comOutPtrGenericOverloads option (default true)
to nativemethods.json for opt-out.

Co-authored-by: Copilot <[email protected]>
@jevansaks
Jevan Saks (jevansaks) force-pushed the user/jevansa/iid-ppv-args-generic-overloads branch from 422814f to 52643a7 Compare June 11, 2026 00:11
@jevansaks
Jevan Saks (jevansaks) marked this pull request as ready for review June 11, 2026 01:19
@jevansaks
Jevan Saks (jevansaks) merged commit f8e8367 into main Jun 11, 2026
19 checks passed
@jevansaks
Jevan Saks (jevansaks) deleted the user/jevansa/iid-ppv-args-generic-overloads branch June 11, 2026 17:18
This was referenced Sep 3, 2026
Jesse Stricker (jessestricker) pushed a commit to jessestricker/windows-theme-switcher that referenced this pull request Nov 14, 2026
Updated
[Microsoft.Windows.CsWin32](https://github.com/microsoft/CsWin32) from
0.3.330 to 0.3.371.

<details>
<summary>Release notes</summary>

_Sourced from [Microsoft.Windows.CsWin32's
releases](https://github.com/microsoft/CsWin32/releases)._

## 0.3.371


## Changes:

* #​1878: Initialize out SafeHandles before publishing them
* #​1875: Fix PInvoke017 for span-based string copies in using scopes

This list of changes was [auto
generated](https://devdiv.visualstudio.com/DevDiv/_build/results?buildId=15977588&view=logs).

## 0.3.369


## Changes:

* #​1870: Fix cross-assembly COM projections and shared helpers
* #​1871: Upload consumer-tested NuGet packages from PR builds

This list of changes was [auto
generated](https://devdiv.visualstudio.com/DevDiv/_build/results?buildId=15944789&view=logs).

## 0.3.367


## Changes:

* #​1864: Fix OverflowException for sign-extended handles in SafeHandle
conversions

This list of changes was [auto
generated](https://devdiv.visualstudio.com/DevDiv/_build/results?buildId=15771099&view=logs).

## 0.3.366


## Changes:

* #​1860: Update Win32 metadata to 71.0.37-preview
* #​1859: Generate SafeHandle owners for cleanup functions that return
BOOLEAN
* #​1844: Generate SafeHandle for RAIIFree-annotated parameters, and
make getting the Value from a SafeHandle easier (but still safe)

This list of changes was [auto
generated](https://devdiv.visualstudio.com/DevDiv/_build/results?buildId=15738141&view=logs).

## 0.3.363


## Changes:

* #​1846: Support TypeDefinition handles for struct-typed constants in
nesting and extension blocks
* #​1851: Switch builds to the VSEng-MicroBuildVSStable pool
* #​1850: Fix build-task LibraryImport generation with extensionReceiver
* #​1825: extensionReceiver with CsWin32RunAsBuildTask=true emits
unsupported LibraryImport partials

This list of changes was [auto
generated](https://devdiv.visualstudio.com/DevDiv/_build/results?buildId=15677307&view=logs).

## 0.3.360


## Changes:

* #​1849: Fix custom marshaller namespaces for root metadata types
* #​1848: Flat-namespace native typedef marshallers emit invalid C# in
build-task mode
* #​1828: Fix XML documentation with colliding Win32 ABI types

This list of changes was [auto
generated](https://devdiv.visualstudio.com/DevDiv/_build/results?buildId=15621650&view=logs).

## 0.3.358


## Changes:

* #​1843: Use published Win32 metadata for interior-pointer tests
* #​1840: Keep buffers containing interior pointers as native pointers
* #​1820: Exclude Foundation.Metadata from direct API generation

This list of changes was [auto
generated](https://devdiv.visualstudio.com/DevDiv/_build/results?buildId=15604398&view=logs).

## 0.3.355


## Changes:

* #​1838: Update Win32 metadata to 71.0.28-preview

This list of changes was [auto
generated](https://devdiv.visualstudio.com/DevDiv/_build/results?buildId=15590335&view=logs).

## 0.3.347


## Changes:

* #​1821: Honor configured visibility for IVTable

This list of changes was [auto
generated](https://devdiv.visualstudio.com/DevDiv/_build/results?buildId=15574118&view=logs).

## 0.3.346


## Changes:

* #​1831: Generate safe COM output pointer ABI
* #​1835: Add `CsWin32PlatformTarget` to generate arch-specific APIs in
AnyCPU projects
* #​1807: Update dependency Xunit.Combinatorial to 2.1.41
* #​1817: Update dependency Microsoft.NET.Test.Sdk to 18.10.1 -
autoclosed
* #​1814: Update nbgv and nerdbank.gitversioning updates to 3.10.94
* #​1815: Update dependency powershell to v7.6.6
* #​1811: Update dependency dotnet-coverage to v18.11.2
* #​1816: Update Dockerfile and global.json updates
* #​1823: Update dependency ubuntu to v26
* #​1808: Update actions/deploy-pages action to v5.0.1
<details><summary><b>See More</b></summary>

* #​1824: Update .NET SDK to 10.0.401

This list of changes was [auto
generated](https://devdiv.visualstudio.com/DevDiv/_build/results?buildId=15556629&view=logs).</details>

## 0.3.335


## Changes:

* #​1822: Fix IInspectable-derived COM interface layout
* #​1813: Fix GitHub release service connection

This list of changes was [auto
generated](https://devdiv.visualstudio.com/DevDiv/_build/results?buildId=15400584&view=logs).

## 0.3.333

### Feature Improvements

- **Smarter COM marshalling
([#​1771](microsoft/CsWin32#1771 CsWin32
now automatically marshals recognized COM outputs as WinRT objects when
appropriate.
- **Simpler IID/PPV calls
([#​1720](microsoft/CsWin32#1720 IID and
`void**` parameter pairs now produce generic `<T>` overloads.

Before:

```csharp
shellItem.BindToHandler(pbc, in bhid, typeof(IStream).GUID, out object result);
IStream stream = (IStream)result;
```

After:

```csharp
shellItem.BindToHandler<IStream>(pbc, in bhid, out IStream stream);
```

CsWin32 supplies the IID and returns the strongly typed interface
directly.

## Changes:


### Fixes:


* #​1745: Invalid Code Generated for CsWin32.UI.Accessibility
methods/interfaces/Delegates With `"allowMarshaling": true`
* #​1739: NullReferenceException from passing null as CallbackParam for
MiniDumpWriteDump

### Others:


* #​1812: Make generator path quoting test cross-platform
* #​1810: Preserve coclass generic type fields for AOT
* #​1809: Quote generator tool path in command line
* #​1771: Automatically marshal COM outputs as WinRT
* #​1789: Update dotnet monorepo to 5.9.0
* #​1797: Update dependency nerdbank.dotnetrepotools to v1.5.42
<details><summary><b>See More</b></summary>

* #​1803: Update dependency CSharpIsNullAnalyzer to 0.2.19 - autoclosed
* #​1801: Update dependency Microsoft.Windows.SDK.CPP to 10.0.28000.2705
* #​1800: Update dependency Microsoft.Windows.SDK.BuildTools to
10.0.28000.2705
* #​1788: Update dependency Microsoft.NET.Test.Sdk to 18.9.0
* #​1787: Update dependency powershell to v7.6.5
* #​1782: Update Dockerfile and global.json updates
* #​1786: Qualify special string typedefs (PCWSTR) to
Windows.Win32.Foundation for non-Win32 metadata
* #​1784: Update dependency NuGet.Protocol to 7.9.0
* #​1781: Null-terminate PCSTR friendly overload strings
* #​1775: Generated string-friendly overloads for PCSTR parameters do
not append a null terminator
 ... (truncated)

Commits viewable in [compare
view](https://github.com/microsoft/CsWin32/commits/v0.3.371).
</details>

[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=Microsoft.Windows.CsWin32&package-manager=nuget&previous-version=0.3.330&new-version=0.3.371)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

2 participants