public inbox for libffi-discuss@sourceware.org
 help / color / mirror / Atom feed
* Passing function pointer to fcall
@ 2011-11-27  0:06 wp1068189-ssc
  2011-11-27  4:05 ` Anthony Green
  0 siblings, 1 reply; 6+ messages in thread
From: wp1068189-ssc @ 2011-11-27  0:06 UTC (permalink / raw)
  To: libffi-discuss


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 ? 

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2011-12-07 13:19 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-11-27  0:06 Passing function pointer to fcall wp1068189-ssc
2011-11-27  4:05 ` Anthony Green
2011-11-27  7:33   ` wp1068189-ssc
2011-11-27 14:12     ` Stefan Sonnenberg-Carstens
2011-12-07 12:01       ` Anthony Green
2011-12-07 13:19         ` wp1068189-ssc

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).