如何在c#中使用C dll头文件库
我对 C# 非常陌生,我正在尝试为我的项目使用帮助包。 该包是用 c 编写的,并且有 1) /bin/ 几个.dll文件 2)/include/有头文件 3) /lib/msvc/.lib 文件 我的问题是如何在我的 C# WPF 项目中使用这些文件? 我知道C#中没有“#include”,并且不能通过添加到项目引用来导入.dll。那么我该如何在 C# 中做到这一点呢?
谢谢
I am very new to C#, and i am trying to use a help package for my project.
The package is written in c and has
1) /bin/ several .dll files
2) /include/ has a header file
3) /lib/msvc/ .lib file
my question is how can i use those files in my C# WPF project?
i know there is no "#include" in C#, and the .dll can not be imported by adding to the project's reference. so how can i do it in C#?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有很多方法可以实现这种“互操作”——我曾经做过一个名为“令人头晕的互操作”的演讲。关键是你的原生 DLL 的结构。既然你说它是用 C 语言编写的,那么它很可能非常适合与 PInvoke 一起使用,它最适合与 C 函数或 C++ 中的 C 风格函数一起使用。其他答案对语法有很好的指导,并且有助于在 C# 端声明您的函数(及其所需的参数)。
另外两个主要选项是 C++/CLI 包装器,当函数采用大量复杂类型时,这是一个不错的选择;或者是 COM,它几乎肯定需要您更改本机代码,因此不太适合这种情况。
There are a lot of ways to do this "interop" - I once did a talk called "head spinning interop". The key is the structure of your native DLL. Since you say it's in C, the chances are it's in perfect shape to be used with PInvoke, which works best with C functions or C-style functions in C++. The other answers have excellent pointers to the syntax and some help to get your function (and the parameters it takes) declared on the C# side.
The other two main options are a C++/CLI wrapper, a good choice when the functions take a lot of complicated types, or COM, which would almost certainly require you to change the native code so is not a good fit in this situation.
您需要使用 pInvoke
http://msdn.microsoft .com/en-us/library/aa288468%28VS.71%29.aspx
You need to use pInvoke
http://msdn.microsoft.com/en-us/library/aa288468%28VS.71%29.aspx
您可能会发现托管 C++ 很有用。您可以编写一个直接使用头文件和 .lib 的托管 C++ 库,并用一组供 C# 使用的 .NET 类包装它们。
You might find managed C++ useful. You can write a managed C++ library that uses the header files and the .lib directly, and wraps them with a set of .NET classes to be used by C#.
只是添加到答案中,您可能需要查看 此博客文章。它有一个指向 Visual Studio 插件的链接,可以从标头生成 P/Invoke 签名。
最好的
Just adding to the answers, you may want to take a look in this blog post. It has a link to a Visual Studio addin that can generate P/Invoke signatures from your headers.
Best
您需要添加一系列 C# 平台调用 方法定义。这告诉 C# 如何调用 .dll,并直接使用 C API。
头文件和库文件完全未使用。
You need to add a series of C# Platform Invocation method definitions. This tells C# how to call into the .dll, and use the C API directly.
The header and library files are completely unused.