Skip to content

Instantly share code, notes, and snippets.

@TooTallNate
Created January 7, 2012 20:12
Show Gist options
  • Save TooTallNate/1575877 to your computer and use it in GitHub Desktop.
Save TooTallNate/1575877 to your computer and use it in GitHub Desktop.
The example from the libffi docs for how to use `ffi_closure` with `ffi_closure_alloc()` and `ffi_closure_free()`.
#include <stdio.h>
#include <ffi.h>
/* Acts like puts with the file given at time of enclosure. */
void puts_binding(ffi_cif *cif, unsigned int *ret, void* args[],
FILE *stream)
{
*ret = fputs(*(char **)args[0], stream);
}
int main()
{
ffi_cif cif;
ffi_type *args[1];
ffi_closure *closure;
int (*bound_puts)(char *);
int rc;
/* Allocate closure and bound_puts */
closure = ffi_closure_alloc(sizeof(ffi_closure), &bound_puts);
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)
{
/* Initialize the closure, setting stream to stdout */
if (ffi_prep_closure_loc(closure, &cif, puts_binding,
stdout, bound_puts) == FFI_OK)
{
printf("Before bound_put()\n");
rc = bound_puts("Hello World!");
/* rc now holds the result of the call to fputs */
}
}
}
/* Deallocate both closure, and bound_puts */
ffi_closure_free(closure);
return 0;
}
@Str1ker17
Copy link

How this can be used to call a function without compile-time knowledge of its name and arguments?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment