简体   繁体   English

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

[英]How to pass C# struct to C++

I have a WP C++ Runtime Component that is to be consumed by a C# WP application. 我有一个C#WP应用程序将使用的WP C ++运行时组件。

In C++ Runtime Component, I have 在C ++运行时组件中,我有

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 : 在C#应用程序中,我具有CallbackImpl ,它实现了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 但是现在我需要传递比String更复杂的东西:在C#中,我有

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++? 如何从C ++调用它?

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

I tried to create a "mirror" structure in C++ like this: 我试图在C ++中创建一个“镜像”结构,如下所示:

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++. 但是我不明白C#的List在C ++中映射到什么。

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. MyDescCPP旁边没有^ (破折号),因为与List<T>ref类型MyDescCPP ,您定义的MyDescCPP是一个struct ,这意味着它是一个value类型。

EDIT: 编辑:

oh, and your struct must be value class or value struct because you want a CLR type there and not the native type: 哦,您的struct必须是value classvalue struct因为您要在其中使用CLR类型而不是本机类型:

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

Classes and Structs (C++ Component Extensions) 类和结构(C ++组件扩展)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM