How do I access and customize the IInspectable methods of a Windows Runtime class written in C++/WinRT?

Raymond Chen

The Windows Runtime IInspectable interface adds three methods beyond those of its base interface IUnknown: Get­Runtime­Class­Name, Get­Iids, and Get­Trust­Level, 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.

So how do you access these methods if they aren’t present on winrt::Windows::Foundation::IInspectable?

You access them through separate free functions.

// returned as a winrt::hstring
auto name = winrt::get_class_name(something);

// returned as a winrt::com_array<winrt::guid>
auto interfaces = winrt::get_interfaces(something);

// returned as a winrt::Windows::Foundation::TrustLevel
auto level = winrt::get_trust_level(something);

Okay, so that’s how you can call these secret methods. But how do you override the default implementations?

For Get­Runtime­Class­Name and Get­Trust­Level you just override it like any other overridable method:

struct Something :
    winrt::hstring GetRuntimeClassName() const
    { return L"CustomSomething"; }

    auto GetTrustLevel() const
    { return winrt::Windows::Foundation::TrustLevel::BaseTrust; }

For Get­Iids, the list of interfaces is generated automatically from the list provided to implements. If you want to remove an interface from the list, you can “cloak” it:

    Interface1, Interface2, winrt::cloaked<Interface3>>

This marks the third interface as cloaked, which means that it is not reported by Get­Iids.

Bonus chatter: What is the default implementation of these methods?

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 winrt::name_of. The default trust level is Base­Trust,