I am in the process of writing a kind of runtime system / interpreter, and one of the things I need to do is call the c / C ++ functions located in the external libraries.
In linux, I use functions dlfcn.hto open a library and call a function located inside. The problem is that when using dlsysm(), the return function return pointer must be passed to the appropriate type before calling in order to know the function arguments and the return type, however if Im calls some arbitrary function in the library, then obviously I will not know this prototype in compilation time.
So, what I'm asking is, is there a way to call a dynamically loaded function and pass its arguments and get its return value without knowing its prototype?
So far, I have come to the conclusion that this is not an easy way to do this, but some workarounds that I have found are:
Make sure that all the functions that I want to download have the same prototype and provide a mechanism for sorting these functions to get the parameters and return values. This is what I am doing now.
Use inline asm to push parameters onto the stack and to read the return value. I really want to avoid this if possible!
If anyone has any ideas, then it will be very appreciated.
Edit:
Now I found exactly what I was looking for:
http://sourceware.org/libffi/
"Portable library of external functions interface"
( , !)