473,704 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2471
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(voi d);

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(voi d)
{
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.l earn.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**********@Ho me.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*******@mari ani.ws> wrote in message
news:bo******** @dispatch.conce ntric.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
1550
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 scripting level which is in a different namespace than the dynamically created class. I can call the method, but only by passing strings and using getattr.I find this inelegant. I would rather pass a callable object, but cannot figure out how to...
7
4519
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 want to dynamically call, where the current page would then refresh after a user clicks on any one of three associated button links. Could someone please assist me and craft a simple script that would accomplish this feat of impossibility? ...
4
7288
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 constructor again. In my larger program, I find that the member variables don't get re-initialized when "reconstructed". I don't have that problem in this demo, but the second time the constructor is called, its "this" points to a different location. ...
2
1832
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 Method2Run. If the clientType is A, it would run ClientInfoA function. If it is clientType B, it would run the ClientInfoB function. Based on the value of Method2Run, how would I run the function dynamically? I know that there are many ways not to...
5
3030
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? In other words, suppose that I have a generic pointer : void * f_ptr; I know that this pointer points to a fuction. I also know the function
8
5360
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> sections? I can't seem to figure out the syntax for for calling code-behind directly. The method is within the class my page inherits from and is public, but when I try to call it from my page I get this error: CS1520: Class, struct, or...
5
2217
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() from it: <code>
15
8204
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 Delegate. My testing showed that actually it was six times faster: 0.5 seconds for 100,000 iterations versus 3.1 seconds. Can anyone explain why? Something in the way I coded it? I'd appreciate any insights. Here's the code (in a Windows Form...
7
5523
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 example to illustrate the question rather than to try to ask the question in general terms. Assume there is a table with these records
0
947
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 while closing the item's form.? Item form is dynamically used by many other forms. Now I just setting sales class object to a static variable in the items class, and I using this sales object to enable sales form. (eg: sales class object ....
0
8677
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9266
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9130
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8974
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7879
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5926
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4435
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3130
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2468
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.