#include #include /* Acts like puts with the file given at time of enclosure. */ void puts_binding(ffi_cif *cif, long *ret, void* args[], FILE *stream) { *ret = fputs(*(char **)args[0], stream); } int main(void) { ffi_cif cif; ffi_type *args[1]; ffi_closure *closure; void *executable; int (*bound_puts)(char *); int rc; /* Allocate closure and bound_puts */ closure = ffi_closure_alloc(sizeof(ffi_closure) * 100, &executable); if (closure) { /* Initialize the argument info vectors */ args[0] = &ffi_type_pointer; /* Initialize the cif */ if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 1, &ffi_type_uint, args) == FFI_OK) { int N = 43; /* Initialize the closure, setting stream to stdout */ if (ffi_prep_closure_loc(closure + N, &cif, (void *)puts_binding, stdout, ((ffi_closure *)executable) + N) == FFI_OK) { bound_puts = (void *)(((ffi_closure *)executable) + N); printf("Before bound_put()\n"); rc = bound_puts("Hello World!\n"); /* rc now holds the result of the call to fputs */ printf("Result is %d\n", rc); } } } /* Deallocate both closure, and bound_puts */ ffi_closure_free(closure); return 0; }