在C ++和C#之间传递vector struct

我有c ++非托管代码,我想从c#访问。 所以我按照了一些教程,为我的项目构建了一个dll(只有一个类btw)。 现在我想从c#中使用它,我正在使用p / invoke,如下所示。

我的问题是:是否有可能对我的Windows点进行编组,以便将其作为向量传递到我的c ++代码中? 我可以更改所有代码(除了qwindows点,但我可以自己指出)。 有没有我不需要创建交流包装的解决方案? 我正在关注这个问题: 如何使用st#:: vector :: iterator作为C#中的参数调用非托管C ++函数?

非常感谢ps,我发现了一个“解决方案”,但我无法查看它http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_21461195.html

C#

using Point = System.Windows.Point; class CPlusPlusWrapper { [DllImport("EmotionsDLL.dll", EntryPoint = "calibrate_to_file")] static extern int calibrate_to_file(vector pontos);//marshall here [DllImport("EmotionsDLL.dll", EntryPoint = "calibration_neutral")] static extern int calibration_neutral(); ///  /// wraps c++ project into c# ///  public void calibrate_to_file() { } 

DLL标题

 namespace EMOTIONSDLL { struct points{ double x; double y; double z; }; #define db at class DLLDIR EMOTIONS { public: EMOTIONS(); CvERTrees * Rtree ; vector mapear_kinect_porto(vector pontos); void calibrate_to_file(vector pontos); int calibration_neutral(); int EmotionsRecognition(); }; } 

您可以将C#数组编组为C ++ std :: vector,但它会非常复杂并且根本不是一个好主意,因为std :: vector的布局和实现在编译器版本之间并不保证是相同的。

相反,您应该将参数更改为指向数组的指针,并添加指定数组长度的参数:

 int calibrate_to_file(points* pontos, int length); 

在C#中,将方法声明为采用数组并应用MarshalAs(UnmanagedType.LPArray)属性:

 static extern int calibrate_to_file([MarshalAs(UnmanagedType.LPArray)]] Point[] pontos, int length); 

另请注意,您的C ++ 结构与System.Windows.Point不兼容。 后者没有z成员。

但是你的代码的一个更大的问题是你不能真正期望DLL导入实例方法并且能够像这样调用它。 实例方法需要其类的实例,并且没有简单的方法从C#创建非COM C ++类的实例(并且也不是一个好主意)。 因此,您应该将其转换为COM类,或者为它创建C ++ / CLI包装器。

我认为你应该只传递你的类型的数组,然后在相关函数中将它们转换为vector或List。

它也可能是你引用static extern INT calibrate_to_file()的事实,而在C ++中它是VOID calibrate_to_file()

更新:我认为您缺少function上的DLLEXPORT标签?