Results 1 to 20 of 20

Thread: callback in a class

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    callback in a class

    I need to make a callback for a function in a class, when i just declare the function itself, the compiler complains when i want to pass the pointer to the function.

    I know the anser has to do with a typedef (did something like this a long time ago), but I forgot how to declare the function with it....

    here's something similar to what I have
    Code:
    class myclass
    {
    private:
        void CALLBACK myCallbackFunction(long num);
        void some_other_function(void);
    }
    
    void CALLBACK myclass::myCallbackFunction(long num)
    {
    }
    
    void myclass::some_other_function(void)
    {
          // compiler gives error on next line
          // cannot convert (function prototype here) to DWORD_PTR
          
          ret = API_function((DWORD_PTR)myCallbackFunction);
    }
    Anyone knows how to make it work ?
    Last edited by CVMichael; Jan 29th, 2003 at 03:11 AM.

  2. #2
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    You have mispelled CALLBACK.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    Your right, in this code I did (I edited to CALLBACK not COLLBACK as I had before)... but it is not mispelled in my real code...

    If I did, the compiler would give me a sintax error...

    And the code I wrote there is just a sample of what i'm doing... If I paste my real class, it will be too much code to figure out the problem...
    Last edited by CVMichael; Jan 29th, 2003 at 02:54 AM.

  4. #4
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396

    Re: callback in a class

    How about trying this
    Code:
    class myclass
    {
    private:
        void CALLBACK myCallbackFunction(long num);
        void some_other_function(void);
    }
    
    void CALLBACK myclass::myCallbackFunction(long num)
    {
    }
    
    void myclass::some_other_function(void)
    {
          // compiler gives error on next line
          // cannot convert (function prototype here) to DWORD_PTR
          
          ret = API_function((DWORD_PTR)myclass::myCallbackFunction);
    }

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    Now I get 2 errors at the same line...

    error C2440: 'type cast' : cannot convert from 'void (__stdcall myclass::* )(long)' to 'DWORD_PTR'

  6. #6
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    What's that DWORD_PTR doing there?
    Code:
    ret = API_function((DWORD_PTR)myCallbackFunction);
    All pointers whether it is pointing to char or short or integer are all 32 bit. Pointer is a special type.

    What you are doing, is casting the function to a dword pointer?

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    This is the API function i'm trying to use:

    MMRESULT acmStreamOpen(
    LPHACMSTREAM phas,
    HACMDRIVER had,
    LPWAVEFORMATEX pwfxSrc,
    LPWAVEFORMATEX pwfxDst,
    LPWAVEFILTER pwfltr,
    DWORD_PTR dwCallback,
    DWORD_PTR dwInstance,
    DWORD fdwOpen
    );


    If you look at dwCallback parameter, you'll see it's a DWORD_PTR

  8. #8
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Originally posted by CVMichael
    Now I get 2 errors at the same line...

    error C2440: 'type cast' : cannot convert from 'void (__stdcall myclass::* )(long)' to 'DWORD_PTR'
    What's the other error?

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    I made a mistake there, I actually got one error... (the one I pasted)

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    But how to you do a callback in a class in general ?

  11. #11
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Then I do not know what the error may be, though everything seems to be alright.

  12. #12
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Originally posted by CVMichael
    But how to you do a callback in a class in general ?
    The class method will have to be static.

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    I made my callback static, now I get this error...

    error C2352: 'CACMClass::StreamEvent' : illegal call of non-static member function

    I'm trying to call a method in the class from the callback function, how do I do it ?

  14. #14
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    From MSDN

    Compiler Error C2352
    'class::function' : illegal call of non-static member function

    The specified nonstatic member function was called in a static member function.

    The following is an example of this error:

    class X
    {
    public:
    static void func1();
    void func2();
    static void func3()
    {
    func1(); // OK, calls static func1
    func2(); // error, calls nonstatic func2
    }
    };

    ==========================================
    Your myCallbackFunction() uses non-static members, so we have to try the this pointer approach.

    typedef (myclass::*PFN)();

    PFN pfn=&myclass::myCallbackFunction;
    ........

    API_function((DWORD_PTR)this->*pfn);

  15. #15

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    hehe, I was doing just tht right now... but I get a warning:

    warning C4312: 'type cast' : conversion from 'DWORD' to 'CACMClass *' of greater size

    the api has a parameter to pass a value to the callback function, it's DWORD dwInstance

    As I know DWORD is unsigned long, and it should be enough to pass a class pointer right ?

    Then why is the warning " ... of greater size" ?

  16. #16
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Originally posted by CVMichael
    hehe, I was doing just tht right now... but I get a warning:

    warning C4312: 'type cast' : conversion from 'DWORD' to 'CACMClass *' of greater size

    the api has a parameter to pass a value to the callback function, it's DWORD dwInstance

    1)As I know DWORD is unsigned long, and it should be enough to pass a class pointer right ?

    Then why is the warning " ... of greater size" ?
    1) Yes

    2) Dunno, but try this APIFunction((DWORD)this);

    I admit your case is special that your callback is going to be used in the class itself and I had never used acm multimedia functions myself.

    Excuse me, all this while I am busy chatting online with a girl, I don't think I can help you, maybe you would want to search the C++ forum.

  17. #17

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    Thanks for you help...

    I think it should work now, although I cannot test it yet, cuz the rest of the code is not done...

  18. #18
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Remember to cast it back before using it

    Code:
    myclass* ptr = (myclass*) fdwOpen;//I assume fdwOpen contains this 
    ptr->mymemberfunction();
    Really have to go now.

  19. #19
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    It just came to me that you need not pass the class pointer to the API Function because the Callback function which gets it, is a member function of the class itself. So just pass a NULL.

    So the casting of the previous post is not needed as well.

    Only if your Callback function is 'external' to the class, then you need to pass the class pointer.

  20. #20
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Search the forum for callback and class, this has already been answered a few times.
    The reason for the warning "of greater size" is due to a compiler setting. DWORD is unsigned long, a 32-bit value. A pointer is 32-bit too, but only for now. Soon there will be 64-bit systems where pointers are 64 bits long. But DWORD will still be 32-bit. Therefore VC++ warns you about this 64-bit portability problem.
    DWORD_PTR however is an unsigned value which is the same size as a pointer.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

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