On Tue, 11 Nov 2003 06:34:45 GMT, "Alex Lyman" <alex lyman @
earthlink.net> wrote:
Does anyone have any code handy (or know what a good direction for me to
head in), to call functions, if you have an address of the function, its
declspec (for my app, it's limited to _stdcall and thiscall), and what
parameters it expects? I know all about the argument ordering on the stack,
but I don't really know enough ASM to work with it. Does anyone out there
know of a cheap way to do it in more standardized C++? (efficiency doesn't
matter to to the project, really, so any random musings would be helpful)
<random-musings>
If you know what parameters the function expects, there's no need to
use ASM to do it. Just call the function.
It becomes tricky when you have a *variable* number of parameters ...
not for the function, which can be declared with the C++ syntax
similar to:
void foo(int, ...);
or merely
void foo(...);
although it might be a good idea to have the count of arguments passed
as the first argument.
The problem, of course, is for the caller who might get input from,
say, an XML file which has a list of arguments, the name of the
function, and perhaps the name of the library as well. Somehow, the
function call must be constructed in order to call the function in the
library (assuming that it is in a shared library).
Also, if the caller must call various types of functions, it will need
to have a generic type (perhaps void* would work) or else need to have
other knowledge about the types of arguments passed. If they are all
of the same type, I might pass an array of pointers with the last
element a NULL pointer. Also, you need to devise a way to handle input
and output (or also in/out) arguments...
Another approach would be to implement some kind of "COM" interface,
i.e. a mechanism with a generic calling convention which allows
clients to "discover" functions and the parameters they expect.
HTH
</random-musings>
--
Bob Hairgrove
No**********@Home.com