Creating a fake agile wrapper that is technically agile but is not useful outside its home apartment, part 3
cppdashboard.dev/r/2026/08/creating-a-fake-agile-wrapper-that-is-technically-agile-but-3This 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