CALLBACK Functions as Class Members 
Author Message
 CALLBACK Functions as Class Members

I have a class:

class CFoo
{
private:
    m_data1;
    m_data2;
public:
    HRESULT         CallOutsideFunc(HWND hWnd);
    HRESULT         (WINAPI *MyCallBackFunc)(buncha params));

Quote:
}

HRESULT CFoo::CallOutsideFunc(HWND hWnd)
{
    HRESULT result;
    result = OutsideFunc(MyCallBackFunc,(LPVOID)m_data1);
    return result;

Quote:
}

HRESULT CFoo::*MyCallBackFunc(buncha params)
{
    do a buncha stuff...;
    return something;

Quote:
}

This doesn't work. If I declare it this way I can access the data passed
into MyCallBackFunc but not the Class Member data of the instance that
passed it. I could also pass in a pointer to this but  OutsideFunc is
expectign a certain format and I don't have access to the code to modify it
to accept my this pointer.
My Question:
Is there a way to have a CALLBACK function in a class that is called from
outside the class?

Thanks,

Christopher White
Software Engineer
Packard Bell NEC

http://www.*-*-*.com/



Sun, 17 Dec 2000 03:00:00 GMT  
 CALLBACK Functions as Class Members

If your callback is not being called through a "pointer-to-member", then the
answer is no.  You'll need to use a static member function.  According to your
code, your callback receives a bunch of params.  If not your "this" pointer,
might it receive ANYTHING which you could use to determine which CFoo instance
should process the callback?

HTH

Quote:

> I have a class:

> class CFoo
> {
> private:
>     m_data1;
>     m_data2;
> public:
>     HRESULT         CallOutsideFunc(HWND hWnd);
>     HRESULT         (WINAPI *MyCallBackFunc)(buncha params));
> }

> HRESULT CFoo::CallOutsideFunc(HWND hWnd)
> {
>     HRESULT result;
>     result = OutsideFunc(MyCallBackFunc,(LPVOID)m_data1);
>     return result;
> }

> HRESULT CFoo::*MyCallBackFunc(buncha params)
> {
>     do a buncha stuff...;
>     return something;
> }

> This doesn't work. If I declare it this way I can access the data passed
> into MyCallBackFunc but not the Class Member data of the instance that
> passed it. I could also pass in a pointer to this but  OutsideFunc is
> expectign a certain format and I don't have access to the code to modify it
> to accept my this pointer.
> My Question:
> Is there a way to have a CALLBACK function in a class that is called from
> outside the class?

> Thanks,

> Christopher White
> Software Engineer
> Packard Bell NEC

> http://www.neccsd.com

-- Aaron [MVP]
---------------------
Aaron J Margosis

Fortress-NT - NT Workstation/Server Security Utility:
    http://www.sunbelt-software.com/fortress.htm
ScrnSaveSwitch/Plus - Screen Saver Control Utility:
    http://www.ssswitch.com


Mon, 18 Dec 2000 03:00:00 GMT  
 CALLBACK Functions as Class Members

I'm guessing, but do you not want...

Quote:
> class CFoo
> {
> private:
>     m_data1;
>     m_data2;
> public:
>     HRESULT         CallOutsideFunc(HWND hWnd);
>     static HRESULT (WINAPI *MyCallBackFunc)(CFoo*, buncha params));
>     HRESULT         DoCallBackFunc(buncha params));
> }

> HRESULT CFoo::CallOutsideFunc(HWND hWnd)
> {
>     HRESULT result;
>     result = OutsideFunc(&MyCallBackFunc,(LPVOID)m_data1);
>     return result;
> }

> HRESULT CFoo::MyCallBackFunc(CFoo* This, buncha params)
> {
>     return This->DoCallBack(buncha params);
> }

> HRESULT CFoo::DoCallBackFunc(buncha params)
> {
>     // Do a buncha stuff;
>     return something;
> }

This lets you access the member data of the instance that invoked the
callback. The static MyCallBackFunc is just a helper to overcome the
problem that the callback should be a non-member function.
--
Ken Hagan


Tue, 19 Dec 2000 03:00:00 GMT  
 CALLBACK Functions as Class Members

The typical way to do it would be:

class CFoo
{
public:
        static void RawCallback( int nFoo, LONG lUserData );
        {
                CFoo* pFoo = reinterpret_cast<CFoo*>( lUserData );
                pFoo->Callback( nFoo );
        }

        void Callback( int nFoo )
        {
                ... blah blah blah ...
        }

Quote:
};

Do you mean that the API you're using has no provision for an "lUserData"
argument that it retains and passes to your callback?  If so, then the best
tactic depends on whether you have one CFoo object or many.

If one CFoo, just store it in some global variable g_pFoo.

class CFoo
{
...
        static void RawCallback( int nFoo, LONG lUserData );
        {
                g_pFoo->Callback( nFoo );
        }

If many CFoo objects, then RawCallback() must have _some_ argument to let
you determine the appropriate CFoo to use. (If not, the API or your
approach to using it with CFoo is flawed).  For instance, maybe it has a
"WORD wUserData" arugment, not big enough to hold a real CFoo*.  In that
case, try using a CDict< WORD, CFoo* > to lookup the appropriate CFoo*
given the WORD id.  Basically that's how MFC associates CWnd*s with HWNDs.

        Greg



Wed, 20 Dec 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Passing C++ Class Member Function to as a C Callback Function Parameter

2. class member function as callback function

3. How to use member function of a C++ class as a callback function

4. Accessing class member functions from Global Callback

5. Making DDEML Callback function a member of a class

6. CALLBACK DLGPROC as a class member function

7. Making DDEML Callback function a member of a class

8. Getting pointer to non-static member function from C callback function

9. Callback functions as member functions

10. Calling C++ member function through C function callback

11. Using Non-Static Callback Functions as member Functions VC5.0

12. Class creating another class and needing callback function

 

 
Powered by phpBB® Forum Software