产生的P / Invoke code最简单的方法? [英] Easiest way to generate P/Invoke code?

查看:178
本文介绍了产生的P / Invoke code最简单的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名经验丰富的.Net程序设计师,但在我的生活都没有编制了C / C ++程序。现在我有这个C-DLL,头和文档(第三方,不是从运API),从中我需要调用约十个方法。

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.

我在考虑使用平台调用的。我发现这三个工具,将创建code对我来说:

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

  • PInvoker: http://www.pinvoker.com
  • P/Invoke Interop Assistant: http://www.codeplex.com/clrinterop
  • P/Invoke Wizard: http://www.paulyao.com/res/pinvoke/pinvoke.aspx

和可能

  • Swig: http://www.swig.org/

Pinvoker似乎比互操作的助手和向导有点不同的方法。检查,这个问题还没有被要求在这里痛饮的时候我才发现。

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?

什么是对我产生的P / Invoke code因为我不很了解的最佳=最简单,最安全的方法C / C ++?

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++?

推荐答案

请参见 http://dotnetperls.com/dllimport -interop 一个互操作为例,这个 MSDN文章获取更多信息。这个东西圣经亚当森的

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.

基本上,你需要确定你要调用的DLL函数。这些都需要打上的extern让他们到外面的世界进行访问。下一步,它可以变得棘手的写功能的的DllImport。这需要在托管和非托管的世界之间进行映射。您将需要制定出如何封送任何复杂的数据结构从C DLL到托管code。

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.

您应该检查,看看是否有任何形式的COM接口的DLL。确保你真正需要使用的P / Invoke第一。

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.

痛饮原本缠绕在C / C ++ code从脚本语言的使用。它能够从接口描述C#code(请参阅教程)。我不建议使用它从C#若P / Invoke的是一个选项。

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.

这篇关于产生的P / Invoke code最简单的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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