71 releases

0.14.19 Sep 14, 2023
0.14.18 May 21, 2023
0.14.17 Jan 16, 2023
0.14.15 Nov 16, 2022
0.5.2 Jul 30, 2021

#72 in FFI

Download history 42/week @ 2023-06-07 36/week @ 2023-06-14 189/week @ 2023-06-21 114/week @ 2023-06-28 83/week @ 2023-07-05 54/week @ 2023-07-12 61/week @ 2023-07-19 63/week @ 2023-07-26 51/week @ 2023-08-02 58/week @ 2023-08-09 167/week @ 2023-08-16 220/week @ 2023-08-23 127/week @ 2023-08-30 205/week @ 2023-09-06 96/week @ 2023-09-13 39/week @ 2023-09-20

538 downloads per month
Used in edge-transformers

MIT license

4.5K SLoC

Generates C# bindings for Interoptopus.


Assuming you have written a crate containing your FFI logic called example_library_ffi and want to generate C# bindings, follow the instructions below.

Inside Your Library

Add Interoptopus attributes to the library you have written, and define an inventory function listing all symbols you wish to export. An overview of all supported constructs can be found in the reference project.

use interoptopus::{ffi_function, ffi_type, Inventory, InventoryBuilder, function};

pub struct Vec2 {
    pub x: f32,
    pub y: f32,

pub extern "C" fn my_function(input: Vec2) -> Vec2 {

pub fn my_inventory() -> Inventory {

Add these to your Cargo.toml so the attributes and the binding generator can be found (replace ... with the latest version):

crate-type = ["cdylib", "rlib"]

interoptopus = "..."
interoptopus_backend_csharp = "..."

Create a unit test in tests/bindings.rs which will generate your bindings when run with cargo test. In real projects you might want to add this code to another crate instead:

use interoptopus::util::NamespaceMappings;
use interoptopus::{Error, Interop};

fn bindings_csharp() -> Result<(), Error> {
    use interoptopus_backend_csharp::{Config, Generator};
    use interoptopus_backend_csharp::overloads::{DotNet, Unity};

    let config = Config {
        dll_name: "example_library".to_string(),
        namespace_mappings: NamespaceMappings::new("My.Company"),

    Generator::new(config, example_library_ffi::my_inventory())


Now run cargo test.

If anything is unclear you can find a working sample on Github.

Generated Output

The output below is what this backend might generate. Have a look at the Config struct if you want to customize something. If you really don't like how something is generated it is easy to create your own.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using My.Company;

namespace My.Company
    public static partial class InteropClass
        public const string NativeLib = "example_library";

        /// Function using the type.
        [DllImport(NativeLib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "my_function")]
        public static extern Vec2 my_function(Vec2 input);

    /// A simple type in our FFI layer.
    public partial struct Vec2
        public float x;
        public float y;