By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 1,896 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

Calling dynamically?

P: n/a
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)
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.536 / Virus Database: 331 - Release Date: 11/3/2003

Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Alex Lyman 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)


Look up "function pointers" or "pointers to functions" in your favorite
C++ reference and in the C++ FAQ below. A function pointer is very
similar to an address of a function, execpt that it _may_ have more
information associated with it.

#include <iostream>
#include <cstdlib>
using namespace std;

typedef void (*P_VOID_FUNC)(void); // function pointer type.

void hello(void);
void goodbye(void);
void like_my_hat(void);

P_VOID_FUNC func_tbl[] =
{
hello, like_my_hat, hello, goodbye
};
const unsigned int NUM_FUNCS =
sizeof(func_tbl) / sizeof(func_tbl[0]);

int main(void)
{
for (unsigned i = 0; i < NUM_FUNCS; ++i)
{
(*func_tbl[i])(); // Call via function pointer.
}
return EXIT_SUCCESS;
}

void hello(void)
{
cout << "Hello.\n";
return;
}

void like_my_hat(void)
{
cout << "Do you like my hat?\n";
return;
}

void goodbye(void)
{
cout << "Goodbye.\n";
return;
}

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 19 '05 #2

P: n/a
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
Jul 19 '05 #3

P: n/a
Perhaps I should describe my project in more detail:

I want to open alot of standard C/C++ code up to be run from the console
(for now -- a virtual machine is the ultimate project goal), so I need to
have some way of associating text, with a function pointer and an actual
description of the function (callspec, arguments, return type, and [for
methods] class). I've gotten to the point where all of the data-collection
part is working 100%; and I can parse input statements and search for the
record of the function. All I have left is actually calling a function.

Thanks for the tip of libffi, Gianni Mariani. However, I forgot to mention
that my target platform is 16-bit DOS, and, as of yet, there isn't a libffi
implimentation for it. Sure it will probably come in handy in the future,
though, if I ever need to do something similar in VC++ or GCC. Might even
figure out what I need to know from its source, maybe.

Anyways, thanks for all the help y'all :)

- Alex
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.536 / Virus Database: 331 - Release Date: 11/3/2003

Jul 19 '05 #4

P: n/a
Alex Lyman wrote:
Perhaps I should describe my project in more detail:

I want to open alot of standard C/C++ code up to be run from the console
(for now -- a virtual machine is the ultimate project goal), so I need to
have some way of associating text, with a function pointer and an actual
description of the function (callspec, arguments, return type, and [for
methods] class). I've gotten to the point where all of the data-collection
part is working 100%; and I can parse input statements and search for the
record of the function. All I have left is actually calling a function.

Thanks for the tip of libffi, Gianni Mariani. However, I forgot to mention
that my target platform is 16-bit DOS, and, as of yet, there isn't a libffi
implimentation for it. Sure it will probably come in handy in the future,
though, if I ever need to do something similar in VC++ or GCC. Might even
figure out what I need to know from its source, maybe.


The concept is really very simple. You have an array that describes the
args, you call an ASM routine that sets up a stack frame, and calls
another C function to "fill in" the stack frame, when it returns the asm
loads whatever registers need to be filled in and it calls the desired
function, upon return the return value is stuffed in the right place.

Jul 19 '05 #5

P: n/a
Well, after lots of trial-and-error and digging through (and trying to
comprehend) libffi, I figured some ASM code (definitly pushed my ASM
know-how on this one) for __cdecl-type function calls -- so it shouldn't be
too hard to add support for _stdcall and thiscall functions in the near
future. Thanks, guys for all the help and direction-pointing!

- Alex

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bo********@dispatch.concentric.net...
Alex Lyman wrote:
Perhaps I should describe my project in more detail:

I want to open alot of standard C/C++ code up to be run from the console
(for now -- a virtual machine is the ultimate project goal), so I need to have some way of associating text, with a function pointer and an actual
description of the function (callspec, arguments, return type, and [for
methods] class). I've gotten to the point where all of the data-collection part is working 100%; and I can parse input statements and search for the record of the function. All I have left is actually calling a function.

Thanks for the tip of libffi, Gianni Mariani. However, I forgot to mention that my target platform is 16-bit DOS, and, as of yet, there isn't a libffi implimentation for it. Sure it will probably come in handy in the future, though, if I ever need to do something similar in VC++ or GCC. Might even figure out what I need to know from its source, maybe.


The concept is really very simple. You have an array that describes the
args, you call an ASM routine that sets up a stack frame, and calls
another C function to "fill in" the stack frame, when it returns the asm
loads whatever registers need to be filled in and it calls the desired
function, upon return the return value is stuffed in the right place.

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.536 / Virus Database: 331 - Release Date: 11/3/2003
Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.