Howto call an unmanaged C++ function with a std::vector as parameter from C#?

前端 未结 4 1011
孤街浪徒
孤街浪徒 2020-12-12 01:04

I have a C# front end and a C++ backend for performance reasons. Now I would like to call a C++ function like for example:

void findNeighbors(Point p, std::v         


        
4条回答
  •  没有蜡笔的小新
    2020-12-12 01:24

    The best solution here is to write a wrapper function in C which is limited to non-C++ classes. Non-trivial C++ classes are essentially unmarshable via the PInvoke layer [1]. Instead have the wrapper function use a more traditional C signature which is easy to PInvoke against

    void findNeigborsWrapper(
      Point p,
      double maxDist, 
      Point** ppNeighbors,
      size_t* pNeighborsLength)
    

    [1] Yes there are certain cases where you can get away with it but that's the exception and not the rule.

提交回复
热议问题