{"id":112591,"date":"2026-08-05T07:00:00","date_gmt":"2026-08-05T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=112591"},"modified":"2026-08-05T22:04:50","modified_gmt":"2026-08-06T05:04:50","slug":"20260805-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20260805-00\/?p=112591","title":{"rendered":"Creating a fake agile wrapper that is technically agile but is not useful outside its home apartment, part 3"},"content":{"rendered":"<p>Last time, we tried to execute on <a title=\"Creating a fake agile wrapper that is technically agile but is not useful outside its home apartment, part 2\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20260804-00\/?p=112586\"> 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<\/a>. But it broke down because objects that are marked <code>INoMarshal<\/code> can&#8217;t go into the global interface table.<\/p>\n<p>So we will just force the square peg into the round hole: We can put the non-marshalable object inside an object that <i>is<\/i> marshalable.<\/p>\n<pre>template&lt;typename Smart&gt;\r\nstruct force_marshal :\r\n    winrt::implements&lt;force_marshal&lt;Smart&gt;, ::IUnknown, winrt::non_agile&gt;\r\n{\r\n    force_marshal(Smart const&amp; p) : m_p(p) {}\r\n    Smart m_p;\r\n};\r\n<\/pre>\n<p>The <code>force_marshal&lt;Smart&gt;<\/code> 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 cannot be either. (If the wrapper were agile, then we&#8217;d be back where we started: How do we ensure that the <code>m_p<\/code> is destructed in the correct apartment?)\u00b9<\/p>\n<p>We can put the unmarshalable object inside the wrapper, and then put the wrapper in the global interface table.<\/p>\n<pre>template&lt;typename T&gt;\r\nstruct fake_agile_ref\r\n{\r\n    \u27e6 ... \u27e7\r\n\r\n    fake_agile_ref(Smart const&amp; p) : m_raw(winrt::get_abi(p))\r\n    {\r\n        if (m_raw) {\r\n            m_context = winrt::capture&lt;IContextCallback&gt;(CoGetObjectContext);\r\n            m_token = get_context_token();\r\n            m_git = winrt::create_instance&lt;IGlobalInterfaceTable&gt;(CLSID_StdGlobalInterfaceTable);\r\n            winrt::check_hresult(m_git-&gt;RegisterInterfaceInGlobal(\r\n                <span style=\"border: solid 1px currentcolor;\">winrt::make&lt;force_marshal&lt;Smart&gt;&gt;(p).get()<\/span>,\r\n                __uuidof(IUnknown), &amp;m_cookie));\r\n        }\r\n    }\r\n\r\n    \u27e6 ... \u27e7\r\n};\r\n\r\ntemplate&lt;typename T&gt; fake_agile_ref(winrt::com_ptr&lt;T&gt; const&amp;)\r\n    -&gt; fake_agile_ref&lt;T&gt;;\r\ntemplate&lt;typename T&gt; fake_agile_ref(T const&amp;)\r\n    -&gt; fake_agile_ref&lt;T&gt;;\r\n<\/pre>\n<p>Okay, so now we have managed to create an agile wrapper around an unmarshalable object. This agile wrapper is agile on paper: You can use it from any thread. However, it is not agile in practice: If you try to use it from the wrong apartment, it throws an exception. But at least the behavior when used from the wrong apartment is <i>well-defined<\/i>, as opposed to the case of directly using an unmarshalable object from the wrong apartment, which is <i>undefined<\/i>.<\/p>\n<p>Next time, we&#8217;ll do some fine tuning.<\/p>\n<p><b>Bonus chatter<\/b>: Of course, now that we have a marshalable wrapper, we <i>could<\/i> use that wrapper to call the original non-marshalable object.<\/p>\n<table class=\"cp3\" style=\"border-collapse: collapse; text-align: center;\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\">\n<tbody>\n<tr>\n<td>Original apartment<\/td>\n<td style=\"border-right: dashed currentcolor 1px;\">\u00a0<\/td>\n<td>&nbsp;<\/td>\n<td>Other apartment<\/td>\n<\/tr>\n<tr>\n<td style=\"border: solid currentcolor 1px;\">Wrapper<\/td>\n<td style=\"border-right: dashed currentcolor 1px;\">\u2190<\/td>\n<td>\u2190<\/td>\n<td style=\"border: solid currentcolor 1px;\">Caller<\/td>\n<\/tr>\n<tr>\n<td>\u2193<\/td>\n<td style=\"border-right: dashed currentcolor 1px;\">\u00a0<\/td>\n<td>&nbsp;<\/td>\n<\/tr>\n<tr>\n<td style=\"border: solid currentcolor 1px;\">Non-marshalable<\/td>\n<td style=\"border-right: dashed currentcolor 1px;\">\u00a0<\/td>\n<td>&nbsp;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The non-marshalable object does not allow any arrows to come in from other apartments, but the wrapper lives in the same apartment as the non-marshalable object, so <i>its<\/i> arrow is coming from within the same apartment.<\/p>\n<p>You can think of the wrapper as a VPN into the original apartment, allowing calls to come in from the outside, but to appear to the non-marshalable object as if they came from within the same apartment.<\/p>\n<p>Now, you <i>could<\/i> do that, but I&#8217;m not going to. The original object presumably went out of its way to declare itself non-marshalable for a reason, so we honor that preference and not play funny games to trick it into doing something it said that it didn&#8217;t want to do.<\/p>\n<p>\u00b9 Two commenters fell into this trap by suggesting that we wrap the non-marshalable object inside an object that implements <code>IMarshal<\/code>. If you implement <code>IMarshal<\/code>, then you are saying, &#8220;I&#8217;m way cooler than a standard non-agile object. I&#8217;m going to do fancy stuff (like being agile).&#8221; But we <i>want<\/i> to be a boring non-agile object, so that COM will do standard marshaling for us.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Forcing an object into the global interface table.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-112591","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Forcing an object into the global interface table.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112591","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=112591"}],"version-history":[{"count":1,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112591\/revisions"}],"predecessor-version":[{"id":112594,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112591\/revisions\/112594"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=112591"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=112591"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=112591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}