From: Steve Blinkhorn <steve@pr...> - 2002-09-24 12:45:49
A little while ago, I asked on the list for help getting dynamic
function calls working, parallel to the use of dlsym() under Unix.
Several people replied, some in private emails, and I extended the
question to include dynamic function calls within a .exe rather than
having a separate .dll. Thanks to their help and a bit of
suck-it-and-see, here's how to do it:
void (*pointer) ();
p = "tstfunc";
pointer = (void *) GetProcAddress(NULL, "tstfunc");
printf("address of tstfunc is %d\n", pointer);
To compile and load:
gcc -c sample.c
dlltool -e exports.o --export-all-symbols sample.o
gcc -o sample.exe sample.o exports.o
1. I work across platforms, so wherever possible I avoid using
header files that are OS specific. The cast of the result of
GetProcAddress() to void works fine.
2. For my purposes, the --export-all-symbols option to dlltool is
just what I need; for other purposes it may be better to mark exported
3. That really is all there is to it. GetProcAddress is in
libkernel32.a, and you may have to load this explicitly in some
circumstances. Although this is a very simple example, I have
complex programs with large numbers of functions (typically hundreds)
exported and accessed in this way. It is *so* simple I'm surprised
it's not better known.
Thanks once again to those who pointed me in the right direction.
Steve Blinkhorn <steve@...>