Need help with pointers to class methods being used as callbacks...

S

SpreadTooThin

I have a C routine that wants to call a method of its user.
In this case the method is a method inside a class.
The C routine is passed a void * which can be used by the user any way
they like.
I use that parameter and pass the actual instance of the C++ class
that performed the call.
The C callback handler then uses the instance of the class and calls
the appropriate method.

Here is the code but it doesn't work. What am I doing wrong?

Code:
void DeviceFoundCallBack(void * userRefCon,
IOBluetoothDeviceInquiryRef inquiryRef, IOBluetoothDeviceRef
deviceRef)
{
	userRefCon->DeviceFoundCallBack(inquiryRef, deviceRef);
}

void CompleteCallback(void * userRefCon, IOBluetoothDeviceInquiryRef
inquiryRef, IOBluetoothDeviceRef deviceRef)
{
	userRefCon->CompleteCallBack(inquiryRef, deviceRef);
}

void BlueTooth::startSearch(void)
{
	_inquiry =
IOBluetoothDeviceInquiryCreateWithCallbackRefCon(this);  // This will
be passed back to us to let us know the instance.
	IOBluetoothDeviceInquirySetDeviceFoundCallback(_inquiry,
&DeviceFoundCallBack);
}
 
V

Victor Bazarov

SpreadTooThin said:
I have a C routine that wants to call a method of its user.
In this case the method is a method inside a class.
The C routine is passed a void * which can be used by the user any way
they like.
I use that parameter and pass the actual instance of the C++ class
that performed the call.
The C callback handler then uses the instance of the class and calls
the appropriate method.

Here is the code but it doesn't work. What am I doing wrong?

Code:
void DeviceFoundCallBack(void * userRefCon,
IOBluetoothDeviceInquiryRef inquiryRef, IOBluetoothDeviceRef
deviceRef)
{
userRefCon->DeviceFoundCallBack(inquiryRef, deviceRef);[/QUOTE]

'userRefCon' has to be of a class type or pointer to a class to use
the -> syntax.  You have a void*, which is not a class.  You need to
convert your 'void*' into a pointer to the class:

    someclasstype* pObj = static_cast<someclasstype*>(userRefCon);

only then you will be alble to call your member function.
[QUOTE]
}

void CompleteCallback(void * userRefCon, IOBluetoothDeviceInquiryRef
inquiryRef, IOBluetoothDeviceRef deviceRef)
{
userRefCon->CompleteCallBack(inquiryRef, deviceRef);
}

void BlueTooth::startSearch(void)
{
_inquiry =
IOBluetoothDeviceInquiryCreateWithCallbackRefCon(this);  // This will
be passed back to us to let us know the instance.
IOBluetoothDeviceInquirySetDeviceFoundCallback(_inquiry,
&DeviceFoundCallBack);[/QUOTE]

You may have problems with the second argument here.  See the FAQ about
the callbacks and member functions.
[QUOTE]
}

V
 
A

Andre Kostur

I have a C routine that wants to call a method of its user.

Uh.. then it's not really a C routine is it? C routines would have no
concept of "method"s. (It may be a C++ routine with C linkage....)
In this case the method is a method inside a class.
The C routine is passed a void * which can be used by the user any way
they like.
I use that parameter and pass the actual instance of the C++ class
that performed the call.
The C callback handler then uses the instance of the class and calls
the appropriate method.

Here is the code but it doesn't work. What am I doing wrong?

Code:
void DeviceFoundCallBack(void * userRefCon,
IOBluetoothDeviceInquiryRef inquiryRef, IOBluetoothDeviceRef
deviceRef)
{
userRefCon->DeviceFoundCallBack(inquiryRef, deviceRef);
}[/QUOTE]

userRefCon is a void*.  voids don't have methods.  You need to cast that 
pointer back to the appropriate type first.
[QUOTE]
void CompleteCallback(void * userRefCon, IOBluetoothDeviceInquiryRef
inquiryRef, IOBluetoothDeviceRef deviceRef)
{
userRefCon->CompleteCallBack(inquiryRef, deviceRef);
}[/QUOTE]

Same as above.
[QUOTE]
void BlueTooth::startSearch(void)
{
_inquiry =
IOBluetoothDeviceInquiryCreateWithCallbackRefCon(this);  // This will
be passed back to us to let us know the instance.
IOBluetoothDeviceInquirySetDeviceFoundCallback(_inquiry,
&DeviceFoundCallBack);
}
 
B

Bart van Ingen Schenau

SpreadTooThin said:
I have a C routine that wants to call a method of its user.
In this case the method is a method inside a class.
The C routine is passed a void * which can be used by the user any way
they like.
I use that parameter and pass the actual instance of the C++ class
that performed the call.
The C callback handler then uses the instance of the class and calls
the appropriate method.

Here is the code but it doesn't work. What am I doing wrong?

Code:
void DeviceFoundCallBack(void * userRefCon,
IOBluetoothDeviceInquiryRef inquiryRef, IOBluetoothDeviceRef
deviceRef)
{
userRefCon->DeviceFoundCallBack(inquiryRef, deviceRef);[/QUOTE]

userRefCon is a void* and does not have a member called
DeviceFoundCallBack.
You must use a cast in this situation to tell the compiler what type of
object userRefCon actually points to.

[QUOTE]
}

void CompleteCallback(void * userRefCon, IOBluetoothDeviceInquiryRef
inquiryRef, IOBluetoothDeviceRef deviceRef)
{
userRefCon->CompleteCallBack(inquiryRef, deviceRef);[/QUOTE]

Same problem here.
[QUOTE]
}

void BlueTooth::startSearch(void)
{
_inquiry =
IOBluetoothDeviceInquiryCreateWithCallbackRefCon(this);  // This will
be passed back to us to let us know the instance.
IOBluetoothDeviceInquirySetDeviceFoundCallback(_inquiry,
&DeviceFoundCallBack);
}

Bart v Ingen Schenau
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top