开发者

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

开发者 https://www.devze.com 2023-04-01 14:23 出处:网络
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 por开发者_JAVA技巧t it to C# because I want to dev

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 por开发者_JAVA技巧t 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?


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.


A couple of ways I can think of.

  1. Manual conversion to C# dll with possible code generation help of T4.
  2. Change your c++ library to a managed dll so you can use it in your c# project.
  3. Of course, you might use interop with your c++ library in your C# project. But in that case, I am not sure about purpose of your c++ library since you said it's a wrapper.

Since you already have c++ library that you can fully control, I would try to go with #2 first.

0

精彩评论

暂无评论...
验证码 换一张
取 消