#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    6
    Rep Power
    0

    MSVC++: cast(?) a TIMERPROC function pointer...


    Hello, I'm pretty new to Windows programming, so bare with me... and help me out please :)

    I'm trying to implement an easy-to-use timer-repeater class in MS Visual C++ 6.0.
    I've figured out how to do the functionality, code follows:

    PHP Code:
    // ---- globals: ----
    void doSomething();
    int iTimerID;
    void (*pfCallFunction)(void);

    //------------------------------------------------------------------------
    VOID CALLBACK ownTimerProc(HWND hWndUINT uMsgUINT idTimerDWORD dwTime) {
        
    pfCallFunction();    // Effectively calls doSomething()
        
    return;                // We don't care about those parameters.
    }

    //------------------------------------------------------------------------

    void doSomething() {
        
    // Code that has to be performed 50 times a second.
        
    ...
        return;
    }
    //------------------------------------------------------------------------
    void initStuff() {

        
    // Set the pointer of the function to call repeatedly into pfCallFunction.
        
    pfCallFunction doSomething;

        
    // Set a timer to jump to ownTimerProc() every 20 milliseconds:
        
    iTimerID SetTimer(NULLNULL20, (TIMERPROC)ownTimerProc );    

        
    // Now the function doSomething() gets called 50 times a second.

        // Do things...
        
    ...
        
        
    KillTimer(NULLiTimerID);    // When finished, kill timer.
        // ... kill window, end program.
    //------------------------------------------------------------------------ 
    Remember, the point was to make an easy-to-use class out of it,
    that's why we don't write the code of doSomething() in ownTimerProc() itself.

    So far, so good ... but I want to make a timerRepeater class.

    However, now comes the problem - it might be trivial, but I'm not that
    good with all these function pointers and stuff...

    Here's the relevant parts of my class.h :

    PHP Code:
    private:
        
    void (*fpCallFunction)(void);

    // constructor:
    ownTimerRepeater::ownTimerRepeater(int millisecondsvoid* function) {
        
    iTimerID SetTimer(NULLNULLmilliseconds, (TIMERPROC)ownTimerProc );      // *** ERROR ***
        
    fpCallFunction = function;
        
    active true;
    }
        
    //and the class has a public method:
    VOID CALLBACK ownTimerRepeater::ownTimerProc(HWND hWndUINT uMsgUINT idTimerDWORD dwTime) {
        if (
    active)    {
            
    fpCallFunction();
        }
        return;    

    The error I get is: (on line marked with *** ERROR ***)

    "error C2440: 'type cast' : cannot convert from '' to 'void (__stdcall *)(struct HWND__ *, unsigned int, unsigned int, unsigned long)'
    None of the functions with this name in scope match the target type"

    It works fine if I am not trying to make a class out of the whole thingy.
    How do I set this up so that it compiles ?

    Also, I'm not sure about if that second parameter of the constructor "void* function" is correct,
    it is to transfer the function pointer to the private member variable void (*fpCallFunction)(void)
    to later use in ownTimerProc... but I haven't gotten that far, first the TIMERPROC cast thingy must be solved...

    Any help appreciated, thank you.

    Andru

    edit: smilies disable and typos
    Last edited by Andru; July 11th, 2003 at 09:59 AM.
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    6
    Rep Power
    0
    No-one could help me out ?
    Last edited by Andru; July 13th, 2003 at 01:27 PM.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    6
    Rep Power
    0
    Anyone... ? Please :-)
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    6
    Rep Power
    0
    Could someone please help me out ?

    How do I write/cast this *member* function pointer in order to get it to work ?

    It works if the ownTimerProc is not a member function of a class... but I want to make it a member function.

    Thanks for any help,

    Andru
  8. #5
  9. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,645
    Rep Power
    4248
    Declare the member function as static. i.e.
    Code:
    static VOID CALLBACK ownTimerRepeater::ownTimerProc(.... rest of stuff)
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    6
    Rep Power
    0
    Thanks Scorp,

    I actually tried that... but there's a problem now:

    static member functions cannot access private nor public data members (that's how I understood it).

    The static ownTimerProc needs to call a function I give as parameter in the constructor. But as it cannot access the data members, I can't think of how to get it there, any suggestions ?
  12. #7
  13. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,645
    Rep Power
    4248
    Last edited by Scorpions4ever; July 24th, 2003 at 05:21 PM.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    6
    Rep Power
    0
    Thanks a lot Scorp, I'll take a look at those :)

IMN logo majestic logo threadwatch logo seochat tools logo