Article

Creating a fake agile wrapper that is technically agile but is not useful outside its home apartment, part 3

The Old New Thing ·The Old New Thing ·Published 2026-08-05 ·5 min read

This article discusses the challenges of creating a marshalable wrapper around a non-marshalable COM object in C++. It explains how to implement a solution using templates to manage object lifetimes across different apartments.

From the article

Last time, we tried to execute on our plan to use the global interface table to hold hold a reference to an object in another apartment that automatically expires when the apartment runs down . But it broke down because objects that are marked INoMarshal can’t go into the global interface table.

So we will just force the square peg into the round hole: We can put the non-marshalable object inside an object that is marshalable.

template<typename Smart> struct force_marshal : winrt::implements<force_marshal<Smart>, ::IUnknown, winrt::non_agile> { force_marshal(Smart const& p) : m_p(p) {} Smart m_p; }; The force_marshal<Smart> object babysits a non-marshalable smart pointer to a COM object and exposes a marshalable wrapper around it. Since the wrapped object is not agile, the wrapper…


Share this resource


Discovered 2026-08-06 Source The Old New Thing Archive 2026-08 →