Elegant way to use dynamic bindings

Mike Parker aldacron at gmail.com
Fri Feb 9 12:50:49 UTC 2018


On Friday, 9 February 2018 at 12:15:04 UTC, Dennis wrote:

>
> I presume the .dll isn't loaded properly (if at all), but I 
> can't find a load function in the .lib and don't know how to 
> debug this. So I guess I'll just do it manually since that 
> works, but does anyone have some tips to make .dll bindings 
> elegantly?

I suggest you read this first:

http://derelictorg.github.io/bindings/

It explains the difference between dynamic bindings and static 
bindings. The important thing to understand is that both types 
can be used with DLLs, but only static bindings can be use with 
static libraries.

Dynamic bindings have no link time dependency on the DLL and the 
DLL must be loaded manually. Static bindings have a link time 
dependency and can be linked with either a static library, an 
import library (on Windows -- what you get when you run implib or 
compile a DLL yourself), or directly with a shared object (.so) 
on non-Windows systems (and probably .dylib/Frameworks on Mac).

Dynamic bindings use function pointer because that's the only way 
to do manuall loading. For static bindings, you use normal 
function declarations and the OS will load the DLL automatically 
when the app starts up (dynamic loading) if you've linked to an 
import library or .so, and, of course, will load nothing if 
you've linked to a static library.

Some Derelict packages, like DerelictSDL2 and DerelictGLFW3, 
support both dynamic and static binding. For the former, the 
function pointer declarations and the loader are all in [1]. For 
the latter, there is no loader and the declarations are all in 
[2].

[1] 
https://github.com/DerelictOrg/DerelictGLFW3/blob/master/source/derelict/glfw3/dynload.d

[2] 
https://github.com/DerelictOrg/DerelictGLFW3/blob/master/source/derelict/glfw3/statfun.d

So if you don't need to load manually and don't mind the 
link-time dependency on the C library, then you can use normal 
function declarations in your binding:

extern(C) @nogc nothrow {
     void foo();
}



More information about the Digitalmars-d-learn mailing list