![]() |
Storing an array of function calls, and their params.....
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 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; 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 |
Re: Storing an array of function calls, and their params.....
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... |
Re: Storing an array of function calls, and their params.....
Quote:
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! :D |
Re: Storing an array of function calls, and their params.....
thinking in terms of memory occupation helps a lot. You have:
Code:
typedef struct 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! |
Re: Storing an array of function calls, and their params.....
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. |
Re: Storing an array of function calls, and their params.....
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); Code:
callerFunc->fooFunc = MyCoolFunc; Code:
callerFunc->fooFunc(callerFunc->param1, callerFunc->param2); Thanks again for the help! |
Re: Storing an array of function calls, and their params.....
bye bye portability
|
Re: Storing an array of function calls, and their params.....
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. |
Re: Storing an array of function calls, and their params.....
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. |
Re: Storing an array of function calls, and their params.....
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 |
Re: Storing an array of function calls, and their params.....
Because, normal C++ is only fun when you do crazy things that either won't work or are a miracle when they do.
The crazy idea I had was to dynamically make a function that would call the other function with the appropriate parameters. Like... push ( b ) push ( a ) call ( MyCoolFunc ) pop ( eax ) pop ( eax ) ret Then set that little function as your function pointer, though since I don't know that much about assembly it's a little harder for me to get this working. Almost done though. |
Re: Storing an array of function calls, and their params.....
yep, I know such stuff ... jump to subroutines with the normal jump, to save a following 'return from subroutine' etc ... but normally I rather try to keep assembly and C/C++ separated. this had already positive effects when I had to ran a program on a non x86 machine at university :-)
|
Re: Storing an array of function calls, and their params.....
I'm coding for a game thats written in straight C, so virtual functions aren't available to me (easily anyway).
I'm creating a script system that is event based. The user can tell the game a bunch of different commands to execute when a certain event happens. What I was looking to do, is parse the file, then save off the commands that are linked to each event happening in a stack like the one we're talking about. Then, when the event happens, I can quickly execute the commands linked to that event, without reparsing anything, or working with strings. It seemed the quickest way to do what I needed to do. If theres a better way, I'd be happy to hear about it, I'm always open to learning more, especially if its faster, or more efficient. |
Re: Storing an array of function calls, and their params.....
There is probably a way to make the compiler do all the work but I haven't found it yet.
Not fun writing a function byte by byte though, calls fine now, just won't pop the stuff off the stack lol. *goes insane* |
Re: Storing an array of function calls, and their params.....
WARNING: Disgusting, hacked up code!
Code:
#define WIN32_LEAN_AND_MEAN |
Re: Storing an array of function calls, and their params.....
What Maleficus is doing right now IS portable. As long as all the functions have identical arguments, storing them in the struct is perfectly doable.
Code:
typedef struct Code:
// instancing If you want to call funcs that have variable types of arguments instead, you can do something like this: Code:
typedef struct Code:
// instancing |
Re: Storing an array of function calls, and their params.....
Quote:
Thanks for the help guys! :D |
All times are GMT +2. The time now is 04:49. |
Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.