Function pointer from mangled name at runtime?
bitwise via Digitalmars-d
digitalmars-d at puremagic.com
Sat Sep 2 07:36:35 PDT 2017
On Friday, 1 September 2017 at 20:27:45 UTC, Jonathan Marler
wrote:
>
> You would also need to know type information, not just the name
> itself. If you had a name like mine.A.B.Test, then you would
> need to know what A, B, and Test are, are they modules,
> classes, structs, enums, etc
Actually found a way around this.
When `reflectFqn` below is instantiated, the fully qualified name
that was provided as the template arg gets baked into the
function name in hex. So all I have to do is figure out how to
get the mangle of reflectFqn!("some string") at runtime.
`
abstract class Reflection {
string name() const;
}
class ReflectionImpl(T) : Reflection {
override string name() const {
return T.stringof;
}
}
export extern(C) const(Reflection) reflectFqn(string fqn)()
{
mixin("alias T = " ~ fqn ~ ";");
static const(Reflection) refl = new ReflectionImpl!T;
return refl;
}
const(Reflection) reflect(T)() {
return (reflectFqn!(fullyQualifiedName!(Unqual!T)));
}
int main(string[] argv)
{
alias TFunc = const(Reflection) function();
string mangle = reflectFqn!"package.module.Test".mangleof;
auto handle = GetModuleHandleA(null);
auto getRefl= cast(TFunc)GetProcAddress(handle,
mangle.toStringz);
writeln(getRefl().name);
return 0;
}
`
More information about the Digitalmars-d
mailing list