unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Nala Ginrut <nalaginrut@gmail.com>
To: Mark H Weaver <mhw@netris.org>
Cc: guile-user@gnu.org
Subject: Re: dynamic ffi and C struct
Date: Sun, 20 May 2012 11:40:35 +0800	[thread overview]
Message-ID: <CAPjoZoe2nQU8geCwwSuTyQerHttKmJFvvtSLhf2-oGahwfDPFQ@mail.gmail.com> (raw)
In-Reply-To: <871umf8ytj.fsf@netris.org>

And if you want to dereference the returned struct pointer, use parse-c-struct
say, there's a returned C struct like:
aa { (int)1 ,(int)2 }

you may convert it to list like this:
(parse-c-struct aa (list int int))
==> (1 2)

Then the rest is list operation, has no part in C.


On Sun, May 20, 2012 at 8:45 AM, Mark H Weaver <mhw@netris.org> wrote:
> cong gu <gucong43216@gmail.com> writes:
>> Is it possible to use the current dynamic ffi to call a C function
>> whose parameter is a C struct (not a pointer to a C struct) ?
>
> Although it is not documented in the manual, I see from the source code
> that a C struct parameter can be specified using a nested list in the
> foreign type passed to 'pointer->procedure'.
>
>> For example, what should I do for the following function?
>>
>> typedef struct {
>>   int dat[2];
>> } foo_t;
>>
>> int func ( foo_t arg );
>
> For this example, there's an additional complication of the inline array
> within the struct, which is not supported by libffi, upon which Guile's
> dynamic FFI is based.  I'm not an expert on this subject, but I strongly
> suspect that on most (if not all) platforms, the struct above has the
> same layout as the following one:
>
>  typedef struct {
>    int dat_0;
>    int dat_1;
>  } foo_t;
>
> Assuming this is the case, here's one way to wrap 'func':
>
>  (use-modules (system foreign))
>
>  (define foo-library-obj (dynamic-link "libfoo"))
>  (define foo-struct-type (list int int))
>
>  (define (make-foo-struct dat-0 dat-1)
>    (make-c-struct foo-struct-type (list dat-0 dat-1)))
>
>  (define func
>    (pointer->procedure int
>                        (dynamic-func "func" foo-library-obj)
>                        (list foo-struct-type)))
>
> and here's an example of how to use it:
>
>  (func (make-foo-struct 4 5))
>
> It is also possible to wrap C functions that return structs, by passing
> a list to the first parameter of 'pointer->procedure', and then using
> 'parse-c-struct' to extract the fields of the returned struct.
>
> Hope this helps!
>
>    Mark
>



      reply	other threads:[~2012-05-20  3:40 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-19 11:32 dynamic ffi and C struct cong gu
2012-05-20  0:45 ` Mark H Weaver
2012-05-20  3:40   ` Nala Ginrut [this message]

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

  List information: https://www.gnu.org/software/guile/

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

  git send-email \
    --in-reply-to=CAPjoZoe2nQU8geCwwSuTyQerHttKmJFvvtSLhf2-oGahwfDPFQ@mail.gmail.com \
    --to=nalaginrut@gmail.com \
    --cc=guile-user@gnu.org \
    --cc=mhw@netris.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).