This is the mail archive of the
libffi-discuss@sourceware.org
mailing list for the libffi project.
Passing function pointer to fcall
- From: wp1068189-ssc <stefan dot sonnenberg at pythonmeister dot com>
- To: "libffi-discuss at sourceware dot org" <libffi-discuss at sourceware dot org>
- Date: Sun, 27 Nov 2011 01:05:52 +0100 (CET)
- Subject: Passing function pointer to fcall
- Reply-to: wp1068189-ssc <stefan dot sonnenberg at pythonmeister dot com>
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 ?Â