简体   繁体   English

将带有结构数组的结构从 c# 传递到 c++

[英]Passing a struct with array of structures from c# to c++

I have ac# hierarchy of structs.我有 ac# 结构层次结构。

private struct Vector3
{
    public float x;
    public float y;
    public float z;
}

private structure Location
{
    public Vector3 coords;
    public float distanceFromOrigin;
}

private struct Locations
{
    public Location[] locations;
}

private struct Scene
{
    public Locations cam1;
    public Locations cam2;
    public float timeInMilliSecs;
}

I'm instantiating the Scene and with cam1 and cam2 each gets an array of 10 Locations.我正在实例化Scene并使用cam1cam2分别获得一个包含10 个位置的数组。 Everything traces fine.一切都很好。 Works as expected to this point and the data structure is populated with the correct data.到目前为止按预期工作,并且数据结构填充了正确的数据。

I'm passing the instance of Scene to an unmanaged DLL我将Scene的实例传递给非托管 DLL

[DllImport(dllname)]
private static extern void updateScene(Scene scene);

In my c++ I have在我的 C++ 中,我有

extern "C" {
    DLL_EXPORT void updateScene(Scene scene);
}

and an overload和过载

void updateScene(Scene scene) {
    setSecene(scene); // this calls function fine but with erroneous data
}

and signatures for the equivalent structs和等效结构的签名

struct Vector3
{
    float x;
    float y;
    float z;
}

struct Location
{
    Vector3 coords;
    float distanceFromOrigin;
}

struct Locations
{
    Location locations[10];
}

struct Scene
{
    Locations cam1;
    Locations cam2;
    float timeInMilliSecs;
}

The Scene struct is passed into the C with the correct data structure but not the correct data, none of the Vector3s are right and timeInMilliSecs is always 0. Scene结构以正确的数据结构但不是正确的数据传递到C ,没有一个 Vector3s 是正确的并且timeInMilliSecs始终为 0。

Previously I had Locations as individual instances of Location and that worked fine but since making it an array it doesn't work.以前我曾Locations为单个实例Location ,并且工作得很好,但由于使其成为一个数组这是行不通的。

I'm guessing that I need to Marshall this across but I don't know where to start.我猜我需要马歇尔这个,但我不知道从哪里开始。 Can anyone help, please?请问有人可以帮忙吗?

Try:尝试:

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
public Location[] locations;

Array in c# is a reference type. C#中的数组是引用类型。

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

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