native c header:
typedef HANDLE HCAMERA;
int Begin(HCAMERA* h);
int End(HCAMERA h);
HANDLE is defined:
typedef void *HANDLE;
native c source I want:
HCAMERA h;
int r = 0;
r = Begin(&h);
VERIFY(r);
r = End(h);
VERIFY(r);
I tried following code in sbcl 1.3.1 but not working.
(cffi:use-foreign-library "camera.dll")
(cffi:defcfun "Begin" :int
(handle :pointer))
(cffi:defcfun "End" :int
(handle :pointer))
(defparameter *camera* (cffi:foreign-alloc :pointer)) ; alloc handle
(cffi:with-foreign-object (handle :pointer)
(setf (cffi:mem-ref handle :pointer) *camera*) ; handle address
(Begin handle)
(End *camera*))
BTW: HOW TO GET THE ADDRESS OF THE FOREIGN OBJECT (camera)? AM I DOING IT RIGHT?
I finally figured out using following code:
You can get the address like this:
If you have this C file
you can see, e.g. by this common lisp file, that you get the same address from lisp and C for
handle
. It isn't the same for*camera*
because it is passed by value. I tried it on Linux, but I think it should be the same on Windows, just changecamera.so
tocamera.dll
.Possible pitfall: If I use this lisp code from Emacs, I don't see the stdout from C. I executed it from the command line with
sbcl --script file.lisp
. Hope, that helps you somehow.