使用非托管 C++具有 .NET API 的库
我有一个项目,我必须使用 API 来读取专有文件类型的文件。它是闭源的,只是一堆 DLL,可以作为参考添加到我的项目中(目前我使用的是 C#)。然而,我还希望使用 C++ 库 OpenMS (http://open-ms.sourceforge.net/) 来管理和分析我获得的数据。我读过一些关于 P/Invoke 和创建包装器的内容(还有其他一些人建议了其他方法,例如: http://www.codeguru.com/cpp/cpp/cpp_management/interop/article.php/c6867 )但我不知道如何我应该讨论这个问题以及最好的方法是什么,特别是因为我见过的大多数示例都非常简单,并且这个库有许多复杂的数据类型等。由于库非常大,是否可以自动化创建包装器的过程?
I have a project where I must make use of an API to read file of a proprietary filetype. It is closed source, just a bunch of DLLs that can be added as a reference in my project (currently I am using C#). However, I also wish to make use of a C++ library, OpenMS (http://open-ms.sourceforge.net/) in order to manage and analyse the data I obtain. I have read a bit about P/Invoke and creating a wrapper (and some others suggested other methods such as this: http://www.codeguru.com/cpp/cpp/cpp_managed/interop/article.php/c6867 ) but I am not sure how I should go about this and what the best way would be, especially as most examples I have seen are very simple and this library has many complex data types etc. Is it possible to automate the process of creating a wrapper as the library is very large?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不添加非托管 DLL 作为引用;仅适用于用 .NET 语言编写的托管 DLL。
如果您使用 C#,P/Invoke 是您唯一的选择。是的,这样做的难度和消耗的时间会随着库的复杂程度成比例地增加。您需要确保库使用的所有数据类型都有托管等效项,如有必要,您可以自己显式定义它们。不幸的是,我不知道有什么方法可以自动化这个过程。
如果可以选择切换到 C++/CLI,那么您将更加轻松,因为您可以直接使用非托管库,将其与 .NET 代码混合。
You don't add unmanaged DLLs as references; that only works for managed DLLs written in a .NET language.
If you're using C#, P/Invoke is your only option. Yes, the difficulty and time consumed by doing so will increase proportionately with the complexity of the library. You will need to ensure that there are managed equivalents of all the data types used by the library, defining them yourself explicitly if necessary. Unfortunately, I am not aware of any way to automating this process.
If switching to C++/CLI is an option, you will have a much easier time as you can consume the unmanaged library directly, mixing it with your .NET code.
事实上,Mono 开发者开发的东西与 P/Invoke 或 C++/CLR 不同,并且可能对 C# 开发人员更友好,
http://tirania.org/blog/archive/2011/Dec-19.html
问题是它现在有多成熟,但你可以使用它并学习,
https://github.com/mono/cxxi
Mono guys in fact develop something different from P/Invoke or C++/CLR and may be more friendly to C# developers,
http://tirania.org/blog/archive/2011/Dec-19.html
The question is how mature it is right now, but you can play with it and learn,
https://github.com/mono/cxxi
不确定,也许这个工具可以帮助你 - swig
Not shure, may be this tool help you - swig