[Dlang-internal] How do I find and call a function in a module I only know during runtime?

Vladimir Panteleev via Dlang-internal dlang-internal at puremagic.com
Mon Dec 26 13:39:40 PST 2016


On Monday, 26 December 2016 at 20:19:52 UTC, Andrei Alexandrescu 
wrote:
> Yah, I'm trying this:

Your program works for me verbatim on GNU/Linux on x86_64.

A more portable approach would be to instrument the modules at 
compile-time (e.g. by iterating the modules' members and 
constructing a static array), and using it at run-time.

> void main()
> {
>     void *hndl = dlopen(null, 1);
>     hndl !is null || assert(0);
>     auto n = "runAllTests".ptr;//m.name ~ "" '\0';

Perhaps try a leading underscore for the mangled function name. 
Not sure how things are different on OS X.

>     auto p = cast(void function()) dlsym(hndl, n);

This will use the wrong calling convention. You need to cast to 
an extern(C) function pointer.



More information about the Dlang-internal mailing list