Use .dll in Elixir

I need to access the functions of a dll.
Is this possible in Elixir?

Yes and no.

It will require you a lot of work writing glue-code in C (or Rust using rustler) yourself.

Since small mistakes in the implementation of the gluecode, errors in the glued shared object (or however your OS names them) can bring your complete BEAM-VM down.

Also calling into foreign code and returning the result is not allowed to take longer than a single millisecond to not confuse BEAMs scheduler. There is dirty scheduling available if you compiled erlang yourself and set the correct compileflags, according to rumors dirty scheduling shall be the default in the next major OTP release (mid '17).

If you still want to do this, do a google for “elixir NIF”

Another possibility is to use a C-Port, which would require you to write glue-code as well, but the danger to bring down the VM isn’t that high, also the scheduling problem isn’t in the way anymore. But then you do have the gluecode running in a dedicated BEAM-process which you have to talk to via message passing.

And the last option you have is to create a wrapper CLI tool, which does use the shared object, which you then use as a Port (note the missing C). It is started and owned by a dedicated BEAM-process but then runs outside of the VM. The owning process can send and receive messages from that port. Those messages are strings only (plus some Port-metadata), it would be the same as if you start that CLI-tool by hand and then hack some commands directly into your terminal and read the response but having that terminal managed and hidden by BEAM.


Thank you!

I’ll use other technology on that project then.

You’re assuming he’s dealing with a native DLL. It could be a DotNet assembly too.

1 Like

Oh damn, I often forget that… It’s been a while that I used something related to CLR.

For those I have to assume that only the CLI/Port will do.

It’s a native dll.

What does it do and what do you need it for?

It serves to comply with Brazilian legislation.
It serves to communicate with a fiscal apparatus called SAT.

I have a python application that already addresses this need.
But I wanted to create an easier version to distribute.

If those calls to it cannot fail and they respond quickly then it would be easy to integrate as a NIF, but even using it via a PORT would be very easy (could even talk to your existing python thing if you wanted). :slight_smile:

Would you have any examples that would make this integration as a NIF?
Using some dll that I can test.

I like using Rustler and the Rustler docs has a good example project, so you can call your DLL from rust then (hmm, I’ve an idea for a generic FFI, perhaps via port… maybe someday). If you want to do it from C then there are tons of examples around for that, the ‘ranch’ project that is a dependency of Phoenix (via cowboy I think?) has a nif built in so you can look at that for one of many examples. :slight_smile:

I’m going to study Rustler then.
Thank you!

How will this be easier with the introduction of Dirty Schedulers?

Noone said it will be easier, only that you do not have to fullfil that 1ms restriction for NIFs.

1 Like

You are right and that got me googling, Steve Vinoski’s talk cleared things up.