How do I use C++ code in my C#(.NET 4.8) project ?

Bibeto 21 Reputation points
2022-08-24T19:10:06.32+00:00

Hello dear community,

Context :

I intend to use this library written in native C++ OMPL in my C# project in .NET 4.8. From what I saw, this library has 3 dependencies :
*Boost (version 1.58 or higher)
*CMake (version 3.5 or higher)
*Eigen (version 3.3 or higher)
The source code is in the github repository and can be built with CMake on windows. I intend to use a couple of functions/classes from this library not all its code.

Questions :

  1. What's the best approach of using it in my C# project in .NET 4.8 while keeping the best of performance in C++
  2. I heard there's DLLimport which uses unmanaged C++ code and makes it managed. For example I intend to use class A which uses class B. Do I have to do a DLLimport for class A AND B ?
Developer technologies | C#
{count} votes

Accepted answer
  1. RLWA32 49,541 Reputation points
    2022-08-24T19:54:43.313+00:00

    Generally speaking there are 3 methods to interoperate between C# and unmanaged (native) C/C++ --

    1. Use P/Invoke to call functions in a C/C++ DLL. DllImport is used to make the unmanaged function accessible to C# and also provides the information that the interop marshaler will use to call the function and pass data between the managed code and the unmanaged code. This approach is not appropriate for C++ classes. Neither C# or the interop marshaler understand C++ objects or their member functions (except for static functions that do not require a class instance). See https://learn.microsoft.com/en-us/dotnet/framework/interop/marshalling-data-with-platform-invoke
    2. Use an assembly written in C++/CLI to serve as an intermediary between C# and unmanaged C/C++. The advantage here is that the intermediary assembly can understand C/C++ functions as well as C++ objects and their member functions.
    3. Use COM. I mention this for completeness but it is most likely not relevant for your circumstances.
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,766 Reputation points Volunteer Moderator
    2022-08-24T19:22:58.81+00:00

    most likely you will need to use Explicit invoke via DllImport

    https://learn.microsoft.com/en-us/cpp/dotnet/calling-native-functions-from-managed-code?view=msvc-170

    you will build the c/c++ library and add reference to your C# project.

    then for each api in the library you want to call you will define a DllImport, which define the extra point and marshaling of parameters. if the library exposes any structs or classes you will need to define C# version and define the marshaling. I see the library includes python binding, so these will give clues.

    https://learn.microsoft.com/en-us/cpp/dotnet/overview-of-marshaling-in-cpp?view=msvc-170

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.