public inbox for libffi-discuss@sourceware.org
 help / color / mirror / Atom feed
From: wp1068189-ssc <stefan.sonnenberg@pythonmeister.com>
To: "libffi-discuss@sourceware.org" <libffi-discuss@sourceware.org>
Subject: Passing function pointer to fcall
Date: Sun, 27 Nov 2011 00:06:00 -0000	[thread overview]
Message-ID: <198099805.151376.1322352353393.JavaMail.open-xchange@ox.hosteurope.de> (raw)


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 ? 

             reply	other threads:[~2011-11-27  0:06 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-27  0:06 wp1068189-ssc [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=198099805.151376.1322352353393.JavaMail.open-xchange@ox.hosteurope.de \
    --to=stefan.sonnenberg@pythonmeister.com \
    --cc=libffi-discuss@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).