简体   繁体   English

使用C ++ / CLI而不是C#来包装(第三方)C库(dll)有什么优点?

[英]What are the advantages of using C++/CLI instead of C# for wrapping a (third party) C-library (dll)?

I have a third party C-libraries which I need to use from C#. 我有一个第三方C库,需要从C#中使用。 The rest of the system is written in C#, so using C++/CLI is only an option for the wrapper. 系统的其余部分是用C#编写的,因此使用C ++ / CLI只是包装程序的一种选择。

What are the advantages of using C++/CLI for wrapping C-libraries instead of C#? 使用C ++ / CLI来包装C库而不是C#的优点是什么? Is there any advantages in creating a C#-wrapper? 创建C#包装器有什么好处吗?

The libraries contains some structs, a few enums and lots of functions. 这些库包含一些结构,一些枚举和许多功能。

Edit: Would it be possible to use C structs and enums in C# without wrapping them? 编辑:是否可以在C#中使用C结构和枚举而不包装它们?

The advantage of using C++/CLI is that you can write both native code and managed code in the same assembly. 使用C ++ / CLI的优点是可以在同一程序集中同时编写本机代码和托管代码。

Interacting with C Libraries is easier when using C++/CLI, you can work with your libraries normally with all the and still expose fully managed .NET interfaces or classes. 使用C ++ / CLI时,与C库进行交互更容易,您可以正常使用所有库并仍公开完全托管的.NET接口或类。 But of course C++/CLI adds some complexity. 但是C ++ / CLI当然会增加一些复杂性。

On the other hand in C#, you need to redeclare all structs and use DllImport to declare your C-Functions again in C#. 另一方面,在C#中,您需要重新声明所有结构,并使用DllImport在C#中再次声明您的C函数。

I'd say for simple interop tasks (like just some functions and few structs), using C# and PInvoke is just ok. 我会说,对于简单的互操作任务(例如一些函数和少量结构),使用C#和PInvoke就是可以的。 If using a complete API that would require lots of struct redefinitions, pinning, conversions and whatever, C++/CLI is probably the better choice. 如果使用需要大量结构重新定义,固定,转换等操作的完整API,则C ++ / CLI可能是更好的选择。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM