如何使用st#:: vector <> :: iterator作为C#中的参数调用非托管C++函数?

Iai*_*oat 0 c# c++ pinvoke

在非托管C++中,我有一个函数,我试图从C#调用.这个C++函数如下:

typedef std::vector<Point> Points;
typedef std::back_insert_iterator<Points> OutputIterator;

namespace MYNAMESPACE{
    DLLEXPORT OutputIterator convexHull(Points::iterator first, Points::iterator last,  OutputIterator result);
}
Run Code Online (Sandbox Code Playgroud)

从C++调用时,该函数使用如下:

  Points points, result;

  points.push_back(Point(0,0));
  points.push_back(Point(10,0));
  points.push_back(Point(10,10));
  points.push_back(Point(6,5));
  points.push_back(Point(4,1));

  OutputIterator resultIterator = std::back_inserter(result);

  MYNAMESPACE::convexHull( points.begin(), points.end(), resultIterator);
  std::cout << result.size() << " points on the convex hull" << std::endl;
Run Code Online (Sandbox Code Playgroud)

我已经开始编写C#代码,但我不知道我应该传递哪些类型:

[DllImport("unmanagedCode.dll", EntryPoint = "convexHull", CallingConvention = CallingConvention.StdCall)]
        public static extern ???<Point> convex_hull_2(???<Point> start, ???<Point> last, ???<Point> result);
Run Code Online (Sandbox Code Playgroud)

C#中的Point结构只是:

struct Point{
    double x;
    double y;
}
Run Code Online (Sandbox Code Playgroud)

这是传递数组或List of Point的情况吗?

我有C++的源代码,可以更改函数参数; 是否会有一种不同类型的参数可以更容易从C#调用?

Ben*_*igt 6

使用C++/CLI包装器类(因为您有C++源代码,您可以将它与现有代码一起编译到单个DLL中).

P/invoke不是为与C++类交互而设计的,任何这样做的尝试都非常脆弱.C++模板可能更糟糕.不要试试.即使是dllexport荷兰国际集团,其功能是其他的C++代码是一个可怕的想法,STL类,如vectorvector::iterator不应该跨越DLL边界传递.

使用/clr并让Visual C++编译器处理细节.