This is the mail archive of the libffi-discuss@sourceware.org mailing list for the libffi project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Function Callbacks?


Is there a trivial example demonstrating how to use closures to
achieve this?  From all the documentation, it definitely seems to be
the solution I am looking for, however I have not managed to wrap my
head around how it works.

I found an example of using closures here:
http://www.atmark-techno.com/~yashi/libffi.html#Closure-Example

But this does not seem to be quite what I am trying to achieve, as it
has actual hard coded function definitions and nowhere is there a call
to ffi_call.

To be clear, what I want is to have a function (in a separate lib), like:

int (*mytest)(int);
extern "C" int test(void * ptr)
{
 mytest = (int (*)(int))ptr;
 return mytest(4);
}

I can use libffi to easily call this function, but I need to react
(and ideally return a value, but mostly react) to the call to mytest
(which is not a function that actually exists).

Any help achieving this is very much appreciated.




On Thu, Feb 7, 2013 at 8:51 AM, The Devils Jester
<thedevilsjester@gmail.com> wrote:
> Thank you (both).
>
> A quick browse of libffi closures seems to confirm that they are
> indeed what I am looking for.
>
> On Thu, Feb 7, 2013 at 8:43 AM, Anthony Green <green@moxielogic.com> wrote:
>> On Thu, Feb 7, 2013 at 9:29 AM, The Devils Jester
>> <thedevilsjester@gmail.com> wrote:
>>> I use libffi because I do not know the details of a particular
>>> function at compile time.  libffi lets me programatically
>>> collect/generate the needed data and call a function with this data,
>>> which can all be collected from an external source.
>>>
>>> If one of these details happens to be a function call back, which also
>>> has its details (return type, arguments, etc...) unknown at compile
>>> time (and in fact, most likely doesn't even exist), how can I
>>> 'properly' interact with this function if I have to react to a call to
>>> its 'callback'?
>>>
>>> Does libffi have a way to achieve this?
>>
>> If I understand you right, I think what you are trying to accomplish
>> can be done with a libffi closure object.  You would pass the closure
>> pointer as an argument to your first function.   The closure callback
>> would then decode the arguments based on a runtime defined function
>> definition and handle things appropriately.
>>
>> AG


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]