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

发布于 2024-12-02 01:13:13 字数 156 浏览 3 评论 0原文

我有一个(仅标头)C++ 库,我希望将其移植到 C#。它是我出于某种目的而制作的 win api 的包装。我想将其移植到 C#,因为我想使用 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

皇甫轩 2024-12-09 01:13:13

这在很大程度上取决于您的库有多大、它的结构如何、您的内存分配方式、您使用了多少 C# 中不可用的库(例如 C++ 标准库)、您使用了多少 C++ 模板编程 不同的

策略可能是

  • 尝试通过您自己的代码生成器自动移植(部分)它,
  • 不要将其移植到 C#,而是使用 C++/CLI(我过去做得非常成功)
  • 手动进行,因为 C++到目前为止您使用过的概念没有很好地映射到 C#

“仅标头”似乎没有真正的区别。事实上,当每个类只有一个 C++ 文件要移植到一个 C# 类文件时,技术上事情可能会变得容易一些。

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.

请爱~陌生人 2024-12-09 01:13:13

我能想到的几种方法。

  1. 通过 T4 可能的代码生成帮助手动转换为 C# dll。
  2. 将您的 C++ 库更改为托管 DLL,以便您可以在 C# 项目中使用它。
  3. 当然,您可以在 C# 项目中使用与 C++ 库的互操作。但在这种情况下,我不确定你的 c++ 库的用途,因为你说它是一个包装器。

由于您已经拥有可以完全控制的 C++ 库,因此我会首先尝试使用#2。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文