Results 1 to 8 of 8

Thread: Call back functions

  1. #1
    Zaei
    Guest

    Call back functions

    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.

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Don't really see the problem.... which bit are you having trouble with?
    Harry.

    "From one thing, know ten thousand things."

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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);
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4
    Zaei
    Guest
    Nifty, Thanks much.

    Z.

  5. #5
    Zaei
    Guest
    After a romp thorugh ASM land, i figured out an easier way to do this:
    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);
    }
    Z.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Be very careful with calling conventions and passing parameters when doing that
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7
    Zaei
    Guest
    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.

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Class member functions have a hidden parameter this

    Why can't you use templates?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width