如何将 C# 结构传递给C++

本文关键字:C++ 结构 | 更新日期: 2023-09-27 18:32:18

我有一个 WP C++运行时组件,将由 C# WP 应用程序使用。

在运行时组件C++,我有

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

在C#应用程序中,我有CallbackImpl,它实现了ICallback

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

而且它工作得很好。

但是现在我需要传递比String更复杂的东西:在 C# 中我有

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

我补充说:

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

我如何从C++中称呼它?

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

我试图像这样C++创建一个"镜像"结构:

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

但我不明白 C# 的List在C++中映射到什么。

如何将 C# 结构传递给C++

这是你需要的吗?

virtual List<MyDescCPP>^  getMoreGamesDescs(); 

MyDescCPP旁边没有^(破折号),因为与ref类型的List<T>不同,您定义的MyDescCPPstruct,这意味着它是value类型。

编辑:

哦,你的struct必须是value classvalue struct的,因为你想要一个CLR类型而不是本机类型:

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

类和结构(C++组件扩展)