0

I would like to write a library to make it easier to write DLL plugins for a non-rust program, with a C API/ABI, but I do not see how I could achieve that.

In my poc I have functions like

#[no_mangle] (repeated each time)
extern "C" fn init(*mut *mut ctx) -> i32 { ... }
extern "C" fn do_something(*mut ctx) -> i32 { ... }
extern "C" fn cleanup(*mut ctx) -> i32 { ... }

I used a Boxed structure for my ctx variable.

I have a lot of boilerplate code in my PoC for these and would like to put all that in a library to avoid repeating it everywhere.

So basically my objective is to have a lib that lets me just define the custom context and functions for each plugin, a bit like:

use plugin_lib;

struct MyCtx {
...
}

fn my_init(pp_ctx: *mut *mut MyCtx) -> i32 { ... };
fn my_init(p_ctx: *mut MyCtx) -> i32 { ... };
fn my_init(p_ctx: *mut MyCtx) -> i32 { ... };

But if I try to create a library with missing my_ functions, it will obviously not link, right? And at the same time if I just define objects in a lib and try to use them in the plugin part I cannot make it export functions unless I write all of them again (or at least small wrappers to call the lib ones, which sounds ugly).

What would be the right way to build this? Or if there are several, how would you do it?

DaLynX
  • 328
  • 1
  • 11
  • 1
    sounds like you should create a macro that your library users can use that will make these functions for them – kmdreko Dec 05 '21 at 19:08
  • That actually looks like what I need, you're right, thank you! Can I export them in a package for distribution like I would with a lib file? (It would still be code not binary inside as I understand, but still?) – DaLynX Dec 05 '21 at 19:34
  • Once upon a time, this was how programs were written, but it caused many problems and only authors could add certified plugins. I would try to add a deno interpreter and make it possible to write plugins in a language like js, which can only access certain methods from the main system. – Grzesiek Dec 06 '21 at 06:02

0 Answers0