Implementing class methods using WRL RRS feed

  • Question

  • I'm implementing a WinRT class using WRL.

    I'm not using C++/CX because this is an adaptation of a legacy COM class that must continue to work as a COM component (and I'm not going through thousands of lines of code to change CComPtr<> to hats!).

    I'm trying to implement a couple of static methods, and I simply can't figure out how to declare them for the ABI.

    I've gotten as far as defining a separate interface for the static methods and implementing the static interface on the ActivationFactory.  My final problem is what MIDL syntax or WRL macro/template I need to use to get the correct metadata generated?

    I just need the winmd metadata to identify the static methods as static methods.  It's done in the Windows Runtime, so how do I do it???

    B.T.W.  I know you guys are encouraging developers to use C++/CX, but there are many good reasons for not using it, and you've provided WRL for just that reason.

    I've seen a lot of the guts of WRL and it's a very clean and powerful template library.  However, the reason I've seen so much of it is because digging through the templates is the only way I could figure out how to use it!  The documentation is almost useless.  There's a bunch of tautologies (RuntimeClass - "this is a runtime class"), almost no explanations, and a lot of "Supports the WRL infrastructure and is not intended to be used directly from your code."

    Also, the syntax rules for MIDLRT are not documented at all.  Nothing.  Nada.  I've searched all over.

    Definitely not-ready-for-primetime (like the rest of VS2012).  Any idea when these important development tools are going to be documented?

    Tuesday, February 12, 2013 1:39 AM


All replies

  • Hi,


    Please follow Implementation of the Static Member Function section in this blog



     class KnownValuesFactory : public ActivationFactory<IKnownValuesStatics>
            InspectableClassStatic(RuntimeClass_WRLKnownValuesComponent_KnownValues, BaseTrust)
            STDMETHODIMP GetZero(int* value) override
                *value = 0;
                return S_OK;

    Best regards,


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by CinSoft Wednesday, February 27, 2013 10:11 PM
    Tuesday, February 12, 2013 10:19 AM
  • Eureka!

    Thank you very much, that did the trick.  The InspectableClassStatic macro was helpful, although ActivatableStaticOnlyFactory was not appropriate because my class is not static-only.  However, the [static(....)] attribute in the MIDL (also mentioned in the blog article) was crucial!

    Whoever-it-may-concern - PLEASE DOCUMENT MIDLRT!!!!

    A related issue - I want to pass a Windows.UI.Xaml.Controls.Canvas instance as a parameter to one of my methods.  I can't use the class name in the MIDL, and I can't even use the ICanvas interface (because it's marked as "exclusiveto").  So I have to pass an IInspectable* and then do a QueryInterface to get the ICanvas*.  Then I have to document that even though the parameter shows up in the public interface as IInspectable* (or "object" in C#), it really must be a Canvas instance.

    Any way around this?  There must be a way to be more specific on the parameter type without MIDLRT rejecting it because it's "exclusiveto".

    I noticed that the referenced blog article contained more comments on the ongoing C++/CX v.s. WRL debate. 

    Here's my two cents.  C++/CX is helpful for some purposes and WRL is REQUIRED for others.  Since these two alternatives don't seem to satisfy everybody, how about a third approach?

    C++/CX is very similar to the late-and-unlamented C++/CLI, and WRL is similar to ATL.  How about something like MFC that uses a bunch of macros/templates to wrap and abstract WRL, while still leaving the complex guts of WRL intact for those who need that level of control?

    There are many many C++ developers who have been happily using MFC for years.  They are unconcerned about the blind trust they must place in the MFC macros.  They don't really mind the minor intellisense/compile/debug oddities that result.  As long as they don't have to go through the steeper learning curve of ATL, they're happy to have a cookbook approach to using the ATL/MFC framework. 

    With a few well-chosen macros and well-places #ifdefs, it's even possible to write portable code that compiles under MFC in Visual Studio and without MFC in other environments (hint, use STL for strings and collections and don't even try to port an MFC-specific Windows-specific class).

    But first document WRL and MIDLRT!


    Saturday, February 16, 2013 12:30 AM
  • Hi,


    Here is the document
    for WRL and a video for it. Maybe you can get help from them.





    Best regards,


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, February 18, 2013 7:10 AM
  • Thanks,

    The Visual Studio documentation doesn't cover static methods yet.  It's very very slim documentation - almost useless at this time.

    The Channel 9 link is defunct.

    Early adopter, bleeding edge, growing pains, blah, blah, blah...

    Ain't technology grand!

    • Edited by CinSoft Wednesday, February 27, 2013 10:10 PM
    Wednesday, February 27, 2013 10:09 PM