akeit0
akeit0
CC#
Created by akeit0 on 2/15/2025 in #help
How to get the direct function pointer of virtual struct method?
How to Get the Function Pointer of FooStruct.Foo? I have the following C# code:
interface IFoo
{
void Foo();
}

struct FooStruct : IFoo
{
public void Foo() { }
}
interface IFoo
{
void Foo();
}

struct FooStruct : IFoo
{
public void Foo() { }
}
To obtain the function pointer of FooStruct.Foo, I used:
var fp = typeof(FooStruct).GetMethod("Foo").MethodHandle.GetFunctionPointer();
var fp = typeof(FooStruct).GetMethod("Foo").MethodHandle.GetFunctionPointer();
However, fp does not point to the actual method implementation I need. Instead, it refers to the function pointer for the boxed version of FooStruct. For reference, the generated assembly is this:
L0000: add rcx, 8
L0004: mov rax, TargetFunctionPointer
L000e: jmp rax
L0000: add rcx, 8
L0004: mov rax, TargetFunctionPointer
L000e: jmp rax
I can retrieve TargetFunctionPointer by analyzing the JIT-compiled native code, but I’m looking for a more direct or reliable approach. Is there a better way to get the actual function pointer of FooStruct.Foo?
14 replies