从C#调用c函数

Dar*_*ght 9 .net c# function

作为一个简单的例子,我有一个C函数,它接收一个int,递增它并返回递增的值.另一方面,我在C#中输入文本框的输入数字.如何调用C函数来处理数字并从c#返回?感谢提前

Res*_*uum 12

您正在寻找P/Invoke.

System.Runtime.InteropServices如果您的C DLL包含一个名为的函数,您将需要一个引用,然后执行以下操作increase_int:

[DllImport("mylib.dll")]
private static extern int increase_int(int in_value);
Run Code Online (Sandbox Code Playgroud)

并从你的代码中使用它

int newValue = increase_int(oldValue);
Run Code Online (Sandbox Code Playgroud)