How to manage dll with callbacks?

I'm translating a C app to Rust. The C app dynamically loads dlls, calls into them periodically, and they callback to the app. The dlls were compiled against a C include file with the necessary callback API marked as exported.

How do I do the same in Rust, where both the app and dlls are in Rust? I gather that the dll has to have a dependency on a crate that provides the API - but how can the app and the dll both depend on the same crate in different ways? The dlls should only have access to the "client" side of that API, while the app should have access to the "server" side. Or is there a way to avoid having an extra crate for the API?

The only way I can think of to do this is to not have the dlls depend on a crate for the API. Instead, have the app pass the API into the dlls as closures based on std only. But then I have to marshal info across an interface that only has std types in it, which is going to be very cumbersome.

Rust ABI is not stable, you will have to use only C types (or use one of the stable ABI crates available).

But why can't a common crate that defines the types work for you?

I did not know that Rust ABI was unstable. That does complicate things. I'll look into those stable ABI crates - thanks for those links.

Even if I had a common crate that defines the types - the point was to not require the version of Rust be exactly the same for the dlls and app.

Perhaps I should reconfigure it so that the whole thing is bundled as a single normal Rust app and rebuild it for each different configuration of (what were) dlls, instead of using command-line options to determine which dlls are loaded.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.