P/Invoke或C ++/CLI,用于包装C库 [英] P/Invoke or C++/CLI for wrapping a C library

查看:46
本文介绍了P/Invoke或C ++/CLI,用于包装C库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有中等大小(40多种功能)的C API,需要从C#项目中调用.这些函数在逻辑上分解成几个类,这些类将通过API呈现给项目的其余部分.

Have a moderate size (40-odd function) C API that needs to be called from a C# project. The functions logically break up to form a few classes that will be API presented to the rest of the project.

从健壮性,可维护性,部署等方面,是否有客观原因偏爱P/Invoke或C ++/CLI来实现该API的互操作性?

Are there any objective reasons to prefer P/Invoke or C++/CLI for the interoperability underneath that API, in terms of robustness, maintainability, deployment, ...?

我可能想到的问题可能是但没有问题的:

The issues I could think of that might be, but aren't problematic are:

  • C ++/CLI将需要单独的程序集,P/Invoke类可以位于主程序集中.(我们已经有多个程序集,而且无论如何都会有C dll,所以这不是主要问题).
  • 两种方法之间的性能似乎没有明显的不同.

我不确定的问题是:

  • 我的感觉是,如果出现互操作问题,C ++/CLI会更容易调试,是真的吗?
  • 对语言的熟悉程度足以让人们知道C#和C ++,但在这里很少了解C ++/CLI的细节.

还有什么?

推荐答案

在使用现有C库的情况下,我更喜欢PInvoke.虽然PInvoke有时会有些乏味且麻烦,但它是一项相当广为人知的技术,具有越来越多的可用工具和Internet文档.一般来说,无论遇到什么问题,Web上都已经有可用的示例,或者快速检查堆栈溢出将提供解决方案.

In the case where I am working with an existing C library, I prefer PInvoke. PInvoke, while a bit tedious and troublesome at times, is a fairly well understood technology that has an ever growing set of tools and internet documentation available. Generally speaking, whatever problem you run into, there is already a sample available on the web, or a quick check on stack overflow will provide a solution.

C ++/CLI是一项很棒的技术,但是与针对Interop特定情况的PInvoke相比,恕我直言,其文档是受限制的.它还没有PInvoke拥有的互操作解决方案的工具基础架构.对我来说,为可以用PInvoke解决的方案添加C ++/CLI组件似乎太昂贵了.

C++/CLI is a great technology, but IMHO its documentation is limited as compared to PInvoke for interop specific scenarios. It also doesn't have the tooling infrastructure for interop solutions that PInvoke has. Adding a C++/CLI assembly for a scenario that can be solved with PInvoke just seems too costly to me.

另一方面,如果我使用的是大型C ++库,则我认为C ++/CLI还要多一些.PInvoke不适用于C ++,我必须最终添加某种中间层.一个小的C层来包装所有C ++函数调用,或者一个C ++/CLI库来弥合差距.在这种情况下,C ++/CLI对我来说更自然.

On the other hand, if I'm working with a large C++ library, I consider C++/CLI a bit more. PInvoke does not work with C++, and I must end up adding some kind of intermediate layer. Either a small C layer to wrap all of the C++ function calls or a C++/CLI library to bridge the gap. C++/CLI feels a bit more natural to me in this case.

这篇关于P/Invoke或C ++/CLI,用于包装C库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆