# Pitch: A vision for COM Interoperability in Swift

**URL:** <https://forums.swift.org/t/pitch-a-vision-for-com-interoperability-in-swift/86049>\
**Category:** Windows\
**Created:** [April 15, 2026, 7:58pm UTC](https://forums.swift.org/t/pitch-a-vision-for-com-interoperability-in-swift/86049 "2026-04-15T19:58:44Z")\
**Posts on this page:** 13\
**Page:** 5

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [November 9, 2026, 1:55pm UTC](https://forums.swift.org/t/pitch-a-vision-for-com-interoperability-in-swift/86049/81 "2026-11-09T13:55:22Z")

</div>

> [@TwirlySeal](#):
>
> > [@al45tair](#):
> >
> > The biggest single problem IMO is that COM interfaces get imported as _classes_. This is problematic on multiple fronts ...
> 
> On your second point, I completely agree that COM interfaces should be represented as protocols in Swift rather than classes ...

Out of curiosity, why would the protocols be better here instead of classes?

---

<div class="post-metadata">

**Author:** ![TwirlySeal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/twirlyseal/32/44467_2.png) [@TwirlySeal](https://forums.swift.org/u/TwirlySeal)\
**Post date:** [November 9, 2026, 2:26pm UTC](https://forums.swift.org/t/pitch-a-vision-for-com-interoperability-in-swift/86049/82 "2026-11-09T14:26:27Z")

</div>

I think the main reason is so that multiple COM interfaces can be implemented on the same Swift type, which isn't possible with single inheritance from a class. C++ relies on multiple inheritance to do this, but protocol import for pure virtual classes is a more conservative solution. My thinking is that although C++ interop is a major goal for the language, the LSG might be hesitant to allow full multiple inheritance in Swift (though correct me if I'm wrong about that). It is quite uncommon for multiple inheritance to be a strict requirement in C++ outside of interfaces, so I think this achieves most of what we want.

---

<div class="post-metadata">

**Author:** ![al45tair](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/al45tair/32/17985_2.png) [@al45tair](https://forums.swift.org/u/al45tair)\
**Post date:** [November 9, 2026, 3:52pm UTC](https://forums.swift.org/t/pitch-a-vision-for-com-interoperability-in-swift/86049/83 "2026-11-09T15:52:38Z")

</div>

> [@TwirlySeal](#):
>
> Furthermore, `__stdcall` is [ignored on 64-bit architectures](https://learn.microsoft.com/en-us/cpp/cpp/stdcall?view=msvc-170#:~:text=On,stack); it only makes a difference on 32-bit architectures.

It's worth noting here that, as pitched, I believe this is intended to work for 32-bit programs as well as 64-bit programs, hence the calling convention being an issue still.

> [@TwirlySeal](#):
>
> My argument is simply that pure virtual classes are the standard way interfaces are defined across all of C++, not just COM. Being able to import pure virtual classes as protocols would be widely useful for C++ interop on every platform

I don't disagree that it would be useful if the C++ interop code imported virtual base classes as protocols, though I think it's slightly trickier to do that overall, since whether a virtual base class is being used as an interface type or not is at least in part up to the programmer (it probably is if it's entirely pure, but maybe some methods aren't pure and instead have a default implementation of something, or maybe there are non-virtual methods, and so on…). It doesn't sound like a trivial problem trying to identify exactly which things should be protocols and which should be classes. @egor.zhdan might have further comments about that also.

The point though is that most of the code changes in the compiler are things you would ideally want for C++ interop also; the previous objection that there are too many compiler changes and it could all be done in a library has, I think, missed that point slightly.

> [@tera](#):
>
> Out of curiosity, why would the protocols be better here instead of classes?

You can look at this two different ways, I think:

On the one hand, Swift doesn't support multiple inheritance, but you definitely want to be able to have types conform to multiple protocols (or implement multiple interfaces, in COM speak).

On the other, Swift protocols are the logical equivalent of interface types from other languages, or indeed from COM — they specify an interface without specifying the implementation or any details of data storage.

---

<div class="post-metadata">

**Author:** ![compnerd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/compnerd/32/2165_2.png) [@compnerd](https://forums.swift.org/u/compnerd)\
**Post date:** [November 9, 2026, 5:48pm UTC](https://forums.swift.org/t/pitch-a-vision-for-com-interoperability-in-swift/86049/84 "2026-11-09T17:48:17Z")

</div>

> [@TwirlySeal](#):
>
> To clarify on the calling convention, the `Unknwn.h` header from the Windows SDK I showed explicitly marks COM interface methods with `STDMETHODCALLTYPE` (`__stdcall`), not `__thiscall` as Saleem said. Furthermore, `__stdcall` is [ignored on 64-bit architectures](https://learn.microsoft.com/en-us/cpp/cpp/stdcall?view=msvc-170#:~:text=On,stack); it only makes a difference on 32-bit architectures. `__thiscall` is [also ignored](https://learn.microsoft.com/en-us/cpp/cpp/thiscall?view=msvc-170#:~:text=On%20ARM%2C%20ARM64%2C%20and%20x64%20machines%2C%20%5F%5Fthiscall%20is%20accepted%20and%20ignored%20by%20the%20compiler%2E%20That%27s%20because%20they%20use%20a%20register%2Dbased%20calling%20convention%20by%20default) on 64-bit architectures. This header is the authoritative source for the ABI of `IUnknown` from Microsoft and it is 100% correct. While I built the COMTest demo using Swift (and transitively Clang) rather than MSVC, Clang on Windows natively targets the MSVC C++ ABI.

The calling convention still passes `this` through a register on 64-bit platforms, so it is implicit. As @al45tair mentioned, that there are still ABI differences. DirectX actually has instances of this happening - the C++ ABI is **not** COM compatible, and a missed C ABI conformance does break interoperability.

> [@al45tair](#):
>
> On the one hand, Swift doesn't support multiple inheritance, but you definitely want to be able to have types conform to multiple protocols (or implement multiple interfaces, in COM speak).

FWIW, this is primarily what the Sema related changes are shaped after - validating the inheritance chain. There is some minor other items (e.g. parameters must be C ABI compatible for COM method, COM runtime module is complete, and with the Microsoft model, the IID/CLSID rules are uphelp properly).

---

<div class="post-metadata">

**Author:** ![TwirlySeal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/twirlyseal/32/44467_2.png) [@TwirlySeal](https://forums.swift.org/u/TwirlySeal)\
**Post date:** [November 9, 2026, 9:01pm UTC](https://forums.swift.org/t/pitch-a-vision-for-com-interoperability-in-swift/86049/85 "2026-11-09T21:01:17Z")

</div>

> [@al45tair](#):
>
> It's worth noting here that, as pitched, I believe this is intended to work for 32-bit programs as well as 64-bit programs, hence the calling convention being an issue still.

It will work on 32-bit programs since `__stdcall` is included in the code blocks I have shown.

> [@compnerd](#):
>
> DirectX actually has instances of this happening - the C++ ABI is **not** COM compatible, and a missed C ABI conformance does break interoperability.

`win32metadata` explicitly flags these exact methods in DirectX, and I would generate different code for them that matches the headers in the Windows SDK. My point is that whatever I do will match what Microsoft does in its headers which are used in C and C++ development.

> [@al45tair](#):
>
> I don't disagree that it would be useful if the C++ interop code imported virtual base classes as protocols, though I think it's slightly trickier to do that overall, since whether a virtual base class is being used as an interface type or not is at least in part up to the programmer (it probably is if it's entirely pure, but maybe some methods aren't pure and instead have a default implementation of something, or maybe there are non-virtual methods, and so on…). It doesn't sound like a trivial problem trying to identify exactly which things should be protocols and which should be classes. @egor.zhdan might have further comments about that also.

What classes are interfaces is indeed based on programmer intent, so I think a Clang attribute applied to the C++ class/struct giving this information would be most appropriate to avoid determining this based on heuristics. Once this attribute is applied, the compiler can check whether it contains any requirements that aren't representable in a protocol and emit an error if that check fails.

> [@al45tair](#):
>
> The point though is that most of the code changes in the compiler are things you would ideally want for C++ interop also; the previous objection that there are too many compiler changes and it could all be done in a library has, I think, missed that point slightly.

I agree the compiler changes are quite similar between a C++ interop strategy and a COM interop strategy, but I was mainly advocating for less compiler work in relation to WinMD reading and projection of C, COM, and WinRT APIs in the compiler, which would be a larger scope increase. That is the part I think can all be done in a library. Expanding C++ interop is primarily a way to generalise these compiler changes so they aren't specific to COM and Windows and (from a design and separation of concerns perspective) focus on ABI primitives for projection generators instead of supporting arbitrary COM interface declarations in Swift, which I have reasoned would not gain much adoption. The lack of those relatively minor other items (such as the COM runtime module in the toolchain) in a C++ interop strategy is not because they pose a major implementation risk but rather to pursue a compiler focusing on generalised language semantics that benefit as many people as possible and keeping platform-specific details (COM) out of the toolchain. It also saves us from solving the same problems again later for C++ interop.

---

<div class="post-metadata">

**Author:** ![compnerd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/compnerd/32/2165_2.png) [@compnerd](https://forums.swift.org/u/compnerd)\
**Post date:** [November 9, 2026, 9:11pm UTC](https://forums.swift.org/t/pitch-a-vision-for-com-interoperability-in-swift/86049/86 "2026-11-09T21:11:03Z")

</div>

> [@TwirlySeal](#):
>
> win32metadata explicitly flags these exact methods in DirectX, and I would generate different code for them that matches the headers in the Windows SDK. My point is that whatever I do will match what Microsoft does in its headers which are used in C and C++ development.

Sure, I'm not saying that the interfaces cannot be interoperable with C++. Microsoft has spent a good deal of effort to try to make that work, but even then, it does break down in edge cases, e.g. `-fsigned-char` (the default on Windows) is technically relying on UB for C++ as `char` and `signed char` are technically distinct in C++ mode.

The point is, COM is most correctly represented in C's model, and we should be using C interop for this. Once you do that, you need interfaces, i.e. `protocol` types, and it is simply reality that the compiler needs to step in and help. Having a small surface that the compiler supports for this is not a bad thing. It does increase the barrier to entry to implement that, but that isn't insurmountable, and in fact, we have nearly a working implementation (at least conceptually with a PoC branch).

---

<div class="post-metadata">

**Author:** ![TwirlySeal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/twirlyseal/32/44467_2.png) [@TwirlySeal](https://forums.swift.org/u/TwirlySeal)\
**Post date:** [November 9, 2026, 9:52pm UTC](https://forums.swift.org/t/pitch-a-vision-for-com-interoperability-in-swift/86049/87 "2026-11-09T21:52:46Z")

</div>

The `char` edge case is relevant to C++ function overload resolution and name mangling, but COM interfaces do not link methods by mangled C++ symbol names, they use fixed vtable indices. Furthermore, I don't rely on ambiguous `char` types as `win32metadata` resolves primitives to explicit fixed-width types (`Int8` / `int8_t`, `UInt8` / `uint8_t`, `UInt16` / `wchar_t`), and there is zero undefined behavior when passing fixed-width integers across a virtual method boundary.

Virtually all of Microsoft's own internal code, games, and native Windows apps consume COM via C++ (using WRL, WIL, or C++/WinRT). If consuming COM in C++ were fundamentally broken in MSVC or Clang, Windows itself wouldn't compile.

Those DirectX edge case methods do need some C-style code generation, but I can easily emit that inside the same header with other C++ declarations. Solely using C interop throws away Clang's built-in knowledge of C++ vtables and inheritance for the rest of the 99% of Windows APIs, and has to reconstruct it specifically for COM. The ability to leverage both C and C++ interop in a projection generator is a strength, not a weakness. Clang attributes I have discussed for C++ interop such as `SWIFT_SHARED_REFERENCE` are also usable in pure C as it accepts C functions for the reference count operations, so most of my codegen will remain the same.

The compiler support is indeed a good thing, which is why I think we should generalise it for C++ interop on all platforms.

---

<div class="post-metadata">

**Author:** ![Nobody1707](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nobody1707/32/2169_2.png) [@Nobody1707](https://forums.swift.org/u/Nobody1707)\
**Post date:** [November 9, 2026, 9:59pm UTC](https://forums.swift.org/t/pitch-a-vision-for-com-interoperability-in-swift/86049/88 "2026-11-09T21:59:39Z")

</div>

> [@compnerd](#):
>
> it does break down in edge cases, e.g. `-fsigned-char` (the default on Windows) is technically relying on UB for C++ as `char` and `signed char` are technically distinct in C++ mode.

Whether char is signed is implementation defined behavior across both C and C++. Neither language guarantees either representation, and signed char and unsigned char are separate types in both. It's technically not undefined behavior to rely on char being signed, it's just a portability hazard.

---

<div class="post-metadata">

**Author:** ![al45tair](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/al45tair/32/17985_2.png) [@al45tair](https://forums.swift.org/u/al45tair)\
**Post date:** [November 10, 2026, 10:46am UTC](https://forums.swift.org/t/pitch-a-vision-for-com-interoperability-in-swift/86049/89 "2026-11-10T10:46:12Z")

</div>

> [@TwirlySeal](#):
>
> What classes are interfaces is indeed based on programmer intent, so I think a Clang attribute applied to the C++ class/struct giving this information would be most appropriate to avoid determining this based on heuristics.

Since we can't add the Clang attribute to Microsoft's headers, that means using API Notes (which, as I pointed out, could be generated from the `winmd` data; they don't need to be manually maintained).

> [@TwirlySeal](#):
>
> I was mainly advocating for less compiler work in relation to WinMD reading and projection of C, COM, and WinRT APIs in the compiler, which would be a larger scope increase.

That isn't part of this pitch, to be clear.

I happen to think it would be _better_ for all kinds of reasons, and we already have the tools (API Notes and Swift overlays) to fix any problems with imported data, but it's not part of this vision.

> [@TwirlySeal](#):
>
> Expanding C++ interop is primarily a way to generalise these compiler changes so they aren't specific to COM and Windows

I still think it's useful to have explicit support for COM per se on Windows. In order to make C++ interop a properly viable route for COM, we would need to:

- Add annotations to identify interface types.
- Add attributes to tell the compiler the reference counting rules for those interface types (COM reference counts _interfaces_, not _objects_; calling `AddRef` on one interface pointer does not necessarily increment the reference count for any other interface pointers you may have even if they were vended by the same object).
- Add a way to map the various different COM-specific attributes to an IID property (there are C macros in at least two flavours, plus `__declspec(uuid("{...}")`, and these are COM specific).
- Add a way to get C++ interop to generate appropriately laid-out vtables for COM.
- Add a way to indicate to C++ interop that, because it's a COM object, it should be `stdcall` and not `thiscall`.
- Add a way to tell the compiler to generate a `QueryInterface` function for you.
- Somehow maintain API Notes to attach all of these attributes to definitions we pick up from header files.

There are a lot of COM-specific things in that list, and _nothing about COM requires C++_. Moreover, in an ideal world you should be able to use COM from Swift code without activating C++ interop. (Turning on C++ interop for a module means that any users of types or functions from that module _also_ have to enable C++ interop; if we can do without it, that would be better.)

---

<div class="post-metadata">

**Author:** ![ktraunmueller](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ktraunmueller/32/46137_2.png) [@ktraunmueller](https://forums.swift.org/u/ktraunmueller)\
**Post date:** [November 10, 2026, 11:00am UTC](https://forums.swift.org/t/pitch-a-vision-for-com-interoperability-in-swift/86049/90 "2026-11-10T11:00:02Z")

</div>

> [@al45tair](#):
>
> Turning on C++ interop for a module means that any users of types or functions from that module _also_ have to enable C++ interop

That's the viral aspect that Saleem also previously mentioned, and I find this invasive nature of Swift's C++ interop to be a bit of a pain in practice.

In my mixed C/C++/Swift Windows (CMake-based) project, I had to introduce a separate Swift static library target for some Windows COM interop code that required the **C** view of COM interfaces. Not a huge problem, but not nice either.

---

<div class="post-metadata">

**Author:** ![compnerd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/compnerd/32/2165_2.png) [@compnerd](https://forums.swift.org/u/compnerd)\
**Post date:** [November 10, 2026, 5:37pm UTC](https://forums.swift.org/t/pitch-a-vision-for-com-interoperability-in-swift/86049/91 "2026-11-10T17:37:02Z")

</div>

> [@TwirlySeal](#):
>
> I was mainly advocating for less compiler work in relation to WinMD reading and projection of C, COM, and WinRT APIs in the compiler, which would be a larger scope increase.

FWIW, I'm still a bit unsure about this. I see @al45tair's point about it, but I also agree with your point here (and is where I originally started from). I am concerned about the amount of specialised knowledge that would put into the compiler. This of course becomes less of a concern with the recent Swift in Swift effort (swift-winmd is written as a Swift module that we _could_ pull in), but I'm not entirely sold on that belonging in the compiler as of yet.

---

<div class="post-metadata">

**Author:** ![compnerd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/compnerd/32/2165_2.png) [@compnerd](https://forums.swift.org/u/compnerd)\
**Post date:** [November 10, 2026, 5:43pm UTC](https://forums.swift.org/t/pitch-a-vision-for-com-interoperability-in-swift/86049/92 "2026-11-10T17:43:25Z")

</div>

I am going to be unavailable for a while, but I figured that I would drop a copy of my latest revision of the vision document to allow people to mull over it. I hope that the new version is clearer for people to follow. It is larger than what discourse allows to post, so I am sharing it as a gist:

> <https://gist.github.com/compnerd/44b7fc773b7e5624457a99f3cbf9ba32>

---

<div class="post-metadata">

**Author:** ![TwirlySeal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/twirlyseal/32/44467_2.png) [@TwirlySeal](https://forums.swift.org/u/TwirlySeal)\
**Post date:** [November 10, 2026, 10:33pm UTC](https://forums.swift.org/t/pitch-a-vision-for-com-interoperability-in-swift/86049/93 "2026-11-10T22:33:09Z")

</div>

> [@al45tair](#):
>
> Since we can't add the Clang attribute to Microsoft's headers, that means using API Notes (which, as I pointed out, could be generated from the `winmd` data; they don't need to be manually maintained).

I generate my own headers rather than annotating the Windows headers with API Notes for a few reasons:

- The Windows headers are hostile to modularity and pollute the global namespace. Including a single Windows header in C and C++ often pulls in the entire Windows API surface from transitive includes, which makes IDE completions useless. Un-projected symbols and symbols that the projection does not need also would still be visible to the user if I was annotating the original Windows headers. And it is not easy to programmatically identify all of the unwanted symbols. Windows API projections for other languages generate code for what they need instead of trying to fix the Windows headers.
- I want to make some other changes to the headers such as adding strongly typed handles (based on `win32metadata`) instead of aliases for void pointers.
- I would like to split everything into organised modules (`Networking`, `Graphics`, `Win32Graphics`, etc.) like `windows-rs` which is not possible with API Notes.
- There are many API refinements I would like to do that can't be expressed in API Notes, for example moving out pointers into the return type like `windows-rs`. I need to generate Swift wrappers for these.

We could add more features to API Notes to address this, but I think some of these refinements are too specific and complex to make sense as simple annotations. This is my most important feedback: whatever solution we go with, I hope the above refinements are possible.

> [@al45tair](#):
>
> - Add annotations to identify interface types.

This is true, and I think it would be useful for users of C++ interop as well.

> [@al45tair](#):
>
> - Add attributes to tell the compiler the reference counting rules for those interface types (COM reference counts _interfaces_, not _objects_; calling `AddRef` on one interface pointer does not necessarily increment the reference count for any other interface pointers you may have even if they were vended by the same object).

Clang attributes for foreign reference counting handle that in my COMTest demo, in a similar way to the `wil::com_ptr` smart pointer from the Windows Implementation Libraries for C++ but without a smart pointer type.

> [@al45tair](#):
>
> - Add a way to map the various different COM-specific attributes to an IID property (there are C macros in at least two flavours, plus `__declspec(uuid("{...}")`, and these are COM specific).

The projection generator knows the UUIDs so I place them in an IID property on the Swift side instead of relying on the C macros or MSVC extensions (`__declspec(uuid("{...}")` and `__uuidof`).

> [@al45tair](#):
>
> - Add a way to get C++ interop to generate appropriately laid-out vtables for COM.

C++ interop already does that because it uses the MSVC C++ ABI which the COM vtable layout was designed to match from the start. The COMTest demo shows this.

> [@al45tair](#):
>
> - Add a way to indicate to C++ interop that, because it's a COM object, it should be `stdcall` and not `thiscall`.

My headers include ` __stdcall` on all the COM interface methods so this already works. Even if I used Microsoft's original Windows headers without modifications or annotations, they still include `__ stdcall` and it would still work.

> [@al45tair](#):
>
> - Add a way to tell the compiler to generate a `QueryInterface` function for you.

I don't need the compiler to generate a `QueryInterface` function for me, I wrote it myself in Swift. It has the signature `queryInterface<T>(...) throws(COMError) -> T` and can be seen in the COMTest demo [here](https://github.com/TwirlySeal/COMTest/blob/962215229f00fb9c31dffa7e6e22e1047a17829c/Sources/COMTest/Projection.swift#L29).

> [@al45tair](#):
>
> - Somehow maintain API Notes to attach all of these attributes to definitions we pick up from header files.

I do not use API Notes so this would not be needed in my current approach.

> [@al45tair](#):
>
> There are a lot of COM-specific things in that list, and _nothing about COM requires C++_. Moreover, in an ideal world you should be able to use COM from Swift code without activating C++ interop. (Turning on C++ interop for a module means that any users of types or functions from that module _also_ have to enable C++ interop; if we can do without it, that would be better.)

I've addressed the COM-specific points in my own code, so that just leaves additions to C++ interop which are widely useful and benefit all platforms. Nothing about COM requires C++, but it is based on the C++ object model, so we already have support for the core ABI for free. Relying on C++ would not be ideal if it were important for Swift users to define arbitrary COM interfaces in Swift, but I have reasoned this is not a common use case and consuming Windows APIs is the primary use case. For this, projection generators can write all the C++ boilerplate for us and hide it behind idiomatic Swift. And the compiler focuses on generalised language semantics that benefit as many users as possible.

The limitation that all C and C++ code must be built in the same translation unit in either C or C++ mode is a current pain point that other build systems are not subject to. The point I made earlier about this is that I think it would be best to solve this limitation, rather than sidestepping it by introducing an entirely new object model to the language, especially when that new object model is based on an ABI that is already supported.

Other languages like C# and Rust do not have interop for COM or C++ and simply go through C interop, which they still have managed to wrap and make idiomatic (with support from C#/WinRT, C#/Win32, and `windows-rs`). .NET Framework had COM interop support but modern .NET moved away from it in favour of user-space projections. I use C++ interop in Swift because it is the way that primitives I need are currently exposed, but the path taken by C# and Rust is also totally viable. There is a smaller set of language changes that would be needed to match Rust, including exposing foreign reference counting to Swift (or a copy hook like `Clone` in Rust or copy constructors in C++), providing calling convention attributes like Rust, and addressing some limitations on Swift protocols that prevent zero cost connection to COM vtables like the traits in `windows-rs`. That would save all of the C++ interop changes I have suggested. So most languages rely on C, C++ relies on C++, and Swift has support for both; I think it should be possible to achieve COM support without special integration for COM specifically. C#, C++ and Rust cover pretty much all of the modern usage of COM today.

[Previous page](https://forums.swift.org/t/pitch-a-vision-for-com-interoperability-in-swift/86049.md?page=4)
