Hi. I want to use the following C function:
;; void ClearBackground(Color color);
Color is C Struct:
;; typedef struct Color { ;; unsigned char r; // Color red value ;; unsigned char g; // Color green value ;; unsigned char b; // Color blue value ;; unsigned char a; // Color alpha value ;; } Color;
So I wrote:
(cffi:defcstruct %color (r :uint8) (g :uint8) (b :uint8) (a :uint8))
(defvar *%lightgray* (cffi:foreign-alloc '(:struct %color)))
(cffi:with-foreign-slots ((r g b a) *%lightgray* (:struct %color)) (setf (cffi:foreign-slot-value *%lightgray* '(:struct %color) 'r) 200) (setf (cffi:foreign-slot-value *%lightgray* '(:struct %color) 'g) 200) (setf (cffi:foreign-slot-value *%lightgray* '(:struct %color) 'b) 200) (setf (cffi:foreign-slot-value *%lightgray* '(:struct %color) 'a) 255) (format t "~s ~s ~s ~s" r g b a))
;; void ClearBackground(Color color); (cffi:defcfun ("ClearBackground" %clear-background) :void (color (:struct %color)))
(defun clear-background (color) (%clear-background color))
But when i run
(clear-background *%lightgray*)
That does not work, I have already added my .asd file ":cffi-libffi" as dependency.
What is the problem? If this issue has no solution, is there an alternate way?
* oleg harput CADvZWFL1TxpgbO9OzkJCwgDbXmKgB3TLwpeA7711tykgXH+npA@mail.gmail.com Wrote on Thu, 8 Jun 2023 17:05:29 +0300
Hi. I want to use the following C function:
;; void ClearBackground(Color color);
[snip]
(cffi:defcstruct %color (r :uint8) (g :uint8) (b :uint8) (a :uint8))
(defvar *%lightgray* (cffi:foreign-alloc '(:struct %color)))
;; void ClearBackground(Color color); (cffi:defcfun ("ClearBackground" %clear-background) :void (color (:struct %color)))
(defun clear-background (color) (%clear-background color))
But when i run
(clear-background *%lightgray*)
You are passing a pointer to a function which takes a struct by value. you should be trying
``` (clear-background (cffi:mem-ref *%lightgray* '(:struct %color))) ```
That does not work, I have already added my .asd file ":cffi-libffi" as dependency.
What is the problem? If this issue has no solution, is there an alternate way?
Does the proposed form? If it does not, please follow up.
---