5 .Nd Invoke a foreign function.
11 .Fa "void (*fn)(void)"
18 function provides a simple mechanism for invoking a function without
19 requiring knowledge of the function's interface at compile time.
21 is called with the values retrieved from the pointers in the
23 array. The return value from
25 is placed in storage pointed to by
28 contains information describing the data types, sizes and alignments of the
29 arguments to and return value from
31 and must be initialized with
33 before it is used with
37 must point to storage that is sizeof(ffi_arg) or larger for non-floating point
38 types. For smaller-sized return value types, the
42 integral type must be used to hold
50 foo(unsigned int, float);
53 main(int argc, const char **argv)
56 ffi_type *arg_types[2];
60 // Because the return value from foo() is smaller than sizeof(long), it
61 // must be passed as ffi_arg or ffi_sarg.
64 // Specify the data type of each argument. Available types are defined
66 arg_types[0] = &ffi_type_uint;
67 arg_types[1] = &ffi_type_float;
69 // Prepare the ffi_cif structure.
70 if ((status = ffi_prep_cif(&cif, FFI_DEFAULT_ABI,
71 2, &ffi_type_uint8, arg_types)) != FFI_OK)
73 // Handle the ffi_status error.
76 // Specify the values of each argument.
77 unsigned int arg1 = 42;
80 arg_values[0] = &arg1;
81 arg_values[1] = &arg2;
83 // Invoke the function.
84 ffi_call(&cif, FFI_FN(foo), &result, arg_values);
86 // The ffi_arg 'result' now contains the unsigned char returned from foo(),
87 // which can be accessed by a typecast.
88 printf("result is %hhu", (unsigned char)result);
93 // The target function.
95 foo(unsigned int x, float y)
97 unsigned char result = x - y;