locked
Handling Interfaces

    Question

  • This might seem like a trivial question but I can't find a completely definitive answer on it--oddly enough.

    When implementing multiple interfaces with WinRT how does one properly go about casting between interfaces.

    Is it most proper to cast a Ref Class to an interface using

    auto raw = reinterpret_cast<IInterface*>(some_ref_class_instance)

    or is it more appropriate to use the equality operator as in

    IInterface* raw =  some_ref_class_instance?

    given a ref class that implements said interface. Will the equality operator on ref classes throw an internal exception on failure(I'm assuming that to be the proper method of casting between interfaces)

    Also can a ref class implement an interface that has been defined under COM, using whatever necessary from wrl, or do I have to look at  building a WinRT component ground up using wrl?

    Sunday, December 23, 2012 11:31 PM

Answers

  • I think I've found my answers while trying to pass a DirectX struct through a c++/cx interface.  To do so I have found it necessary to wrap the struct in a ref class and to declare the member access as internal.  To be able to pass this ref class as a parameter in another interfaces methods I find it is necessary that the ref class that contains the private public member inherit from another interface itself which contains no private or internal members. Now that I am able to pass the wrapped struct as such, I have found that I can create the ref class component, set its internal member variable, cast up(my convention may be wrong here) to the interface using the equality operator, and upon receiving the ref class interface through an interface method in parameter, I can cast back down using dynamic_cast to the ref class and obtain the original struct member with internal access . . . the only thing I have to check now is that the member can be accessed from the calling app component residing in another WinRT DLL.  But, I still have no idea of what parts of what I'm doing here is appropriate, all I can say is on my preliminary test the data was successfully transferred . . . no account for reference counting at the COM level . . .

    • Edited by Xittenn Monday, December 24, 2012 2:11 AM
    • Proposed as answer by Jesse Jiang Wednesday, December 26, 2012 2:39 AM
    • Marked as answer by Xittenn Wednesday, December 26, 2012 5:59 AM
    Monday, December 24, 2012 2:10 AM

All replies

  • I think I've found my answers while trying to pass a DirectX struct through a c++/cx interface.  To do so I have found it necessary to wrap the struct in a ref class and to declare the member access as internal.  To be able to pass this ref class as a parameter in another interfaces methods I find it is necessary that the ref class that contains the private public member inherit from another interface itself which contains no private or internal members. Now that I am able to pass the wrapped struct as such, I have found that I can create the ref class component, set its internal member variable, cast up(my convention may be wrong here) to the interface using the equality operator, and upon receiving the ref class interface through an interface method in parameter, I can cast back down using dynamic_cast to the ref class and obtain the original struct member with internal access . . . the only thing I have to check now is that the member can be accessed from the calling app component residing in another WinRT DLL.  But, I still have no idea of what parts of what I'm doing here is appropriate, all I can say is on my preliminary test the data was successfully transferred . . . no account for reference counting at the COM level . . .

    • Edited by Xittenn Monday, December 24, 2012 2:11 AM
    • Proposed as answer by Jesse Jiang Wednesday, December 26, 2012 2:39 AM
    • Marked as answer by Xittenn Wednesday, December 26, 2012 5:59 AM
    Monday, December 24, 2012 2:10 AM
  • Hi,

    Is your struct contains function?
    The struct in Windows Runtime should be POD ("plain old data structure"). It has a fixed size and consists of fields only; unlike a ref class, it has no properties.

    More information, please check
    http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh699861.aspx

    Best regards,
    Jesse


    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.

    Tuesday, December 25, 2012 6:47 AM