Search code examples
c++c++-clidllimport

std::vector from native library to C++/CLI


Now I need to create a C++ -> C++/CLI -> C# wrapper for same code in native C++ library. This library has methods like std::vector<float>& Foo(std::vector<float>& vec, std::vector<std::vector<float>>& matrix)

I have 2 questions.

1) How to load function from native library in C++/CLI? 2) How to pass std::vector<float>& / std::vector<std::vector<float>>& into the function and get std::vector<float>& back?

I know how to create wrapper for IEnumerable data I need to pass into function as input data, just only this 2 questions I need to solve.(((

Thank you.


Solution

  • To load your C++ native library, whatever you would normally do from C++, do that from C++/CLI. Whether it's a static library or a DLL, the process is the same.

    For your parameters/return types, you'll want to have the C++/CLI expose methods with nice & normal .Net types. You'll need to copy the data to whatever the native C++ library wants to use.

    For the parameters, you'll want to use whatever collection object makes sense in .Net (probably IEnumerable<float>^, IList<float>^, or array<float,2>^), and copy the data to a std::vector manually (with a simple for loop, don't try to get fancy). For the return value, same thing in the opposite direction: create a new List<float>^, copy the data from the vector, and return the new list.