C#调用rust编写的dll

通过 cargo new --lib add 创建 rust 库项目

lib.rs

 

#[no_mangle]
pub extern fn add(a: i32, b: i32) -> i32 {
    a + b
}
 
cargo.toml
增加 dll 配置

[lib]
name="addlist"
crate-type = ["dylib"]

运行  cargo build --release 生成 addlist dll 文件 

C#  代码

class Program
{
[DllImport("addlist.dll")]
private static extern Int32 add(Int32 a, Int32 b);
static void Main(string[] args)
{
Console.WriteLine(add(1, 2));
Console.ReadLine();
}
}

需要设置 一下 x86 或者 x64 就可以run起来了。

 

posted on 2020-02-19 11:26  阿水  阅读(200)  评论(1编辑  收藏  举报

导航