Search code examples
windowslispcommon-lispsbclcffi

How to call native c function with windows HANDLE in common lisp / cffi


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?


Solution

  • You can get the address like this:

    (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, 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 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 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.