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

NAME

4     ffi_call — Invoke a foreign function.
5

SYNOPSIS

7     #include <ffi.h>
8
9     void
10     ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue);
11

DESCRIPTION

13     The ffi_call function provides a simple mechanism for invoking a function
14     without requiring knowledge of the function's interface at compile time.
15     fn is called with the values retrieved from the pointers in the avalue
16     array. The return value from fn is placed in storage pointed to by
17     rvalue.  cif contains information describing the data types, sizes and
18     alignments of the arguments to and return value from fn, and must be ini‐
19     tialized with ffi_prep_cif before it is used with ffi_call.
20
21     rvalue must point to storage that is sizeof(ffi_arg) or larger for non-
22     floating point types. For smaller-sized return value types, the ffi_arg
23     or ffi_sarg integral type must be used to hold the return value.
24

EXAMPLES

26     #include <ffi.h>
27     #include <stdio.h>
28
29     unsigned char
30     foo(unsigned int, float);
31
32     int
33     main(int argc, const char **argv)
34     {
35         ffi_cif cif;
36         ffi_type *arg_types[2];
37         void *arg_values[2];
38         ffi_status status;
39
40         // Because the return value from foo() is smaller than sizeof(long), it
41         // must be passed as ffi_arg or ffi_sarg.
42         ffi_arg result;
43
44         // Specify the data type of each argument. Available types are defined
45         // in <ffi/ffi.h>.
46         arg_types[0] = &ffi_type_uint;
47         arg_types[1] = &ffi_type_float;
48
49         // Prepare the ffi_cif structure.
50         if ((status = ffi_prep_cif(&cif, FFI_DEFAULT_ABI,
51             2, &ffi_type_uint8, arg_types)) != FFI_OK)
52         {
53             // Handle the ffi_status error.
54         }
55
56         // Specify the values of each argument.
57         unsigned int arg1 = 42;
58         float arg2 = 5.1;
59
60         arg_values[0] = &arg1;
61         arg_values[1] = &arg2;
62
63         // Invoke the function.
64         ffi_call(&cif, FFI_FN(foo), &result, arg_values);
65
66         // The ffi_arg 'result' now contains the unsigned char returned from foo(),
67         // which can be accessed by a typecast.
68         printf("result is %hhu", (unsigned char)result);
69
70         return 0;
71     }
72
73     // The target function.
74     unsigned char
75     foo(unsigned int x, float y)
76     {
77         unsigned char result = x - y;
78         return result;
79     }
80

SEE ALSO

82     ffi(3), ffi_prep_cif(3)
83
84                               February 15, 2008
Impressum