Creating a fake agile wrapper that is technically agile but is not useful outside its home apartment, part 2
cppdashboard.dev/r/2026/08/creating-a-fake-agile-wrapper-that-is-technically-agile-but-2This article discusses the implementation of a fake agile reference in C++ that holds a reference to an object in another apartment, automatically expiring when the apartment runs down. It details the structure and functionality of the fake agile reference, including its components and construction.
From the article
Last time, we hatched a plan for holding a reference to an object in another apartment that automatically expires when the apartment runs down . Let’s try to implement that plan.
template<typename T> struct fake_agile_ref { private: using Smart = std::conditional_t< std::is_base_of_v<winrt::Windows::Foundation::IUnknown, T>, T, winrt::com_ptr<T>>; We define Smart to represent the smart pointer that holds a T . If T is a projected type, then it is already a smart pointer. Otherwise, T is a COM interface, and we put it inside a com_ptr . This is the same pattern that the C++/WinRT agile_ref<T> uses.
winrt::com_ptr<IContextCallback> m_context; ULONG_PTR m_token = 0; winrt::com_ptr<IGlobalInterfaceTable> m_git; DWORD m_cookie = 0; void* m_raw = nullptr; Our fake agile reference starts with…
Share this resource