Comment #0 by alvaro.segura — 2011-03-27T14:46:07Z
Runtime.loadLibrary()/unloadLibrary() are very handy to load DLLs in a platform independent way, without using directly the platform's LoadLibrary()/UnloadLibrary() or dlopen()/dlclose().
But these two are missing a third very important function: a function to load the address of a given symbol in the shared library. E.g. the equivalent GetProcAddress() in Windows or dlsym() in Linux.
To keep the current simplicity in Phobos, where the library is just referenced by a void*, the new functions could be:
void* Runtime.getLibrarySymbol(void* libhandle, string symbol);
Libraries such as gtkD are currently doing a lot of dynamic loading and use platform specific functions with version(Windows){...}else{...} conditions. That could be avoided and simplified with the above.
--
Anyway I'd favor a little DynLibrary class allowing:
auto lib = new DynLibrary("mylib"); // ".dll" or ".so" autoappended
Function f = lib["main_function"];
BTW, autoappending the extension in Runtime.loadLibrary() would be great to ease program portability. Not sure if it's already like that.
--
Also (this could be a different request report) it would be great to have an additional function in dynamic libraries: a "factory" function to create objects from classes defined inside the DLL (great for plugins for example).
There is already Object.factory("module.Class"); which works great but only locally. Can't that be supported for loaded libraries?
either with a new call:
Runtime.libraryFactory(void* libhandle, string classname);
or
lib.factory("class");
or just making the library, in its init code, add its classes to the calling runtime so that Object.factory("mydll.Classname") does it.