简体   繁体   English

将 C++ 库移植到 C# 的最佳方法是什么?

[英]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#.我有一个(仅标头)C++ 库,我希望将其移植到 C#。 It is a wrapper of the win api which I have made for a certain purpose.它是我为特定目的制作的 win api 的包装器。 I want to port it to C# because I want to develop it further using C#.我想将它移植到 C#,因为我想使用 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.这在很大程度上取决于你的 lib 有多大、它的结构如何、你的内存分配是什么样的、你使用了多少 C# 中不可用的 lib(如 C++ std lib)、你使用了多少 C++ 模板编程等等。

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)不要将它移植到 C#,而是使用 C++/CLI(我过去做得非常成功)
  • do it manually because the C++ concepts you have used so far don't map well to C#手动执行,因为到目前为止您使用的 C++ 概念不能很好地映射到 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.事实上,当您将每个类的一个 C++ 文件移植到一个 C# 类文件时,从技术上讲,事情可能会变得容易一些。

A couple of ways I can think of.我能想到的几种方法。

  1. Manual conversion to C# dll with possible code generation help of T4.使用 T4 的可能代码生成帮助手动转换为 C# dll。
  2. Change your c++ library to a managed dll so you can use it in your c# project.将您的 c++ 库更改为托管 dll,以便您可以在 c# 项目中使用它。
  3. Of course, you might use interop with your c++ library in your C# project.当然,您可以在 C# 项目中使用与 C++ 库的互操作。 But in that case, I am not sure about purpose of your c++ library since you said it's a wrapper.但在那种情况下,我不确定你的 C++ 库的用途,因为你说它是一个包装器。

Since you already have c++ library that you can fully control, I would try to go with #2 first.由于您已经拥有可以完全控制的 C++ 库,因此我会先尝试使用 #2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 包装我们的软件库以提供C ++和C#客户端应用程序的最佳方法是什么? - What is the best way to wrap our software library to serve both C++ and C# client apps? 在C#中为外部开发人员提供库的最佳方法是什么? - What's the best way to provide a library to external developers in C#? 在 C# 项目中添加 class 库的最佳方法是什么? - What is the best way to add class library in C# project? 用C ++编写控件以便可以被C ++和C#程序使用的最佳方法是什么? - What is the best way to write a control in C++ so that it can be used by C++ and C# programs? 在 C/C#/C++ 中执行反向循环的最佳方法是什么? - What's the best way to do a backwards loop in C/C#/C++? 在两者都运行时将信息从C#应用程序传递到C ++应用程序的最佳方法是什么? - What is the best way to pass information from a C# app to a C++ app while both are running? 用C#中的List &lt;&gt;表示的最佳方法是C ++中的数组数组 - Best way to represent with List<> in C# what was an array of arrays in C++ C#app与unix c ++ app进行通信的最佳方式是什么? - What is the best way for C# app to communicate unix c++ app 解释ac#program的最佳方法是什么? - what is the best way to explain a c# program? 在C#中等待的最佳方法是什么? - What is the best way to wait in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM