October 27th, 2023

How to support a COM interface conditionally in WRL

Last time, we looked at conditionally supporting a COM interface in C++/WinRT. Today, we’ll solve the same problem for WRL.

For WRL, you directly override the Query­Interface method. and Get­Iids methods.

struct Widget : Microsoft::WRL::RuntimeClass<
    Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::WinRt>,
    ::ABI::Contoso::IWidget, ::Windows::Foundation::IStringable>
{
    STDMETHOD(QueryInterface)(REFIID riid, void** ppvObject) 
    {                                                        
        // If "Stringable" is not enabled,                   
        // then don't support IStringable.                   
        if (riid == __uuidof(::IStringable) &&               
            !is_stringable_enabled()) {                      
            *ppvObject = nullptr;                            
            return E_NOINTERFACE;                            
        }                                                    
        return RuntimeClass::QueryInterface(riid, ppvObject);
    }                                                        

    // Implement IWidget methods
    STDMETHOD(WidgetMethod)();

    // Implement IStringable methods
    STDMETHOD(ToString)(HSTRING* result);
};

This is basically the same as the C++/WinRT version: When a query comes in for IStringable, we check whether IStringable support is enabled. If not, then we set the result to nullptr and return E_NO­INTERFACE. Otherwise, we forward the call to the base class to continue normally.

Bonus chatter: Commenter Euro Micelli points out that you should also read the previous entry on the requirement for stability of is_stringable_enabled().

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

1 comment

Discussion is closed. Login to edit/delete existing comments.

  • Euro Micelli · Edited

    I think it’s important to highlight (especially for people who find this post and don’t read yesterday’s about the WinRT version), that COM requires that the answer provide by is_stringable_enabled() must never change for a given object. Once a given COM object responds that it supports the requested interface, it must always support it; and once it responds that it doesn’t support it, it must never answer that it does.

    Read more