C#和C ++数组? [英] C# to C++ Array?

查看:188
本文介绍了C#和C ++数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提供了一个库,我知道使用C ++。
我导入的DLL这样:结果
函数[DllImport(PST)]私人静态外部INT pst_get_sensor(REF PSTSensor传感器);
中的PSTSensor是一个结构,在C ++例如,它的定义是这样的:

I have a library provided, which i know uses C++. I imported the DLL as such:
[DllImport("pst")] private static extern int pst_get_sensor(ref PSTSensor sensor); The PSTSensor is a struct, in a C++ example it was defined as such:

struct PSTSensor
{
        char    name[80];       /**< Device name */
        int     id;             /**< Device identifier (for other tracking interfaces */
        float   pose[16];       /**< Device pose estimate as row-major matrix */
        double  timestamp;      /**< Time the data was recorded */
};

的问题是,除了一个int和一个双,它采用的是浮球,更重要的是一个数组的数组的东西C#和C ++使用这个结构来调用 pst_get_sensor(参考传感器)的方式不同; 整个开发。运行代码时对我的环境崩溃

The problem is, that besides an Int and a Double, it uses a Float, and more importantly an array. An array is something way different between C# and C++. When using this struct to call pst_get_sensor(ref sensor); the entire development environment crashes on me when running the code.

目前,我做我的结构在C#这样的:

I currently do my struct in C# like this:

struct PSTSensor{
        public char[] name;
        public int id;
        public float[] pose;
        public double timestamp;
    }

我的问题是,如何创建一个数组,C ++的理解,并正确返回?是这甚至可能吗?

My question is, how do i create an array that C++ understands, and properly returns? Is this even possible?

感谢很多提前,
笑脸

Thanks a lot in advance, Smiley

推荐答案

您的C#结构需要定义内联数组的长度。和名称字段最好在C#代码字符串声明。这使得它更容易让你使用它。

Your C# struct needs to define the inline array lengths. And the name field is best declared as a string in the C# code. That makes it much easier for you to work with it.

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
struct PSTSensor
{
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
    public string name;
    public int id;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
    public float[] pose;
    public double timestamp;
}

的结构布局属性是可选在这里,因为它只是重新状态的缺省值。但您可以选择保留它,以便明确了解你的意图。

The struct layout attribute is optional here, because it just re-states default values. But you may choose to keep it in order to be explicit about your intent.

这篇关于C#和C ++数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆