title: C/C++ Dynamic loading of functions with unknown prototype
body: <p>I'm in the process of writing a kind of runtime system/interpreter, and one of things that I need to be able to do is call c/c++ functions located in external libraries.</p>
  
  <p>On linux I'm using the <code>dlfcn.h</code> functions to open a library, and call a function located within. The problem is that, when using <code>dlsysm()</code> the function pointer returned need to be cast to an appropriate type before being called so that the function arguments and return type are know, however if I’m calling some arbitrary function in a library then obviously I will not know this prototype at compile time.</p>
  
  <p>So what I’m asking is, is there a way to call a dynamically loaded function and pass it arguments, and retrieve it’s return value <em>without</em> knowing it’s prototype?</p>
  
  <p>So far I’ve come to the conclusion there is not easy way to do this, but some workarounds that I’ve found are:</p>
  
  <ul>
  <li><p>Ensure all the functions I want to load have the same prototype, and provide some sort mechanism for these functions to retrieve parameters and return values. This is what I am doing currently.</p></li>
  <li><p>Use inline asm to push the parameters onto the stack, and to read the return value. I really want to steer clear of doing this if possible!</p></li>
  </ul>
  
  <p>If anyone has any ideas then it would be much appreciated.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>I have now found exactly what I was looking for:</p>
  
  <p><a href="http://sourceware.org/libffi/" rel="nofollow noreferrer">http://sourceware.org/libffi/</a></p>
  
  <p>"A Portable Foreign Function Interface Library"</p>
  
  <p>(Although I’ll admit I could have been clearer in the original question!)</p>