2

I have C# wrapper project that uses C++ unmanaged dll code with help of [DllImport] attribute and static extern methods. This is the preferred method described by MSDN.

The problem is that we can have only one of the c++ runner, which means we can't run tasks in parallel.

How to call the unmanaged C++ without using the dllimport + static extern methods so that when we instantiate one of the c# objects it instantiates a new c++ dll object

This is how we are calling C++ code right now.

public class FMU : IDisposable
    {

        [DllImport("FMU.dll", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
        private static extern IntPtr Load(string fmuFileName, bool isCoSimulation, AddMessageDelegate messageDelegate, int arrayLocation);

        [DllImport("FMU.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern IntPtr Instantiate(IntPtr fmu, bool isCoSimulation, bool loggingOn, int nCategories, string[] categories);

        [DllImport("kernel32", SetLastError = true)]
        private static extern bool FreeLibrary(IntPtr hModule);
Hasitha Jayawardana
  • 2,326
  • 4
  • 18
  • 36
Charlotte
  • 31
  • 3
  • interested to know the answers here. I am not clear about this part "...have only one of the c++ runner, which means we can't run tasks in parallel." My understanding about the working of dlls is as explained in this [https://stackoverflow.com/questions/2846310/are-dll-files-loaded-once-for-every-program-or-once-for-all-programs]. Any clarifications would be helpful. – dorKKnight Jul 02 '19 at 11:35
  • There is an alternative way of exposing the functionality from unmanaged C++ code and consume it from C# code. You can wrap the native C++ code with a managed C++/CLI project and then refer it in the C# project. This [article](https://www.codeproject.com/Articles/651516/Exposing-native-to-managed-Cplusplus-CLI-vs-P-Invo) is a bit old but it explains the steps and the concept. You can further explore [msdn](https://learn.microsoft.com/en-us/cpp/dotnet/using-cpp-interop-implicit-pinvoke?view=vs-2019). – dorKKnight Jul 02 '19 at 11:44

0 Answers0