0

I'm building a stack-based virtual machine for fun. I'm trying to implement an instruction to call C library functions. I have the name of the function as a C-string, the arguments to be passed, and the number of arguments. I'd also like to call variadic functions such as printf. If this cannot be done in standard C, platform specific solution is also welcome, either for Windows or for POSIX systems.

I looked at dlsym and GetProcAddress, but still the problem is that the argument list should be known at compile time, just as a normal C function. I need a way to dynamically construct the argument list and call the function with it.

  • 1
    Please don't re-post [deleted questions](https://stackoverflow.com/questions/31099896/how-to-call-a-c-library-function-dynamically-by-name). It will bring people up against you and your question. If you want to improve your closed question, use the edit function and wait for it to be re-opened. – 5gon12eder Jun 28 '15 at 13:06
  • 1
    Also: http://stackoverflow.com/questions/280940/calling-a-c-function-with-a-varargs-argument-dynamically – Mat Jun 28 '15 at 13:09
  • 2
    In general, it cannot be done in standard C, you have to grab the documentation for the calling convention (spoiler: on x86 there are many!) of your platform and build the stack/set the registers/cleanup "manually" (although you can use libffi and let it deal with all this madness). – Matteo Italia Jun 28 '15 at 13:10

0 Answers0