This is the mail archive of the libffi-discuss@sourceware.org mailing list for the libffi project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Passing function pointer to fcall


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 ?Â


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]