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::
?
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::implements<Something, winrt::Windows::Foundation::IInspectable> { 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:
winrt::implements<MyClass, 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
,
0 comments