我有一个项目,它使用了一个基本上是在C++上构建的库。 这个库提供的DLL,我已经导入到我的C#项目。 在Unity导入下面的方法后:从C++到C的结构#
[DllImport("pst")]
private static extern int pst_get_sensor(PSTSensor sensor);
我需要这个PSTSensor结构,所以实际使用的方法。 在C++ h文件,所述结构是定义为:
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 */
};
我试图复制它在C#,并且我结束了这个以下:
struct PSTSensor{
PSTSensor(char[] name, int id, float[] pose, double timestamp){
this.name = name;
this.id = id;
this.pose = pose;
this.timestamp = timestamp;
}
public char[] name;
public int id;
public float[] pose;
public double timestamp;
}
在该示例中的C++代码与这个项目一起被指明要拨打pst_get_sensor(&sensor)
这个'&'的标志,我不承认?我将如何在C#中调用此方法,并使其工作?
我想我毁了结构,看到我从来没有与他们合作过。至少它不会在编译时抛出错误,但我认为它仍然是错误的。有关于此的任何想法?
提前许多感谢, 笑脸
这是一个很好的帮助,我添加了一个构造函数,因为我不确定如何定义没有它的固定大小的数组。我尝试了不同的方式,大多只是向我抛出错误。 – Smileynator
'private static extern int pst_get_sensor(PSTSensor ref sensor);'这给出了错误:标识符预期,'ref'是一个关键字。 – Smileynator
对不起,我认为ref关键字实际上应该是第一位的:(参考PSTSensor传感器)。看看C#的参考,也许它会帮助你更好地理解在C#中通过引用传递:http://msdn.microsoft.com/en-us/library/vstudio/0f66670z.aspx – TommyHaugland