<p>I want to write a small utility to call arbitrary functions from a C shared library. User should be able to list all the exported functions similar to what objdump or nm does. I checked these utilities' source but they are intimidating. Couldn't find enough information on google, if dl library has this functionality either.</p>

<p>(Clarification edit: I don't want to just call a function which is known beforehand. I will appreciate an example fragment along your answer.)</p>

[{'answer_id': 66612, 'author': 'tonylo', 'author_id': 4071, 'author_profile': 'https://Stackoverflow.com/users/4071', 'pm_score': 2, 'selected': False, 'text': '<p>This might be near to what you\'re looking for:\n<a href="http://python.net/crew/theller/ctypes/" rel="nofollow noreferrer">http://python.net/crew/theller/ctypes/</a></p>\n'}, {'answer_id': 66660, 'author': 'Kevin', 'author_id': 6386, 'author_profile': 'https://Stackoverflow.com/users/6386', 'pm_score': 2, 'selected': False, 'text': "<p>Well, I'll speak a little bit about Windows.  The C functions exported from DLLs do not contain information about the types, names, or number of arguments -- nor do I believe you can determine what the calling convention is for a given function.</p>\n\n<p>For comparison, take a look at National Instrument's LabVIEW programming environment.  You can import functions from DLLs, but you have to manually type in the type and names of the arguments before you use a given function.  If this limitation is OK, please edit your question to reflect that.</p>\n\n<p>I don't know what is possible with *nix environments.</p>\n\n<p>EDIT: Regarding your clarification.  If you don't know what the function is ahead of time, you're pretty screwed on Windows because in general you won't be able to determine what the number and types of arguments the functions take.</p>\n"}, {'answer_id': 66665, 'author': 'Todd Gamblin', 'author_id': 9122, 'author_profile': 'https://Stackoverflow.com/users/9122', 'pm_score': 1, 'selected': False, 'text': '<p>You could try ParaDyn\'s SymtabAPI.  It lets you grab all the symbols in a shared library (or executable) and look at their types, offset, etc.  It\'s all wrapped up in a reasonably nice C++ interface and runs on a lot of platforms.  It also provides support for binary rewriting, which you could potentially use to do what you\'re talking about at runtime.</p>\n\n<p>Webpage is here:</p>\n\n<p><a href="http://www.paradyn.org/html/symtab2.1-features.html" rel="nofollow noreferrer">http://www.paradyn.org/html/symtab2.1-features.html</a></p>\n\n<p>Documentation is here:</p>\n\n<p><a href="http://ftp.cs.wisc.edu/paradyn/releases/release5.2/doc/symtabProgGuide.21.pdf" rel="nofollow noreferrer">http://ftp.cs.wisc.edu/paradyn/releases/release5.2/doc/symtabProgGuide.21.pdf</a></p>\n'}, {'answer_id': 66852, 'author': 'millenomi', 'author_id': 6061, 'author_profile': 'https://Stackoverflow.com/users/6061', 'pm_score': 1, 'selected': False, 'text': "<p>A standard-ish API is the dlopen/dlsym API; AFAIK it's implemented by GNU libc on Linux and Mac OS X's standard C library (libSystem), and it might be implemented on Windows by MinGW or other compatibility packages.</p>\n"}, {'answer_id': 67032, 'author': 'Allan Wind', 'author_id': 9706, 'author_profile': 'https://Stackoverflow.com/users/9706', 'pm_score': 0, 'selected': False, 'text': '<p>The source code for nm and objdump are available.  If you want to start from specification then ELF is what you want to look into.</p>\n\n<p>/Allan</p>\n'}, {'answer_id': 68010, 'author': 'gnkdl_gansklgna', 'author_id': 10470, 'author_profile': 'https://Stackoverflow.com/users/10470', 'pm_score': -1, 'selected': False, 'text': "<p>Eek! You've touched on one of the very platform-dependent topics of programming. On windows, you have DLLs, on linux, you have <code>ld.so</code>, <code>ld-linux.so</code>, and mac os x's <code>dyld</code>.</p>\n"}, {'answer_id': 68257, 'author': 'Adam Mitz', 'author_id': 2574, 'author_profile': 'https://Stackoverflow.com/users/2574', 'pm_score': 0, 'selected': False, 'text': "<p>I've written something like this in Perl.  On Win32 it runs dumpbin /exports, on POSIX it runs nm -gP.  Then, since it's Perl, the results are interpreted using regular expressions: / _(\\S+)@\\d+/ for Win32 (stdcall functions) and /^(\\S+) T/ for POSIX.</p>\n"}, {'answer_id': 83724, 'author': 'artificialidiot', 'author_id': 7988, 'author_profile': 'https://Stackoverflow.com/users/7988', 'pm_score': 1, 'selected': False, 'text': '<p>Only sensible solution (without reinventing the wheel) seems to use libbfd. Downsides are its documentation is scarce and it is a bit bloated for my purposes.</p>\n'}]
['https://Stackoverflow.com/questions/66544', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/7988/']