How to call 'printf' using libffi?

Anthony Green green@moxielogic.com
Thu Mar 18 15:25:01 GMT 2021


Here's an example:

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

int main (void)
{
  ffi_cif cif;
  void *args[4];
  ffi_type *arg_types[3];

  char *format = "%.5g, %d\n";
  double doubleArg = 3.14159;
  signed int sintArg = 7;
  ffi_arg res = 0;

  arg_types[0] = &ffi_type_pointer;
  arg_types[1] = &ffi_type_double;
  arg_types[2] = &ffi_type_sint;
  arg_types[3] = NULL;

  /* This printf call is variadic */
  ffi_prep_cif_var(&cif, FFI_DEFAULT_ABI, 1, 3, &ffi_type_sint, arg_types);

  args[0] = &format;
  args[1] = &doubleArg;
  args[2] = &sintArg;
  args[3] = NULL;

  ffi_call(&cif, FFI_FN(printf), &res, args);

  return 0;
}

On Thu, Mar 18, 2021 at 10:04 AM ShaJunxing via Libffi-discuss <
libffi-discuss@sourceware.org> wrote:

> 'printf' not only has variable length arguments (maximum length is
> unlimited?), but also each argument type may be different. I searched
> the whole Internet but still found nothing, anybody help? thank you very
> much.
>
>
>


More information about the Libffi-discuss mailing list