Search code examples
c#c++compatibilityporting

What is the best way to port a C++ library to C#?


I have a (header only) C++ library that I am looking to port to C#. It is a wrapper of the win api which I have made for a certain purpose. I want to port it to C# because I want to develop it further using C#. What is the most efficient (and easiest?) way to port it. Please note that I do not want something hectic because I don't want to spend more time porting the library than it took to make that library in the first place. So is there a way?


Solution

  • It depends very much on how big your lib is, how it is structured, how your memory allocation looks like, how much you made use of libs not available in C# (like the C++ std lib), how much C++ template programming you have used etc.

    Different strategies may be

    • try to port (parts of) it automatically by some code generator of your own
    • do not port it to C#, instead use C++/CLI (something I did very successfully in the past)
    • do it manually because the C++ concepts you have used so far don't map well to C#

    "Header only" does not seem to make a real difference. In fact, things may technically get a little bit easier when you have just one C++ file for each class to be ported to one C# class file.