Question

Im rewriting an existing C++ COM library (STL, Boost & ATL) to C# as I add new functionality.

However there are a couple of large well tested C++ classes which I would rather not rewrite.

Conceptually (and for backwards compatibility) these belong in the same COM library, however I can draw a neat line between the C++ and C# such that all interaction is via an explicit use of a COM interface.

Whats the best way to include both the C# components and the C++ components in the same library?

Was it helpful?

Solution

The best way is to write a C# COM library which serves as a wrapper. The new C# COM library you're writing sounds like it can serve this purpose already.

Into this C# library, you can place your rewritten C# code and a wrapped version of your C++ code. As for how you wrap the C++ code, you can do 2 different things here:

  • Put a thin wrapper C# class around it, and PInvoke into the C++ DLLs
  • Write a C++/CLI wrapper and include your C++ classes directly this way

The first one allows you to keep using C# code and syntax, but is somewhat limiting in terms of your interaction with the C++ classes (you can only access what's exposed in the DLL exports).

The second forces you to write some rather unpleasant C++/CLI wrapper code, but allows you to use your C++ classes in all their glory.

This link here is a very helpful resource for writing C++/CLI wrappers for C++ libraries.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top