Please take a look at this piece of code:
#include<stdio.h>
#include<ffi.h>
#include<stdlib.h>
int msg(void (*a) (char *)) {
return 23;
// but this doesn't work :-(
a("Hi, there !\n");
}
int main(int argc, char ** argv)
{
ffi_cif cif;
ffi_abi abi;
ffi_status status;
int nargs = 1;
ffi_type *rtype =&ffi_type_uint32;
ffi_type **atypes;
void **avalues;
void *result;
atypes = malloc(sizeof(ffi_type));
atypes[0] =&ffi_type_pointer;
avalues = malloc(sizeof(ffi_type_pointer));
// I want to call msg with a pointer to printf
*(void**)avalues[0] = printf;
status = ffi_prep_cif(&cif, FFI_DEFAULT_ABI, nargs, rtype, atypes);
result = (ffi_type *) malloc(sizeof(rtype->size));
if(status != FFI_OK)
printf("ffi_prep_cif failed (%i)\n",status);
ffi_call(&cif,FFI_FN(msg),result,avalues);
return(*(int *)result);
}
As you might imagine, I'd like to call a function "msg" and send it a pointer to
printf,
but that does not work.
I guess it has to do with the pointer me is constructing.
But I have absolutely no idea what I'm doing wrong.
Can anyone help, please ?