• C# 调用 Rust 编写的 dll 之二:输入输出简单数值


    C# 调用 Rust 编写的 dll 之二:输入输出简单数值

    文中所有的程序运行环境为:windows 10 64bit,Net 5.0,Rust 1.51;乌龙哈里 2021-05-05

    接上文《C# 调用 Rust 编写的 dll 之一:创建 dll》

    一、输入输出 int

    1、输入 int:

    rust lib.rs 中写入

    #[no_mangle]//不写这个会把函数名编译成其他乱七八糟的
    pub extern fn input_int(n:i32){
        println!("rust dll get i32: {}",n);
    }

    c# 调用

            [DllImport("D:\LabRust\Learn\ln_dll\target\release\rustdll.dll",
            EntryPoint = "input_int",
            CallingConvention = CallingConvention.Cdecl)]
            public static extern void InputInt(int n);
            static void TestInputInt(int n)
            {
                InputInt(n);
            }

    在 main() 中修改:

            static void Main(string[] args)
            {
                //TestHelloRust();
                TestInputInt(100);
    
                Console.WriteLine("Hello C# !");
                Console.Read();
            }

    结果:

    /*
    rust dll get i32: 100
    Hello C# !
    */

    2、输出 int

    rust 写入函数:

    #[no_mangle]//不写这个会把函数名编译成其他乱七八糟的
    pub extern fn output_int(n:i32)->i32{
        println!("Rust Dll get i32: {}",n);
        n+100
    }

    c# 调用:

            [DllImport("D:\LabRust\Learn\ln_dll\target\release\rustdll.dll",
            EntryPoint = "output_int",
            CallingConvention = CallingConvention.Cdecl)]
            public static extern int OutputInt(int n);
            static int TestOutputInt(int n)
            {
                return OutputInt(n);
            }
            static void Main(string[] args)
            {
                //TestHelloRust();
                //TestInputInt(100);
                Console.WriteLine($"output:{TestOutputInt(100)}");
    
                Console.WriteLine("Hello C# !");
                Console.Read();
            }

    结果:

    /*
    Rust Dll get i32: 100
    output:200
    Hello C# !
    */

    3、修改 int

    能不能把输入值直接修改呢?直接上代码看看。 rust lib.rs:

    #[no_mangle]//不写这个会把函数名编译成其他乱七八糟的
    pub extern fn modify_int(n:&mut i32){
        println!("Rust Dll get i32: {}",n);
        *n+=100;
    }

    c# 调用并运行:

            [DllImport("D:\LabRust\Learn\ln_dll\target\release\rustdll.dll",
            EntryPoint = "modify_int",
            CallingConvention = CallingConvention.Cdecl)]
            public static extern void ModifyInt(ref int n);
            static void TestModifyInt(ref int n)
            {
                ModifyInt(ref n);
            }
            static void Main(string[] args)
            {
                int n = 100;
                TestModifyInt(ref n);
                Console.WriteLine($"modify:{n}");
    
                Console.WriteLine("Hello C# !");
                Console.Read();
            }

    结果为:

    /*
    Rust Dll get i32: 100
    modify:200
    Hello C# !
    */

    大功告成,能修改!

    试了一下 tuple 元组,编译时说不是 FFI safe,就不弄了。

  • 相关阅读:
    [函數] Firemonkey Android 取得系统参数设定的字型大小
    [示例] 访问类的私有属性
    [修正] 移动平台曲线不平滑的问题(如:TRectangle, TPath...等)
    [修正] Firemonkey TSelection 控件等比缩放时,左下角拉动问题
    [修正] iOS 10 使用相机及相簿闪退的问题修正
    报表之表头
    报表字段刷新
    tfs解除锁
    sql server中排名的问题
    TFS查询无法在Excel中打开
  • 原文地址:https://www.cnblogs.com/leemano/p/14732540.html
Copyright © 2020-2023  润新知