public inbox for libffi-discuss@sourceware.org
 help / color / mirror / Atom feed
From: Anthony Green <green@moxielogic.com>
To: libffi-discuss@sourceware.org
Subject: Re: Passing function pointer to fcall
Date: Sun, 27 Nov 2011 04:05:00 -0000	[thread overview]
Message-ID: <4ED1B703.4050708@moxielogic.com> (raw)
In-Reply-To: <198099805.151376.1322352353393.JavaMail.open-xchange@ox.hosteurope.de>

On 11/26/2011 7:05 PM, wp1068189-ssc wrote:
> 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 ?


Let's get the obvious one of out the way... you are returning from msg() 
before calling the function.

I'm going to assume that's a typo or something.

What platform are you working on?  Also, you should declare "a" in msg() 
to have the same prototype as printf.

Also, there's an oddity in the libffi API, and that is that "result" 
needs to be the largest native integral type on your system.  So, use a 
long for result instead of mallocing the exact return type size.  You 
can pass it into ffi_call as &result and simply cast it to an int at the 
end.

Anthony Green

  reply	other threads:[~2011-11-27  4:05 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-27  0:06 wp1068189-ssc
2011-11-27  4:05 ` Anthony Green [this message]
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=4ED1B703.4050708@moxielogic.com \
    --to=green@moxielogic.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).