RE: Passing C Style Function Callbacks in Cocoa
RE: Passing C Style Function Callbacks in Cocoa
- Subject: RE: Passing C Style Function Callbacks in Cocoa
- From: email@hidden
- Date: Fri, 5 Mar 2004 16:47:41 -0000
Hi Vince,
You can't actually pass an objects method as a C callback function.
This is what I did to overcome this. I have a C library which I use to
provide access to a USB device, this has a 'NotifyDeviceChange' function
with a 'DeviceChangeNotifyProc' callback.
I pass a context pointer into the function which is passed back to me in the
callback, when I call the notify function from the Obj-C object I use the
self pointer as the context, then in the callback function I dereference the
context to my Obj-C class and send it messages:
// callback func
void DeviceChangeNotifyProc( int nHandle, int nType, void *pContext )
{
MyObj *myObj = (MyObj *) pContext;
[pContext DeviceChangeWithHandle:nHandle andType:nType];
}
// class member
- (void) TestCallback
{
NotifyDeviceChange( self );
}
- (void) DeviceChangeWithHandle:(int:nHandle andType:(int)nType
{
// perform action on hardware here (depends on type)
}
All of this relies on you having control of all the source code of course :)
Alun Carp
Driver Development Team Leader
Data Encryption Systems Limited
>
-----Original Message-----
>
From: email@hidden
>
[mailto:email@hidden] On Behalf Of Vince Ackerman
>
Sent: 05 March 2004 16:09
>
To: email@hidden
>
Subject: Re: Passing C Style Function Callbacks in Cocoa
>
>
To clarify, this is the C style function I need to call from
>
inside a method in myObj. I would like to replace the
>
function pointer setStepBoundCB to call back a method of
>
myObj instead. Is this possible?
>
>
>
Error = TStep_boundCallbackSet( tStepper, setStepBoundCB);
>
>
>
It calls-back the following function which is declared outside myObj.
>
>
DeviceError setStepBoundCB( TStepper* tStepper, bool bound) {
>
>
// Set an instance variable of myObj here
>
;
>
>
}
>
>
Thanks
>
>
Vince
>
_______________________________________________
>
cocoa-dev mailing list | email@hidden
>
Help/Unsubscribe/Archives:
>
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.