none
Can WinRT C# Interface be implemented in C++/CX UWP app? RRS feed

  • Question

  • Hi,

    Can interfaces from C# WinRT be exposed to other languages like C++/CX, where its implementation can be done using C++/CX?

    The real question is around:

         How can we pass C++ Objects to C# WinRT Component (for UI) which can then be used to display in UI?

         Do we have to have similar classes in C# WinRT Component and then create those class objects using C++/CX, which then will be passed to C# WinRT component functions? Problem here is extra objects creation for using C# WinRT? If number of objects in C++ is huge, those many objects needs to be created in C# side as well using C++/CX.

    Regards,

    Vivek


    • Edited by vivek2k6 Thursday, October 12, 2017 11:51 AM
    Thursday, October 12, 2017 11:24 AM

All replies

  • Hello,

    You can create type in C++/CX using ref class declaration with constructors and then from C# side you can create instance of that type without any problems.


    Make the community better together

    Thursday, October 12, 2017 5:30 PM
  • You can create a Windows Runtime Component (WRC) in C++/Cx and call it from a C# app or you can create a Windows Runtime Component in C# and call it from a C++/Cx app.

    See documentation and walkthroughs at https://msdn.microsoft.com/en-us/library/windows/apps/hh441572.aspx

    The Creating Windows Runtime Components in C++ documentation goes over the typing restrictions and issues for C++: https://msdn.microsoft.com/en-us/library/windows/apps/hh441569.aspx

    The Creating Windows Runtime Components in C# and Visual Basic documentation does the same for .Net: https://msdn.microsoft.com/en-us/library/windows/apps/br230301.aspx


    I'm not sure what you mean by "How can we pass C++ Objects to C# WinRT Component (for UI) which can then be used to display in UI?" but I hope the general WRC docs linked will get you going in the right direction. Typically the main app would host the UI and the WRC would host the data model.
    Friday, October 13, 2017 1:33 AM
  • That is fine, I am able to create C#Class (present in WinRT component) object from my C++/CX app.

    What I need to know, is that 

    Suppose I need to show Car objects present in C++ UWP app project.

    This list of car objects are created in C++ code of my app as model objects construction has to happen in app, and then I need these cars to be shown in UI (for which I am using C# WinRT which has xaml files). 

    Model has to be in C++, that's my restriction of my app, it can't be in C++/CX.

    To pass the car list, do I need to again create C++/CX models (using ref new CSharpCar class of C#WinRT) from C++ models, and then pass this C++/CX model to C#WinRT comp.

    Drawback here are:

         1. C#WinRT needs to have similar C# models as C++ models. If my C++ model changes, I need to do changes in my C#WinRT comp as well.

         2. And we have to create C++/CX objects from already existing C++ model objects, one extra object creation is happening.

    How can we avoid these drawbacks?

    Regards,

    Vivek

    Friday, October 13, 2017 9:40 AM
  • That is fine, I am able to create C#Class (present in WinRT component) object from my C++/CX app.

    What I need to know, is that 

    Suppose I need to show Car objects present in C++ UWP app project.

    This list of car objects are created in C++ code of my app as model objects construction has to happen in app, and then I need these cars to be shown in UI (for which I am using C# WinRT which has xaml files). 

    Model has to be in C++, that's my restriction of my app, it can't be in C++/CX.

    To pass the car list, do I need to again create C++/CX models (using ref new CSharpCar class of C#WinRT) from C++ models, and then pass this C++/CX model to C#WinRT comp.

    Drawback here are:

         1. C#WinRT needs to have similar C# models as C++ models. If my C++ model changes, I need to do changes in my C#WinRT comp as well.

         2. And we have to create C++/CX objects from already existing C++ model objects, one extra object creation is happening.

    How can we avoid these drawbacks?

    Regards,

    Vivek

    Friday, October 13, 2017 9:40 AM
  • As I understand You have pure C++ type which you need to pass C# UI? If I'm correct then

    The other way is create type with same fields in C#  specify layoutkind and  marshaling parameters, export class in C++ dll and in final use CLR PInvoke to get actual value. An other way is use COM but both of this is complicated.   

    The best way is create C++/CX Wrapper for that pure C++ type after which you can use it without any problems in your  C# side (It is out of box solution you just need create simple wrapper). If you think it is extra object creation and more complicated that previous two scenarios' then go by that way 


    Make the community better together

    Friday, October 13, 2017 2:10 PM
  • Thanks Azat.

    As of now, like you said above, we are using C++/CX wrapper only which we then pass it to C#WinRT UI component, and this is working.

    I just want to know if can we implement interface of C#WinRT in C++/CX class, and then pass this concrete object to C#WinRT for UI creation.

    Say in C#, interface is there ICar, can we implement in C++/CX like

    class CxCar : public C#NameSpace::ICar {

        private C++Car car;  // C++/CX wrapper uses C++ Car model

        public CxCar(C++Car obj) {...}

        // Now implement ICar interface methods in C++/CX

        public Platform::String^ Name() override { return (Platform::String^)car.getName();}

        public Platform::String^ CarModel() override { return (Platform::String^)car.getCarModel();}

    }

    Then we pass objects of CxCar to C#WinRT component, which treats these as ICar ref type, and UI will use the ICar interface methods for displaying Cars.

    This way C++ Car object doesn't need to be recreated in C#, rather it can be used in C++/CX as composition to implement ICar interface methods.

    Is something of this sort, possible.

    • Edited by vivek2k6 Saturday, October 14, 2017 12:10 AM
    Saturday, October 14, 2017 12:09 AM
  • If I understand what you're asking then that is the normal way to use Windows Runtime Components. See the type handling docs I linked previously.
    Saturday, October 14, 2017 4:24 PM