Search code examples
c#comc++-clicom-interopmanaged-c++

C++ calling C# options


We have native Win32 C++ code and a set of C# assemblies which we wish to call from the C++ code. I summaries our optios as:

  1. Use COM. The C# code would need to be decorated with additional attributes (GUID, COMVisible). The C# assemblies would need to be registered regasm and would then be available to the native C++ code via COM.

  2. Use a C++/CLI (formerly managed C++) wrapper class. A C++ class could be added to the native C++ project. That class would be compiled with /clr. The native C++ code would call the C++/CLI class which would then call the .Net code. No COM involved. The CLR is started by magic as required with marshalling handled by the C++/CLI extenstions.

  3. Host an instance of the CLR in the native C++ code.

I'm going to discount option 3 as I don't see the benefits over option 2 other than we lose the need for a wrapper class. So the question is, what are the pros/cons of option 1 versus option 2?

Thanks in advance.


Solution

  • Option 2 will perform the best, and be the most seamless and maintainable, IMO.

    There is really no advantage to option 1 that I've found. Using C++/CLI seems to function much better, work faster, and be much simpler in general.

    You also can, btw, just use the C# assembly directly without having a wrapper class. This does require compiling any files that want to use it with /CLR, but it works quite well.