简体   繁体   English

如何在C#中使用C dll标头库

[英]how to use C dll headers library in c#

I am very new to C#, and i am trying to use a help package for my project. 我是C#的新手,我正在尝试为我的项目使用帮助包。 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? 该程序包用c编写,具有1)/ bin /几个.dll文件2)/ include /具有头文件3)/ lib / msvc / .lib文件我的问题是我如何在C#WPF项目中使用这些文件? i know there is no "#include" in C#, and the .dll can not be imported by adding to the project's reference. 我知道C#中没有“ #include”,并且无法通过添加到项目的引用中来导入.dll。 so how can i do it in C#? 那么我该如何在C#中做到这一点呢?

Thanks 谢谢

You need to add a series of C# Platform Invocation method definitions. 您需要添加一系列C# 平台调用方法定义。 This tells C# how to call into the .dll, and use the C API directly. 这告诉C#如何调用.dll,并直接使用C API。

The header and library files are completely unused. 头文件和库文件完全未使用。

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. 它具有指向Visual Studio加载项的链接,该加载项可以从标头生成P / Invoke签名。

Best 最好

You might find managed C++ useful. 您可能会发现托管C ++很有用。 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#. 您可以编写一个托管的C ++库,该库直接使用头文件和.lib,并将它们包装在一起以供C#使用.NET类集。

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. 关键是您本机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++. 既然您说它是用C语言编写的,则很有可能与PInvoke一起使用,它与C函数或C ++中的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. 其他答案都很好地指出了语法,并为在C#端声明函数(及其参数)提供了一些帮助。

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. 其他两个主要选项是C ++ / CLI包装器,当函数采用许多复杂类型或COM时,这是一个不错的选择,这几乎肯定会要求您更改本机代码,因此在这种情况下不太适合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM