{"id":108934,"date":"2023-10-27T07:00:00","date_gmt":"2023-10-27T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=108934"},"modified":"2023-10-27T12:10:21","modified_gmt":"2023-10-27T19:10:21","slug":"20231027-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20231027-00\/?p=108934","title":{"rendered":"How to support a COM interface conditionally in WRL"},"content":{"rendered":"<p>Last time, we looked at <a title=\"How to support a COM interface conditionally in C++\/WinRT\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20231026-00\/?p=108930\"> conditionally supporting a COM interface in C++\/WinRT<\/a>. Today, we&#8217;ll solve the same problem for WRL.<\/p>\n<p>For WRL, you directly override the <code>Query\u00adInterface<\/code> method. and <code>Get\u00adIids<\/code> methods.<\/p>\n<pre>struct Widget : Microsoft::WRL::RuntimeClass&lt;\r\n    Microsoft::WRL::RuntimeClassFlags&lt;Microsoft::WRL::WinRt&gt;,\r\n    ::ABI::Contoso::IWidget, ::Windows::Foundation::IStringable&gt;\r\n{\r\n    <span style=\"border: solid 1px currentcolor; border-bottom: none;\">STDMETHOD(QueryInterface)(REFIID riid, void** ppvObject) <\/span>\r\n    <span style=\"border: 1px currentcolor; border-style: none solid;\">{                                                        <\/span>\r\n    <span style=\"border: 1px currentcolor; border-style: none solid;\">    \/\/ If \"Stringable\" is not enabled,                   <\/span>\r\n    <span style=\"border: 1px currentcolor; border-style: none solid;\">    \/\/ then don't support IStringable.                   <\/span>\r\n    <span style=\"border: 1px currentcolor; border-style: none solid;\">    if (riid == __uuidof(::IStringable) &amp;&amp;               <\/span>\r\n    <span style=\"border: 1px currentcolor; border-style: none solid;\">        !is_stringable_enabled()) {                      <\/span>\r\n    <span style=\"border: 1px currentcolor; border-style: none solid;\">        *ppvObject = nullptr;                            <\/span>\r\n    <span style=\"border: 1px currentcolor; border-style: none solid;\">        return E_NOINTERFACE;                            <\/span>\r\n    <span style=\"border: 1px currentcolor; border-style: none solid;\">    }                                                    <\/span>\r\n    <span style=\"border: 1px currentcolor; border-style: none solid;\">    return RuntimeClass::QueryInterface(riid, ppvObject);<\/span>\r\n    <span style=\"border: solid 1px currentcolor; border-top: none;\">}                                                        <\/span>\r\n\r\n    \/\/ Implement IWidget methods\r\n    STDMETHOD(WidgetMethod)();\r\n\r\n    \/\/ Implement IStringable methods\r\n    STDMETHOD(ToString)(HSTRING* result);\r\n};\r\n<\/pre>\n<p>This is basically the same as the C++\/WinRT version: When a query comes in for <code>IStringable<\/code>, we check whether <code>IStringable<\/code> support is enabled. If not, then we set the result to <code>nullptr<\/code> and return <code>E_<wbr \/>NO\u00adINTERFACE<\/code>. Otherwise, we forward the call to the base class to continue normally.<\/p>\n<p><b>Bonus chatter<\/b>: Commenter Euro Micelli points out that you should also read the previous entry on the requirement for stability of <code>is_<wbr \/>stringable_<wbr \/>enabled()<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Customizing the QueryInterface and GetIids methods.<\/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-108934","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Customizing the QueryInterface and GetIids methods.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/108934","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=108934"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/108934\/revisions"}],"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=108934"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=108934"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=108934"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}