简体   繁体   English

从托管C ++到C#的结构列表

[英]list of structs from managed C++ to C#

First off, I would like to reference to this question: Sharing variables between C# and C++ . 首先,我想引用这个问题: 在C#和C ++之间共享变量

It seems to be what I'm looking for, but when trying to implement this, I get some errors. 这似乎是我正在寻找的,但在尝试实现这一点时,我得到了一些错误。

First off this is my code: 首先这是我的代码:

C++ MyCppWrapper.h C ++ MyCppWrapper.h

namespace CppWrapping
{
    #pragma pack(1)
    public struct MyPoint
    {
    public: 
        float X;
        float Y;
        float Z;
        unsigned char R;
        unsigned char G;
        unsigned char B;
        unsigned char A;

    }MyPoint_t;
    #pragma pack()

    public ref class MyCppWrapper
    {
    public:
        MyCpplWrapper(void);
        List<MyPoint>^ getData();
    };
};

C++ MyCppWrapper.cpp C ++ MyCppWrapper.cpp

List<MyPoint>^ MyCppWrapper::getData()
{
    List<MyPoint>^ temp = gcnew List<MyPoint>();
    for (int i = 0; i < Data.Length; i++)
    {
        PointT& pt = Data.points[i];
        MyPoint holder = MyPoint();
        holder.X = pt.x;
        holder.Y = pt.y;
        holder.Z = pt.z;
        holder.R = pt.r;
        holder.G = pt.g;
        holder.B = pt.b;
        holder.A = pt.a;
        temp[i] = holder;
    }
    return temp;
}

C# MyLinker.cs C#MyLinker.cs

[StructLayout(LayoutKind.Sequential, Pack = 1)]
private struct MyPoint_t
{
    public float X;
    public float Y;
    public float Z;
    public byte R;
    public byte G;
    public byte B;
    public byte A;
};

public void getData()
{
    _wrapper = new MyCppWrapper();
    List<MyPoint_t> data = _wrapper.getData();
}

There are quite some errors but what it comes down to are these three errors: 有一些错误,但它归结为这三个错误:

error C3225: generic type argument for 'T' cannot be 'CppWrapping::MyPoint', it must be a value type or a handle to a reference type

'CppWrapping.MyPoint' is inaccessible due to its protection level

'CppWrapping.MyCppWrapper.getData()' is inaccessible due to its protection level

I also get a red marking under the code List data = _wrapper.getData(); 我还在代码List data = _wrapper.getData();下得到一个红色标记List data = _wrapper.getData(); when I hover my cursor over it: 当我将光标悬停在它上面时:

Cannot convert source type 'System.Collections.Generic.List<CppWrapping.MyPoint>' to target type 'System.Collections.Generic.List<ProjectA.MyLinker.MyPoint_t>'

How can I solve this? 我怎么解决这个问题?

edit: 编辑:

I changed public struct MyPoint into public value struct MyPoint reducing the amount of errors from 58 to 1. the error I have now are: 我将public struct MyPoint更改为public value struct MyPoint将错误数量从58减少到1.我现在的错误是:

Cannot implicitly convert type 'System.Collections.Generic.List<CppWrapping.MyPoint>' to 'System.Collections.Generic.List<ProjectA.MyLinker.MyPoint_t>'
public struct MyPoint {}

This declares an unmanaged struct, your C# code cannot access it since it is exported as an opaque value type without any members. 这声明了一个非托管结构,您的C#代码无法访问它,因为它被导出为不带任何成员的不透明值类型。 You must declare it as 你必须声明它

public value struct MyPoint {}

Next thing to do is delete the MyPoint_t declaration from your C# code. 接下来要做的是从C#代码中删除MyPoint_t声明。 Type identity includes the assembly that the type came from so MyPoint_t is not compatible with MyPoint. 类型标识包括类型来自的程序集,因此MyPoint_t与MyPoint不兼容。 You can simply use the MyPoint type from the C++/CLI assembly: 您只需使用C ++ / CLI程序集中的MyPoint类型:

_wrapper = new MyCppWrapper();
List<MyPoint> data = _wrapper.getData();

or simply take advantage of type inference in C#: 或者简单地利用C#中的类型推断:

var data = _wrapper.getData();

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

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