guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: FFI questions


From: Taylan Kammer
Subject: Re: FFI questions
Date: Fri, 15 May 2020 22:09:22 +0200
User-agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.8.0

On 15.05.2020 15:47, Jan Synacek wrote:
Hello,

Currently I have something like this:

(define libxcb (dynamic-link "libxcb"))

...

(define c-change-window-attributes
   (pointer->procedure void
      (dynamic-func "xcb_change_window_attributes" libxcb)
      (list '* uint32 uint32 '*)))

(define (change-window-attributes conn win mask vals)
    (c-change-window-attributes conn win mask (bytevector->pointer vals)))

The last argument to xcb_change_window_attributes is 'const void *' and I
need to pass a u32 vector to it. As it is right now, it segfaults when I
try passing #u32(something) to 'change-window-attributes'. Is it possible
to make it accept a u32 vector using just Scheme or do I have to work
around it on the C level?

I think the reason it segfaults might be that the #u32() object gets garbage collected soon after you call bytevector->pointer on it. See that you put it in a variable, and reference that variable at some point after xcb_change_windor_attributes should be done with the vector, to make sure that this is not the problem.

Otherwise I'm not sure why it should segfault. A #u32() bytevector really should be backed by a contiguous array of uint32_t values, just like what you'll get when you define a uint32_t[] in C.

My second question is about FFI and structs. Is it possible to access C
struct members from Scheme by name? If not, how do I generally approach the
problem? I've checked how guile-xcb does it and it seems to be building
hash tables with field names as keys. But that basically requires me to
"redefine" all the C structs that I would be interested in at the Scheme
level.

These might be useful:

https://github.com/TaylanUB/scheme-bytestructures

https://www.nongnu.org/nyacc/ffi-help.html

I maintain bytestructures, am happy to answer questions. (If I don't respond on the mailing list, feel free to mail me directly or open an issue on GitHub; mails with address@hidden and address@hidden in the To: or Cc: fields land in a folder that I don't always check.)

The FFI Helper is by Matt Wette who also hangs around on this mailing list I believe.


- Taylan



reply via email to

[Prev in Thread] Current Thread [Next in Thread]