Dynamically resolve stdlib C functions from .NET.
I want to resolve the addresses of functions such as from stdlib C, for example malloc
at runtime from .NET code (so that I can use JIT code that calls these addresses for my VM). I think that I should use LoadLibrary
and GetProcAddress
for delivery kernel32.dll, but it 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, it looks like it got the DLL handle, but trying to resolve malloc
returned a NULL pointer.
How should I do it?
source to share
AFAIK malloc
NOT part of kernel32.dll
. It is part of the MS C Runtime DLLs as you did not provide any details on how / where you want to use it just some links with related information:
- http://msdn.microsoft.com/en-us/library/abx4dbyh.aspx
- http://www.codeproject.com/Articles/20248/A-Short-Story-about-VC-CRT
Basically you have to load the appropriate DLL and then enable the desired function as an example:
let SomeMSCRT = LoadLibrary @"SomeMSCRT.dll";; let malloc = GetProcAddress(SomeMSCRT, "malloc");;
One note:
Depending on what exactly you want to do, you may run into serious and hard to debug problems - for example using malloc
from F # - this is IMHO, a bad idea ... you need to keep in mind that the C runtime needs to initialize correctly (in case of MS CRT you need to call CRT_INIT
as the very first) etc. The C runtime is NOT aware of any .NET specificity and therefore can lead to unstable behavior ... esp. as the CLR can use CRT (possibly a different version) internally ...
One more point:
Generating machine code at runtime and executing it by navigating to this region of memory is subject to several security measures (depending on the Windows version, etc.). You might need to mark the corresponding memory segment as "executable" which is NOT possible through C runtime, this is only possible when using Windows API calls (for example VirtualAllocEx
) with the appropriate permissions ... see starting point here and here .
source to share
I want to resolve the addresses of functions like C stdlib
There is your problem. You speak as if there is one C time library. In fact, there are many. Each compiler provides its own, in several different flavors (debug, release, static link, dynamic link), and they are all mutually incompatible.
If you knew which one you are trying to use, the answer to your question will become obvious.
If you want OS supported libraries to be available for all Windows applications, then kernel32.dll
this is the right place to look, but the functions are in accordance with the Win32 API specification, not the ANSI or ISO C standard.
source to share