views:

203

answers:

3

Hi

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.

+3  A: 

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.

Reed Copsey
+1  A: 

For option 1 your main pro would be not having to write a wrapper class which can get hairy depending on your project.

For option 2 you won't have to modify your managed library to facilitate unmanaged use, which is sometimes not an option.

For me it comes down to where you want to make your code changes.

SpaceghostAli
+1  A: 

With option 2 you also have a pretty straightforward way of subsequently convert your whole application to C++/CLI to avoid the managed/unmanaged transitions that you will get. The transitions could be an issue depending on how you use your referenced assemblies i.e. getting a performance hit.

So far I have had only positive experiences with C++/CLI and can recommend going that route.

Anders K.