Search code examples
c#rustffi

Pass string from C# to Rust using FFI


I try to pass a string as a function argument to a Rust library (cdylib) as described in the Rust FFI Omnibus.

I tried to however omit the libc dependency, because I think it should not be necessary anymore. I am using Rust 1.50.0 and .net 5.0.103.

From the the documentation it seems to me as if the CStr::from_ptr() function constructs a CStr from the pointer by reading all bytes until the null-termination. And that C# strings are automatically marshalled to C compatible strings (and are therefore null-terminated). My problem however is, that I do not get the full string that I supply as the function argument, instead I only get the first character as the string.

This is my lib.rs:

use std::os::raw::c_char;
use std::ffi::CStr;

#[no_mangle]
pub extern fn print_string(text_pointer: *const c_char) {
    unsafe {
        let text: String = CStr::from_ptr(text_pointer).to_str().expect("Can not read string argument.").to_string();
        println!("{}", text);
    }
}

and my Cargo.toml:

[package]
name = "mylib"
version = "0.1.0"
authors = ["FrankenApps"]
edition = "2018"

[lib]
crate-type = ["cdylib"]

And this is my C# code:

using System;
using System.Runtime.InteropServices;

namespace dotnet
{
    class Program
    {
        [DllImport("mylib.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        public static extern void print_string(string text);

        static void Main(string[] args)
        {
            print_string("Hello World.");
        }
    }
}

In this case the output when I run the program is:

H

When I run the linked sample, I get an error:

thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: Utf8Error { valid_up_to: 1, error_len: Some(1) }', src\lib.rs:12:32

However when I only use ASCII characters and modify the code like that:

Rust:

use libc::c_char;
use std::ffi::CStr;

#[no_mangle]
pub extern "C" fn how_many_characters(s: *const c_char) -> u32 {
    let c_str = unsafe {
        assert!(!s.is_null());

        CStr::from_ptr(s)
    };

    let r_str = c_str.to_str().unwrap();
    println!("{}", r_str.to_string());
    r_str.chars().count() as u32
}

C#

using System;
using System.Runtime.InteropServices;

class StringArguments
{
    [DllImport("mylib", EntryPoint="how_many_characters")]
    public static extern uint HowManyCharacters(string s);

    static public void Main()
    {
        var count = StringArguments.HowManyCharacters("Hello World.");
        Console.WriteLine(count);
    }
}

I do get the desired output:

Hello World.
12

My question is what did I do wrong in my own sample, where I tried to not use libc? Is there any difference between c_char in libc and the standard library, that makes them behave differently?

My guess is that I missed something simple, because I do expect this to work...


Solution

  • Since .NET 4.7 you can use MarshalAs(UnmanagedType.LPUTF8Str) so the following should work fine:

    using System.Runtime.InteropServices;
    
    namespace dotnet
    {
        class Program
        {
            [DllImport("mylib.dll")]
            public static extern void print_string([MarshalAs(UnmanagedType.LPUTF8Str)] string utf8Text);
    
            static void Main(string[] args)
            {
                print_string("göes to élevên");
            }
        }
    }