Search code examples
c#c++windows-runtimewinrt-component

How to pass custom WinRT types in methods from c# to c++ Windows Runtime Components?


I'm trying to pass a simple C++ class, created in C#, trough a method to my c++ WinRT component, but I can't figure out how to do that and if its even possible.

I created this custom class in c++ (from https://msdn.microsoft.com/en-us/library/hh441569.aspx)

namespace CppComponent
{
    // Custom struct
    public value struct PlayerData
    {
        Platform::String^ Name;
        int Number;
        double ScoringAverage;
    };

    public ref class Player sealed
    {
    private:
        PlayerData m_player;
    public:
        property PlayerData PlayerStats 
        {
            PlayerData get(){ return m_player; }
            void set(PlayerData data) {m_player = data;}
        }
    };
}

I can create it in c# and play with it, so that works. I can also use other methods that return int or Platform::String.

But how can I use it in methods to c++ like? (and as return type)

in .cpp file:
Platform::String^ CppComponent::DoSomething(Platform::String^ input, Player  myCustomClass)
{

in .h file:
Platform::String^ DoSomething(Platform::String^ input, Player myCustomClass);

Any idea how to get "Player myCustomClass" right?

PS: working on https://github.com/cmusphinx/pocketsphinx-wp-demo


Solution

  • Turns out I was close already.

    I put de struct and class in my .h file and used Player^ instead of Player like:

    Platform::String^ CppComponent::DoSomething(Platform::String^ input, Player^ myCustomClass)