Function pointer from mangled name at runtime?

bitwise via Digitalmars-d digitalmars-d at puremagic.com
Fri Sep 1 13:38:49 PDT 2017


On Friday, 1 September 2017 at 20:22:21 UTC, Adam D. Ruppe wrote:
> On Friday, 1 September 2017 at 20:17:54 UTC, bitwise wrote:
>> So I'm thinking now..is this as easy as just figuring out how 
>> it's mangled and calling dlsym()?
>
> Yeah, that's what I was thinking. You can use the .mangleof 
> property to get the mangle

import core.sys.windows.winbase;
import std.string;

export extern(C) T foo(T)() {
     return 1234;
}

int main(string[] argv)
{
     int x = foo!int;

     auto handle = GetModuleHandleA(null);
     auto fooInt = cast(int function())GetProcAddress(handle, 
foo!int.mangleof.toStringz);
     writeln(fooInt());

     return 0;
}

Awesome.. this actually works.




More information about the Digitalmars-d mailing list