简体   繁体   English

如何从C程序生成DLL以在C#项目中使用

[英]How generate a DLL from C program for use in a C# project

I need to use a program made in C for a C# project , and for this I need to generate a DLL in Visual Studio 2013, anyone have any tutorial or know how to do it? 我需要使用C语言编写的用于C#项目的程序,为此,我需要在Visual Studio 2013中生成DLL,任何人都有任何教程或知道该怎么做? I want make a DLL for a structs and read/write functions in C, cheers guys. 我想为C中的结构和读/写函数制作一个DLL,干杯们。

You have to use p/invokes. 您必须使用p / invokes。 I think this is very often discussed here. 我认为这在这里经常讨论。 Take a look at: Access a method from a DLL from C# program 看一下: 从C#程序的DLL访问方法

DLL is probably overkill for what you are doing. DLL可能对您正在执行的操作过于杀伤。 You can just write a C procedure and P/Invoke it using CDecl. 您可以只编写一个C过程并使用CDecl P /调用它。 The problem is that you need to know a whole lot for this. 问题是您需要对此了解很多。 If you are simply trying to iterate over an array fast for a performance critical section, you are better off using structs in C#, slapping unsafe on your method, and then using pointers and addresses to do what you want to do. 如果您只是想快速遍历性能关键部分的数组,最好使用C#中的结构,在方法上使用不安全的方法,然后使用指针和地址来执行所需的操作。 Code that looks a whole lot like C/C++ is perfectly legal in C#. 看起来很像C / C ++的代码在C#中是完全合法的。

See a reference here: MSDN : Unsafe Pointers 在此处查看参考: MSDN:不安全指针

Also fished out a somewhat dated reference showing how to P/Invoke the visual c Runtime printf function as an example. 还以示例为参考,其中引用了一些过时的参考资料,该参考资料显示了如何P /调用可视c运行时printf函数。 Keep in mind that things get really hard when you need to give a pointer to a function and when you need to read offsets etc. to pass around structs. 请记住,当您需要提供指向函数的指针以及需要读取偏移量等以传递结构时,事情会变得非常困难。 You'll need to pin anything you pass into the method to stop it from being moved by the garbage collector, which will also have performance implications. 您将需要将传递给该方法的所有内容固定,以防止其被垃圾收集器移动,这也将影响性能。

MSDN: CDecl P/Invoke example MSDN:CDecl P /调用示例

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

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