Elegant way to use dynamic bindings
Dennis
dkorpel at gmail.com
Fri Feb 9 12:15:04 UTC 2018
I want to bind to a .dll on Windows, so I looked at how Derelict
packages are doing it and found it does it like this:
```
extern(C) {
alias da_CoreGetAPIVersions = m64p_error
function(int*,int*,int*,int*);
...
}
__gshared {
da_CoreGetAPIVersions CoreGetAPIVersions;
...
}
protected override void loadSymbols() {
bindFunc(cast(void**)&CoreGetAPIVersions,"CoreGetAPIVersions");
...
```
I don't like how each function name is repeated 4 times.
Preferably, I'd write it like a static library:
```
extern(C) {
///documentation
m64p_error CoreGetAPIVersions(int*,int*,int*,int*);
}
```
And then use some reflection and mixins to make it work for
dynamic bindings.
I found the IMPLIB tool
(http://www.digitalmars.com//ctg/implib.html) and made a .lib for
the .dll and at first I got:
Error 42: Symbol Undefined _CoreGetAPIVersions
Error: linker exited with status 1
So apparently, you got to bind functions pointers, not functions.
```
extern(C) {
///documentation
m64p_error function(int*,int*,int*,int*) CoreGetAPIVersions;
}
```
But then I get:
object.Error@(0): Access Violation
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?
More information about the Digitalmars-d-learn
mailing list