public inbox for libffi-discuss@sourceware.org
 help / color / mirror / Atom feed
From: ShaJunxing <shajunxing@163.com>
To: Anthony Green <green@moxielogic.com>
Cc: libffi-discuss <libffi-discuss@sourceware.org>
Subject: Re: How to call 'printf' using libffi?
Date: Fri, 19 Mar 2021 10:08:29 +0800	[thread overview]
Message-ID: <0b7f2827-e209-2c58-7118-9fec4770ae1c@163.com> (raw)
In-Reply-To: <CACxje5-Us3WkCtDZwOzWik7PL_YK0LU7tf6_X+tW5R5mQiPK1A@mail.gmail.com>

在 2021/3/18 下午11:25, Anthony Green 写道:
> Here's an example:
>
> #include <ffi.h>
> #include <stdio.h>
>
> int main (void)
> {
>   ffi_cif cif;
>   void *args[4];
>   ffi_type *arg_types[3];
>
>   char *format = "%.5g, %d\n";
>   double doubleArg = 3.14159;
>   signed int sintArg = 7;
>   ffi_arg res = 0;
>
>   arg_types[0] = &ffi_type_pointer;
>   arg_types[1] = &ffi_type_double;
>   arg_types[2] = &ffi_type_sint;
>   arg_types[3] = NULL;
>
>   /* This printf call is variadic */
>   ffi_prep_cif_var(&cif, FFI_DEFAULT_ABI, 1, 3, &ffi_type_sint, 
> arg_types);
>
>   args[0] = &format;
>   args[1] = &doubleArg;
>   args[2] = &sintArg;
>   args[3] = NULL;
>
>   ffi_call(&cif, FFI_FN(printf), &res, args);
>
>   return 0;
> }
>
> On Thu, Mar 18, 2021 at 10:04 AM ShaJunxing via Libffi-discuss 
> <libffi-discuss@sourceware.org <mailto:libffi-discuss@sourceware.org>> 
> wrote:
>
>     'printf' not only has variable length arguments (maximum length is
>     unlimited?), but also each argument type may be different. I searched
>     the whole Internet but still found nothing, anybody help? thank
>     you very
>     much.
>
>
Thank you for your answer, really appreciate it. libffi is so amazing it 
makes possible to call almost any shared libraries with even non C 
languages and without any header files and compilations, I love it so much.

Yesterday after sending email I also realized maybe different parameters 
needs different cif, and I did some experiments, it exactly works. But I 
got two more problems. The first is, I found ffi_prep_cif() works fine 
in this situation, so what is ffi_prep_cif_var() different from? The 
second is, I found printf() cannot properly handle float type in my 
machine, my code is:

#include <ffi.h>
#include <stdio.h>
intmain() {
ffi_cif cif;
ffi_type *atypes[2] = {&ffi_type_pointer, &ffi_type_float};
if(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &ffi_type_sint, atypes) == 
FFI_OK) {
char*s = "hello %f\n";
floatf = 3.14;
void*avalues[2] = {&s, &f};
ffi_arg rvalue;
ffi_call(&cif, (void*)printf, &rvalue, avalues);
}
return0;
}

It always outputs zero (maybe other unexpected result). I changed the 
second argument to double, char *, int, long ... all correct, except float.

My machine is "Linux X230 4.9.0-14-amd64 #1 SMP Debian 4.9.246-2 
(2020-12-17) x86_64 GNU/Linux" and libc version is "GNU C Library 
(Debian GLIBC 2.24-11+deb9u4) stable release version 2.24".

Could you please give me a direction?


  reply	other threads:[~2021-03-19  2:09 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-18 14:04 ShaJunxing
2021-03-18 15:25 ` Anthony Green
2021-03-19  2:08   ` ShaJunxing [this message]
2021-03-19  2:36     ` Kaz Kylheku (libffi)
2021-03-19  3:09       ` ShaJunxing
2021-03-19 10:13   ` Andrew Haley
2021-03-19 11:01     ` Anthony Green
2021-03-19 11:43       ` Andrew Haley
2021-03-20 22:40         ` Anthony Green
2021-03-20 23:56           ` Anthony Green
2021-03-22 10:36             ` Andrew Haley
2021-03-19 11:26     ` Jarkko Hietaniemi
2021-03-19 11:33       ` Anthony Green

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=0b7f2827-e209-2c58-7118-9fec4770ae1c@163.com \
    --to=shajunxing@163.com \
    --cc=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).