I want to resolve the addresses of functions like those from the C stdlib such as malloc
at run-time from .NET code (so I can JIT machine code that calls to these addresses for my VM). I believe I should use LoadLibrary
and GetProcAddress
supplying the kernel32.dll but this does not work. Using F# interactive I get:
> [<DllImport("kernel32.dll", CharSet=CharSet.Ansi, SetLastError=true)>]
extern IntPtr LoadLibrary(string fileName);;
val LoadLibrary : string -> IntPtr
> [<DllImport("kernel32.dll", CharSet=CharSet.Ansi, SetLastError=true)>]
extern uint32 GetProcAddress(IntPtr hModule, string fn);;
val GetProcAddress : IntPtr * string -> uint32
> let kernel32 = LoadLibrary @"kernel32.dll";;
val kernel32 : IntPtr = 1993146368n
> let malloc = GetProcAddress(kernel32, "malloc");;
val malloc : uint32 = 0u
So this appears to have obtained a handle to the DLL but trying to resolve malloc
has returned a NULL pointer.
How should I do this?