How would I go about creating a callback function. For example, I have function a that takes in a function pointer, and stores it as static, and when i call it again, it would call the function it had stored. How would this be done?
Z.
Printable View
How would I go about creating a callback function. For example, I have function a that takes in a function pointer, and stores it as static, and when i call it again, it would call the function it had stored. How would this be done?
Z.
Don't really see the problem.... which bit are you having trouble with?
Code:typedef long (*PFNMYFUNCTION)(int, int);
PFNMYFUNCTION pfnTheFunction;
long afunction(int x, int y) {
return x + y;
}
long func() {
pfnTheFunction = afunction;
return (pfnTheFunction)(5, 200);
}
Nifty, Thanks much.
Z.
After a romp thorugh ASM land, i figured out an easier way to do this:
Z.Code:int somefunction()
{
return 10;
}
int callafunc(void* addr)
{
int retval;
__asm call [addr];
__asm mov retval, eax;
return retval;
}
int main()
{
return callafunc(somefunction);
}
Be very careful with calling conventions and passing parameters when doing that ;)
Quite, but its much more generic. What i was needing it for was to create a simple menu class, and, instead of having to have a million different classes for each menu item type, just be able to create a single class that would call the function address you passed it. No parameters (could be easily set with the class, if i needed them), and no return value (which would be simple to implement anyway =).
Z.
Class member functions have a hidden parameter this ;)
Why can't you use templates?