This manual page is for Mac OS X version 10.6

If you are running a different version of Mac OS X, view the documentation locally:

  • In Terminal, using the man(1) command

Reading manual pages

Manual pages are intended as a quick reference for people who already understand a technology.

  • For more information about the manual page format, see the manual page for manpages(5).

  • For more information about this technology, look for other documentation in the Apple Reference Library.

  • For general information about writing shell scripts, read Shell Scripting Primer.



ffi_call(3)                             BSD Library Functions Manual                             ffi_call(3)

NAME
     ffi_call -- Invoke a foreign function.

SYNOPSIS
     #include <ffi/ffi.h>

     void
     ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue);

DESCRIPTION
     The ffi_call function provides a simple mechanism for invoking a function without requiring knowledge
     of the function's interface at compile time.  fn is called with the values retrieved from the pointers
     in the avalue array. The return value from fn is placed in storage pointed to by rvalue.  cif contains
     information describing the data types, sizes and alignments of the arguments to and return value from
     fn, and must be initialized with ffi_prep_cif before it is used with ffi_call.

     rvalue must point to storage that is sizeof(long) or larger. For smaller return value sizes, the
     ffi_arg or ffi_sarg integral type must be used to hold the return value.

EXAMPLES
     #define MACOSX  // for fficonfig.h on Darwin

     #include <ffi/ffi.h>
     #include <stdio.h>

     unsigned char
     foo(unsigned int, float);

     int
     main(int argc, const char **argv)
     {
         ffi_cif cif;
         ffi_type *arg_types[2];
         void *arg_values[2];
         ffi_status status;

         // Because the return value from foo() is smaller than sizeof(long), it
         // must be passed as ffi_arg or ffi_sarg.
         ffi_arg result;

         // Specify the data type of each argument. Available types are defined
         // in <ffi/ffi.h>.
         arg_types[0] = &ffi_type_uint;
         arg_types[1] = &ffi_type_float;

         // Prepare the ffi_cif structure.
         if ((status = ffi_prep_cif(&cif, FFI_DEFAULT_ABI,
             2, &ffi_type_uint8, arg_types)) != FFI_OK)
         {
             // Handle the ffi_status error.
         }

         // Specify the values of each argument.
         unsigned int arg1 = 42;
         float arg2 = 5.1;

         arg_values[0] = &arg1;
         arg_values[1] = &arg2;

         // Invoke the function.
         ffi_call(&cif, FFI_FN(foo), &result, arg_values);

         // The ffi_arg 'result' now contains the unsigned char returned from foo(),
         // which can be accessed by a typecast.
         printf("result is %hhu", (unsigned char)result);

         return 0;
     }

     // The target function.
     unsigned char
     foo(unsigned int x, float y)
     {
         unsigned char result = x - y;
         return result;
     }

SEE ALSO
     ffi(3), ffi_prep_cif(3)

Darwin                                          July 20, 2007                                         Darwin

Reporting Problems

The way to report a problem with this manual page depends on the type of problem:

Content errors
Report errors in the content of this documentation with the feedback links below.
Bug reports
Report bugs in the functionality of the described tool or API through Bug Reporter.
Formatting problems
Report formatting mistakes in the online version of these pages with the feedback links below.