生成 P/Invoke 代码的最简单方法?

发布于 2024-08-26 07:30:52 字数 804 浏览 2 评论 0原文

我是一位经验丰富的.Net 程序员,但我一生中还没有编译过C/C++ 程序。现在我有了这个 C-dll、标头和文档(第三方,不是来自 Win API),我需要从中调用大约十个方法。

我正在考虑使用平台调用。我找到了这三个可以为我创建代码的工具:

可能还有

Pinvoker 的方法似乎与 Interop 助手有点不同和巫师。 Swig 我刚刚检查时发现这里没有问这个问题。

这些工具的优点和缺点是什么?

鉴于我对 C/C++ 不太了解,对我来说生成 P/Invoke 代码的最佳=最简单且最安全的方法是什么?

I am an experienced .Net programer, but have not compiled a C/C++ program in my life. Now I have this C-dll, headers and documentation (3rd party, not from Win API), from which I need to call about ten methods.

I was thinking of using Platform Invoke. I found these three tools that would create the code for me:

and possibly

Pinvoker seems to have a bit different approach than the Interop assistant and the Wizard. Swig I just found when checking that this question has not been asked here.

What are the pros and cons of these tools?

What would be the best = easiest and safest way for me to produce the P/Invoke code given that I don't know much about C/C++?

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

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

发布评论

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

评论(2

夕色琉璃 2024-09-02 07:30:52

有关互操作示例,请参阅 http://dotnetperls.com/dllimport-interop,以及此 MSDN 文章了解更多信息。这方面的圣经是 Adam Nathan 的 图书

基本上,您需要识别 DLL 中要调用的函数。这些需要用 extern 标记,以便外部世界可以访问它们。下一步可能会很棘手,那就是为该函数编写 DllImport。这需要在托管和非托管世界之间进行映射。您需要弄清楚如何将 C dll 中的任何复杂数据结构封送到托管代码中。

您应该检查 DLL 是否有任何类型的 COM 接口。首先确保您确实需要使用 P/Invoke。

SWIG 最初用于包装 C/C++ 代码以供脚本语言使用。它根据接口描述生成 C# 代码(请参阅教程)。如果 P/Invoke 是一个选项,我不建议在 C# 中使用它。

See http://dotnetperls.com/dllimport-interop for an interop example, and this MSDN article for more info. The bible for this stuff is Adam Nathan's book.

Basically you will need to identify the functions in the DLL you want to call. These need to marked with extern to make them accessible to the outside world. The next step, which can get tricky is writing a DllImport for the function. This needs to map between the managed and unmanaged worlds. You will need to work out how to marshal any complex data structures from the C dll into managed code.

You should check to see if there is any sort of COM interface to the DLL. Make sure you really need to use P/Invoke first.

SWIG was originally for wrapping C/C++ code for use from scripting languages. It generates C# code from an interface description (see tutorial). I wouldn't recommend using it from C# if P/Invoke is an option.

月亮邮递员 2024-09-02 07:30:52

如果函数的签名很简单,那么设置应该需要 10 分钟 - 即如果它们需要 2 个 char * 并返回一个 int。只有当它们变得复杂时,事情才会变得混乱

Pinvoke 上的圣经是 Adam Nathans - .NET 和 COM:完整的互操作性指南

我同意其他海报 - swig 是不对的事物

if the signatures of the functions are simple then it should take 10 minutes to setup - ie if they take 2 char * and return an int. Its only once they get complicated that it gets messy

The bible on pinvoke is Adam Nathans - .NET and COM: The Complete Interoperability Guide

And I agree with other poster - swig is not the right thing

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