You can get the address as follows:
(defun get-foreign-address (obj) (write-to-string (cffi:pointer-address obj) :base 16))
If you have this C file
#include <stdio.h> typedef void *HANDLE; typedef HANDLE HCAMERA; int Begin(HCAMERA* h); int End(HCAMERA h); int Begin(HCAMERA* h) { printf("Address from Begin: %p\n", h); return 0; }; int End(HCAMERA h) { printf("Address from End: %p\n", (void*)&h); return 0; };
you can see for example. in this generic lisp file, you will get the same address from lisp and C for handle
. This is not the same for *camera*
because it is passed by value. I tried this on Linux, but I think it should be the same on Windows, just change camera.so
to camera.dll
.
(cffi:use-foreign-library "camera.so") (cffi:defcfun "Begin" :int (handle :pointer)) (cffi:defcfun "End" :int (handle :pointer)) (cffi:defcvar ("stdout" stdout) :pointer) (defparameter *camera* (cffi:foreign-alloc :pointer)) (cffi:with-foreign-object (handle :pointer) (format t "Address from Lisp: ~a~%" (get-foreign-address handle)) (Begin handle) (format t "Address from Lisp: ~a~%" (get-foreign-address *camera*)) (End *camera*)) (cffi:foreign-funcall "fflush" :pointer stdout :int)
Possible error: if I use this lisp code from Emacs, I cannot see stdout from C. I executed it from the command line using sbcl --script file.lisp
. Hope this helps you.
source share