PDA

Click to See Complete Forum and Search --> : Call back functions


Zaei
May 4th, 2001, 04:25 PM
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.

HarryW
May 4th, 2001, 04:28 PM
Don't really see the problem.... which bit are you having trouble with?

parksie
May 4th, 2001, 04:41 PM
typedef long (*PFNMYFUNCTION)(int, int);

PFNMYFUNCTION pfnTheFunction;

long afunction(int x, int y) {
return x + y;
}

long func() {
pfnTheFunction = afunction;

return (pfnTheFunction)(5, 200);
}

Zaei
May 4th, 2001, 07:29 PM
Nifty, Thanks much.

Z.

Zaei
May 8th, 2001, 11:57 AM
After a romp thorugh ASM land, i figured out an easier way to do this:

int somefunction()
{
return 10;
}

int callafunc(void* addr)
{
int retval;
__asm call [addr];
__asm mov retval, eax;
return retval;
}

int main()
{
return callafunc(somefunction);
}


Z.

parksie
May 8th, 2001, 12:07 PM
Be very careful with calling conventions and passing parameters when doing that ;)

Zaei
May 8th, 2001, 12:15 PM
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.

parksie
May 8th, 2001, 12:21 PM
Class member functions have a hidden parameter this ;)

Why can't you use templates?