C++/CLI 或 C# P/Invoke 用于大型 C 库,调用外部函数
现在我知道有很多与此相关的问题,但一般来说它们指的是 C++ 库。
我有一个相当重要的 C dll(大约 2800 个函数),我需要为其中大约 70 个可在 C# 中调用的 API 创建一个 API。我一开始使用 P/Invoke,但它非常乏味,因为我有复杂的结构要从 C 代码传递和返回(带有指向其他指针的结构、双指针等),并且它花了我很多时间来获得我需要的东西。
现在我根本不懂 C++/CLI,但我有一点 C++ 经验(虽然不多)。
我想知道是否值得为我的项目努力学习这一点。使用 C++ 包装器是否能让我不必费力处理编组结构、在全局堆中分配指针等?...
非常感谢这个伟大的社区
Now I know there are quite a few questions regarding this but in general they refer to c++ libraries.
I have a fairly important C dll (approximately 2800 functions) and I need to make an API for about 70 of these callable in C#. I started off using P/Invoke but it's pretty tedious because I have complex structures to pass and return from the C code (structures with pointers to others, double pointers, etc) and it's taking me alot of time to get what I need.
Now I don't know C++/CLI at all but I have a bit of experience with C++ (not a lot though).
I am wondering if it's worth making the effort to learn this for my project. Will using a C++ wrapper enable me not to have to struggle with marshalling structures, allocating pointers in global heap, etc or not?....
Thanks a lot and big ups to this great community
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是相当大的表面积。我认为 C++/CLI 会比 P/invoke 更容易。您不需要使用任何 C++ 功能,您可以编写本质上是 C 的内容,并使用 C++/CLI 编译和导出它。
That's quite a large surface area. I think C++/CLI will be easier than P/invoke. You don't need to use any C++ features, you can write what is essentially C and compile and export it with C++/CLI.
处理 C# 中的本机结构很困难。因此编写 C++/CLI 包装器更容易。您可以轻松地将这些结构转换/包装为
ref 类
,并在 C# 中使用它们。It is hard to deal with native structs from C#. So writing a C++/CLI wrapper is easier. You can convert/wrap those structs to
ref classes
without too much trouble and use them in C#.