How to pass C# struct to C++

341 Views Asked by At

I have a WP C++ Runtime Component that is to be consumed by a C# WP application.

In C++ Runtime Component, I have

public interface class ICallback
{
public:
    virtual void sendMail(Platform::String ^to, Platform::String ^subject, Platform::String ^body);
};

In C# Application, I have CallbackImpl, which implements ICallback:

public class CallbackImpl : Windows8Comp.ICallback
{
    public void sendMail(String to, String subject, String body)
    {
        //...
    }

And it works perfectly.

But now I need to pass something more complex than String: in C# I have

public class MyDesc
{
    public string m_bitmapName { get; set; }
    public string m_link { get; set; }
    public string m_event { get; set; }
}

and I have added:

public class CallbackImpl : Windows8Comp.IMyCSCallback
{
    private List<MyDesc> m_moreGamesDescs;
    public List<MyDesc> getMoreGamesDescs()
    {
        return m_moreGamesDescs;
    }
    // ...
}

How do I call it from C++?

public interface class ICallback
{
public:
    virtual <what ret val?> getMoreGamesDescs();    
};

I tried to create a "mirror" structure in C++ like this:

struct MyDescCPP
{
    Platform::String ^m_bitmapName;
    Platform::String ^m_link;
    Platform::String ^m_event;
}

but I don't understand what does C#'s List map to in C++.

1

There are 1 best solutions below

1
On

Is this what you need?

virtual List<MyDescCPP>^  getMoreGamesDescs(); 

there is no ^ (dash) next to MyDescCPP because unlike the List<T> which is a ref type, the MyDescCPP that you defined is a struct and that means it's a value type.

EDIT:

oh, and your struct must be value class or value struct because you want a CLR type there and not the native type:

value class MyDescCPP
{
    Platform::String ^m_bitmapName;
    Platform::String ^m_link;
    Platform::String ^m_event;
}

Classes and Structs (C++ Component Extensions)