{"id":106511,"date":"2022-04-21T07:00:00","date_gmt":"2022-04-21T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=106511"},"modified":"2022-04-21T07:27:04","modified_gmt":"2022-04-21T14:27:04","slug":"20220421-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20220421-00\/?p=106511","title":{"rendered":"How do I access and customize the IInspectable methods of a Windows Runtime class written in C++\/WinRT?"},"content":{"rendered":"<p>The Windows Runtime <code>IInspectable<\/code> interface adds three methods beyond those of its base interface <code>IUnknown<\/code>: <code>Get\u00adRuntime\u00adClass\u00adName<\/code>, <code>Get\u00adIids<\/code>, and <code>Get\u00adTrust\u00adLevel<\/code>, The C++\/WinRT project does not expose these methods because they are used primarily by the language infrastructure, and putting them directly on C++\/WinRT objects would cause them to clutter up Intellisense and autocomplete.<\/p>\n<p>So how do you access these methods if they aren&#8217;t present on <code>winrt::<wbr \/>Windows::<wbr \/>Foundation::<wbr \/>IInspectable<\/code>?<\/p>\n<p>You access them through separate free functions.<\/p>\n<pre>\/\/ returned as a winrt::hstring\r\nauto name = winrt::get_class_name(something);\r\n\r\n\/\/ returned as a winrt::com_array&lt;winrt::guid&gt;\r\nauto interfaces = winrt::get_interfaces(something);\r\n\r\n\/\/ returned as a winrt::Windows::Foundation::TrustLevel\r\nauto level = winrt::get_trust_level(something);\r\n<\/pre>\n<p>Okay, so that&#8217;s how you can call these secret methods. But how do you override the default implementations?<\/p>\n<p>For <code>Get\u00adRuntime\u00adClass\u00adName<\/code> and <code>Get\u00adTrust\u00adLevel<\/code> you just override it like any other overridable method:<\/p>\n<pre>struct Something :\r\n    winrt::implements&lt;Something,\r\n                      winrt::Windows::Foundation::IInspectable&gt;\r\n{\r\n    winrt::hstring GetRuntimeClassName() const\r\n    { return L\"CustomSomething\"; }\r\n\r\n    auto GetTrustLevel() const\r\n    { return winrt::Windows::Foundation::TrustLevel::BaseTrust; }\r\n};\r\n<\/pre>\n<p>For <code>Get\u00adIids<\/code>, the list of interfaces is generated automatically from the list provided to <code>implements<\/code>. If you want to remove an interface from the list, you can &#8220;cloak&#8221; it:<\/p>\n<pre>winrt::implements&lt;MyClass,\r\n    Interface1, Interface2, winrt::cloaked&lt;Interface3&gt;&gt;\r\n<\/pre>\n<p>This marks the third interface as cloaked, which means that it is not reported by <code>Get\u00adIids<\/code>.<\/p>\n<p><b>Bonus chatter<\/b>: What is the default implementation of these methods?<\/p>\n<p>The default runtime class name is the name of the class (if it is a Windows Runtime class) or the default interface, as reported by <code>winrt::name_of<\/code>. The default trust level is <code>Base\u00adTrust<\/code>,<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Special access methods, but normal overrides.<\/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-106511","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Special access methods, but normal overrides.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/106511","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=106511"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/106511\/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=106511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=106511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=106511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}