|
General Programming Help others and get yourself helped here!
|
|
Member
Status: Offline
Posts: 1,054
Join Date: May 2004
Location: Planet Earth
|
Storing an array of function calls, and their params..... -
16-05-2005
Lets say I have a struct that looks somewhat like in this example (note, this is just example code, its not put here as compilable/logical code):
Code:
typedef struct
{
int foo;
int (*fooFunc) (int *param1, int *param2);
} my_func_t;
my_func_t *func_array[64];
Now, in a function in the code, I want to loop thru and fill that array with different functions and params, so that I can just call them by reference later.
i.e.
Code:
my_func_t *callerFunc;
int i = 0;
char *myChar;
while (i < 64)
{
callerFunc = func_array[i];
if ( i == 1)
callerFunc->fooFunc = MyCoolFunc(12, 13);
if (i == 2)
callerFunc->fooFunc = MyCoolFunc(44, 22);
// ETC, ETC, ETC......
i++;
}
int MyCoolFunc( int parameter1, int parameter2) {
// CODE GOES HERE
}
Now, later I want to loop thru "func_array" and just call the function that was passed to it, and its parameters.
I haven't gotten as far as calling the function pointer again tho, I'm just in the part where I save the array of functions, and get this warning:
warning C4047: '=' : 'int (__cdecl *)(int ,int )' differs in levels of indirection from 'int '
I'm sure its something simple, but its bugging me. :o
Dum Spiro Spero
|
|
|
|
|
Member
Status: Offline
Posts: 236
Join Date: Jan 2004
Location: Toronto, Ontario, Canada
|
Re: Storing an array of function calls, and their params..... -
16-05-2005
fooFunc is a function pointer and you are assigning it an integer which is what MyCoolFunc is returning.
The easiest way to do what you want is simply add iParam1, iParam2, ect... to your struct then just do something like array[ slot ].fooFunc( array[slot ].iParam1, array[ slot ].iParam2 );
Unless I missed the point...
|
|
|
|
|
Member
Status: Offline
Posts: 1,054
Join Date: May 2004
Location: Planet Earth
|
Re: Storing an array of function calls, and their params..... -
16-05-2005
Quote:
Originally Posted by Lazy
fooFunc is a function pointer and you are assigning it an integer which is what MyCoolFunc is returning.
The easiest way to do what you want is simply add iParam1, iParam2, ect... to your struct then just do something like array[ slot ].fooFunc( array[slot ].iParam1, array[ slot ].iParam2 );
Unless I missed the point...
|
No, no - thats exactly what I'm looking for, I'll try it later when I get some time.
What I don't understand is, I've had no problem in the past doing essentially the same thing, but instead of being passed 2 ints, its passed 1 sturct.
Thanks!
Dum Spiro Spero
|
|
|
|
|
Roi de France
Status: Offline
Posts: 5,049
Join Date: Nov 2003
Location: 46°43'60N 0°43'0W 0.187A
|
Re: Storing an array of function calls, and their params..... -
16-05-2005
thinking in terms of memory occupation helps a lot. You have:
Code:
typedef struct
{
int foo; // this is an integer, i.e. 4 bytes
// this is a pointer, thus a long, i.e. 4 bytes too.
int (*fooFunc) (int *param1, int *param2);
} my_func_t;
fooFunc is a pointer, which means, no matter how many trailing parameters you put on that line to the fooFunc function, its representation will be identical in every instance of the struct: 4 bytes, filled with the address of the function you will be calling.
With this:
Code:
callerFunc->fooFunc = MyCoolFunc(12, 13);
you are not storing the address of MyCoolFunc and its parameters, you are CALLING MyCoolFunc() with 12 and 13 as parameters, and storing... its return value.
That's not quite the same thing!
RACC home - Bots-United: beer, babies & bots (especially the latter)
"Learn to think by yourself, else others will do it for you."
|
|
|
|
|
Member
Status: Offline
Posts: 236
Join Date: Jan 2004
Location: Toronto, Ontario, Canada
|
Re: Storing an array of function calls, and their params..... -
16-05-2005
Actually, there might be a way to do this.
What you need to do is create a function on the fly that would push the different numbers onto the stack then call MyCoolFunc then set the function pointer to the newly created function.
All you really need to do that is the 386 datasheet ( I think thats the one ) for the instruction info then go and build your function byte by byte.
|
|
|
|
|
Member
Status: Offline
Posts: 1,054
Join Date: May 2004
Location: Planet Earth
|
Re: Storing an array of function calls, and their params..... -
16-05-2005
I did a quick test last night, and it seemed to work just fine the way you described it Lazy.
I kept everything in the scruct the same, but I added two ints that hold the parameters, and instead of doing this:
Code:
callerFunc->fooFunc = MyCoolFunc(44, 22);
I do this:
Code:
callerFunc->fooFunc = MyCoolFunc;
Which compiles fine without errors. Then, when I want to exec the function that occupies that array spot, I do this:
Code:
callerFunc->fooFunc(callerFunc->param1, callerFunc->param2);
And at least for the quick test that I did, it ran perfectly, doing exactly what I wanted it to do.
Thanks again for the help!
Dum Spiro Spero
Last edited by Maleficus; 16-05-2005 at 22:16..
|
|
|
|
|
Council Member, Author of JoeBOT
Status: Offline
Posts: 1,381
Join Date: Nov 2003
Location: Germany
|
Re: Storing an array of function calls, and their params..... -
16-05-2005
bye bye portability
|
|
|
|
|
Member
Status: Offline
Posts: 236
Join Date: Jan 2004
Location: Toronto, Ontario, Canada
|
Re: Storing an array of function calls, and their params..... -
16-05-2005
How is my suggestion not portable?
The dynamic function creation is so far windows specific but the function pointer stuff isn't.
I'll have an example soon, the call instruction is giving me problems just like jmp did when I was writing my API hooking class.
|
|
|
|
|
Member
Status: Offline
Posts: 1,054
Join Date: May 2004
Location: Planet Earth
|
Re: Storing an array of function calls, and their params..... -
16-05-2005
At the risk of getting flamed: I'm writing to windows, and TBH, I have no access to any other OS, so I'm not worried if its windows only.
As long as it works, I'm happy.
Dum Spiro Spero
|
|
|
|
|
Council Member, Author of JoeBOT
Status: Offline
Posts: 1,381
Join Date: Nov 2003
Location: Germany
|
Re: Storing an array of function calls, and their params..... -
16-05-2005
well, I guess one shouldnt directly mess with the stack, at least not when coding 'normal' c/c++ code.
@mal : there is a crossed out A, mark your texts and click on it, so the color information is discarded.
why is such stuff needed btw ? wouldnt virtual functions and polymorphism do the trick ? never ran across such problems, therefore just asking
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Powered by vBulletin® Version 3.8.2 Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
vBulletin Skin developed by: vBStyles.com
|
|