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 |
All times are GMT +2. The time now is 22:49. |
Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.