Calling C++ member function through C function callback 
Author Message
 Calling C++ member function through C function callback

I need to call a C function in a library from a C++ member function.
One of the parameters to this C function is a pointer to a callback
function. The callback must be a member function of the same
class that I am calling from. Is this even possible? I can't
get it to compile using dynamic_cast, or even _cast.

Short example:

// This is the C function I want to call. The callback needs to
// be another member function of the same class I'm calling from.
extern void FuncWithCallback(int parm1, void (*callback)(char *data));

class CMyClass {
...
private:
        // Here's the function I want FuncWithCallback to call:
        void (*callback)(char *data);

Quote:
}

CMyClass::CMyClass()
{
        ...

        int parm1=0;
        // Here's where I call the function that will call my callback()
        // when events I am interested in occur.
        FuncWithCallback(parm1, callback);      // gets compile error

Quote:
}

CMyClass::callback(char *data)
{
        // Here's where I process whatever event caused
FuncWithCallback()
        // to call this function.

Quote:
}

Ideas? I've tried the usual tricks of casting to void *, and just
can't seem to outwit Visual C++. It really is necessary to do it this
way; the FuncWithCallback() opens a TCP/UDP socket, and callback()
is called when the socket gets some UDP data. I need a separate
function for each instance of CMyClass.

--

My email name is 'mikey'.



Fri, 19 Jan 2001 03:00:00 GMT  
 Calling C++ member function through C function callback

Quote:

>Short example:

>// This is the C function I want to call. The callback needs to
>// be another member function of the same class I'm calling from.
>extern void FuncWithCallback(int parm1, void (*callback)(char *data));

>class CMyClass {
>...
>private:
>        // Here's the function I want FuncWithCallback to call:
>        void (*callback)(char *data);
>}

    Have you written FuncWithCallback() (or can you change the way it
works)?

    Normally, the setup function for a call back takes two parameters: a
32-bit identifier (which I think is parm1 in your example), and the address
of the callback function.  When that function is called, the ID is passed as
well as the relevant data.

    Assuming that this is the case (or FuncWithCallback can be altered to
handle things that way), add a *static* member function to CMyClass, which
we'll call "realcallback" which takes those two parameters.  Then alter
CMyClass to read as follows:

CMyClass::CMyClass()
{
        FuncWithCallback(this, realcallback);      // gets compile error

Quote:
}

CMyClass::realcallback(DWORD id, char *data)
{
    CMyClass*    that = static_cast<CMyClass*> id;
    that->callback(data);

Quote:
}

--
Truth,
   James [MVP]
http://www.NJTheater.Com       -and-
http://www.NJTheater.Com/JamesCurran

begin 666 James M. Curran.vcf


M4DLZ.SLQ."!*;VAN(%-T+"!3=6ET92 R0CM";&]O;69I96QD.TY*.S W,# S
M+34Q-#D-"DQ!0D5,.U=/4DL[14Y#3T1)3D<]455/5$5$+5!224Y404),13HQ
M."!*;VAN(%-T+"!3=6ET92 R0CTP1#TP04)L;V]M9FEE;&0L($Y*(# W,# S
M+34Q-#D-"E523#IH='1P.B\O=W=W+DY*5&AE871E<BYC;VTO2F%M97-#=7)R
M86X-"E523#IH='1P.B\O=W=W+DY*5&AE871E<BYC;VT-"D5-04E,.U!2148[
M24Y415).150Z2F%M97-#=7)R86Y 0V]M<'5397)V92YC;VT-"E)%5CHQ.3DX

`
end



Fri, 19 Jan 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

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

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

3. Call C++ member function from C function ?

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

5. Callback functions as member functions

6. class member function as callback function

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

8. Using a C++ member function as a Timer callback

9. Using a C++ member function as a Timer callback

10. Calling C++ function from C callback

11. Calling C++ Class Member Functions from C

12. how do you pass in a call back function to a member fnc in C++

 

 
Powered by phpBB® Forum Software