[Openmcl-devel] FFI with functions that return (pointers to) structures

Jeremy O'Donoghue jeremy.odonoghue at gmail.com
Mon Feb 21 01:29:05 PST 2005


I'm a relative CL-newbie (but many years of C/C++), so apologies for a
possibly obvious question. I'm having trouble using the FFI with
functions returning pointers to C structures, and it seems like the
(rapidly improving) OpenMCL documentation doesn't quite cover this in
detail.

As a (short) example, I'm trying to put the return value of the
syscall gtgrgid() into a class, for easier interaction with lisp
functions.

The C signature is: struct group *getgrgid(gid_t gid);

struct group {
  char *gr_name;
  char *gr_passwd;
  gid_t gid;
  char **gr_mem;
};

In CL I have:

(defclass sys_group ()
  ((name :accessor name)
   (gid :accessor gid :initarg :gid
	:initform (error "Require gid to initialize"))
   (members :accessor members)))

(defmethod initialize-instance :after ((group sys_group) &key)
  (rlet ((pgroup :group))
   (progn
      (#_getgrgid pgroup (slot-value group 'gid)))))
	  (when pgroup
	  (setf (slot-value group 'name)
		(%str-from-ptr (pref pgroup :group.gr_name))))
	  (with-string-vector 
	      (setf (slot-value group 'members)
		    (%str-from-ptr (pref pgroup :group.gr_mem))))))

The extra parameter pgroup to #_getgrgid is based on the information
in section 9.2.2 of the documentation says that I need to pass a first
parameter of type 'pointer to returned struct/union' to foreign
functions returning struct/union.

The code above fails at the call to #_getgrgid with an indication that
the number of parameters is incorrect (don't recall the precise
message, and I'm not sitting at my Mac now...)
Everything following the call to #_getgrgid() is probably incorrect,
but I've never gotten as far as returning from the call to test it.

Thanks for any pointers you could give.

Jeremy O'Donoghue



More information about the Openmcl-devel mailing list