[Dlang-internal] How do I find and call a function in a module I only know during runtime?
Andrei Alexandrescu via Dlang-internal
dlang-internal at puremagic.com
Mon Dec 26 12:19:52 PST 2016
On 12/26/2016 03:01 PM, Jacob Carlborg wrote:
> On 2016-12-26 19:45, Andrei Alexandrescu wrote:
>> Say I have a module "a" that wants to call a function "void fun()" in
>> another module "b". The name "b" is not known until runtime, i.e. it's
>> read from the console. (Really the name is obtained by iterating foreach
>> (m; ModuleInfo)).
>>
>> How do I get fun()'s address (or the null pointer if it doesn't exist)?
>
> Perhaps use "dlopen" on the executable and then use "dlsym" and pass in
> the mangled name.
Yah, I'm trying this:
void main()
{
void *hndl = dlopen(null, 1);
hndl !is null || assert(0);
auto n = "runAllTests".ptr;//m.name ~ "" '\0';
auto p = cast(void function()) dlsym(hndl, n);
if (!p) assert(0);
p();
}
extern(C) void* dlopen(const(char)*, int);
extern(C) void* dlsym(void*, const(char)*);
export extern(C) void runAllTests() {}
dlopen passes, dlsym fails.
Andrei
More information about the Dlang-internal
mailing list