C++包装本机互操作企业规模

发布于 2024-08-11 18:40:57 字数 217 浏览 2 评论 0原文

我想知道封装用 C++ 编写的大型库以使其可以在 C# 中访问的最佳方法是什么。

我以前做过互操作方面的工作,我喜欢 IJW。但我不确定如何用一个巨大的库来实现这种方法。我想知道是否有任何模式可以使用,否则我只需为 C++ 库中存在的每个类编写一个包装器,这并不是真正易于管理的。

另外,我不能只为图书馆提供一个单一的立面,因为它非常大,而且在这一点上势不可挡,所以任何想法都将不胜感激。

I want to know what is the best approach to wrapping a massive library written in C++ to make it accessible in C#.

I have done work with interop before, and I love IJW. But I am not sure of how to implement this approach with a huge library. I am wondering if there is any pattern to use, otherwise I just have to write a wrapper around every class that exists in the c++ library which is not really manageable.

Also, I cannot just provide a single facade into the library, as again, it is very big, and overwhelming at this point, so any ideas would be greatly appreciated.

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

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

发布评论

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

评论(2

俏︾媚 2024-08-18 18:40:57

如果性能是最关心的问题,请留在 IJW。如果您必须编写包装类或者更喜欢代码的可读性,我建议您创建 COM 包装器,因为有 IDE 向导可以帮助您生成代码,并且本机客户端也可以使用 COM。这就是 Microsoft 公开 Office 和 IE API 的方式。

If performance is a top concern, stay IJW. If you have to write wrapper classes or prefer more readability in your code, I suggest you create COM wrappers as there are IDE wizard to help you on generating code and COM can also be used by native clients. That's how Microsoft expose their APIs for Office and IE.

枉心 2024-08-18 18:40:57

我经历过与你描述的类似的过程。在我们的情况下,没有简单的方法来包装类(IJW 对于大型 C++ 库来说不是一个可行的解决方案),因此我们确实包装了每个类。

我们首先围绕我们需要在应用程序中使用的核心类编写了一个包装器,并根据需要为其他类添加了包装器。这些东西对我们特别有帮助:

  • 在我们的包装层中,我们大量使用宏来在 C++ 代码和 .NET 概念之间进行转换。例如,宏用于将“属性”类型方法转换为真正的.NET 属性。
  • 我们总是使用“property IntPtr NativeObject { get(); }”将本机 C++ 类公开为 IntPtr。这使我们能够将包装器拆分为多个组件,同时仍然允许包装器对象将本机对象相互传递。
  • 一个彻底的 nunit 测试套件,用于验证一切是否按预期工作。

这涉及到很多工作,但是围绕每个类编写包装器是可以管理的,但(显然)只有在获得足够的好处时才值得。 “手动”编写包装器还使我们能够对互操作进行细粒度的控制,这对于我们项目的成功至关重要。

I've been through a process similar to what you describe. In our situation there was no easy way to wrap the classes (IJW isn't a workable solution for a large C++ library), so we did wrap every class.

We wrote a wrapper around first the core classes we needed to consume in our app and added wrappers for other classes as necessary. These things in particular helped us:

  • In our wrapper layer we made heavy use macros to translate translate between c++ code and .NET concepts. e.g., Macros were used to translate 'property' type methods into true .NET properties.
  • We always exposed the native C++ class as an IntPtr using "property IntPtr NativeObject { get(); }". This allowed us to split the wrapper into multiple components while still allowing the wrapper objects to pass native objects to one another.
  • A thorough nunit testsuite to verify everything worked as expected.

There is a lot of work involved, but writing a wrapper around every class is manageable, but (obviously) only worth while if you get sufficient benefit. Writing the wrapper "by hand" also gave us fine-grained control over the interop which was essential to the success of our project.

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