Article

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

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

This 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


Discovered 2026-08-05 Source The Old New Thing Archive 2026-08 → c++comwinrtagile