473,382 Members | 1,437 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Calling dynamically?

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
5 2446
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Danny Shevitz | last post by:
Howdy, I am trying to call class methods that have been created via a "type" function. I have enclosed a simplified example that shows what I am trying to do. In particular I am calling from the...
7
by: Julia Briggs | last post by:
Hello World - I admit I'm new to javascript but I've tried for days to find a solution to my problem. Basically I have 3 unique javascript files that do different screen display events that I...
4
by: Jerry Krinock | last post by:
I've written the following demo to help me understand a problem I'm having in a larger program. The "main" function constructs a Foo object, and then later "reconstructs" it by calling the...
2
by: Joe | last post by:
I have 3 functions: ClientInfoA is doing something ClientInfoB is doing something SelectFunction2Run is a function to determine which function needed to run based on the value of the variable...
5
by: Francesco Bochicchio | last post by:
Hi all, anybody knows if there is a (standard, portable) way to dinamically build a list of parameters to call a C function? Something like va_start & co, but to be used on the calling side? ...
8
by: Brett Robichaud | last post by:
I understand how code-behind can handle events for a page, but can I call a code-behind method from within a <script> tag in my ASP.Net page, or can I only call methods defined in other <script>...
5
by: sfeher | last post by:
Hi, Is there a way to know when a function is available for me to call it from a dynamically loaded a javascript? I use this code to load the include.js file and then I call testIncludeFn()...
15
by: =?Utf-8?B?VG9tIENvcmNvcmFu?= | last post by:
I've been led to believe by several articles, particularly Eric Gunnerson's C# Calling Code Dynamically, that calling a method dynamically through Reflection was much slower than through a...
7
by: mirandacascade | last post by:
Note: I'm not sure if the subject line of this post uses the correct terminology, so apologies if the subject line turns out to be misleading. I think it's probably easier to provide a trivial...
0
by: anishanc | last post by:
Hai dear I have a sales form that contains a button to create new item, when the item's form is displayed i just disable the sales form. My question is that how can i enable the sales form...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.