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

杀马特。学长 韩版系。学妹 提交于 2019-12-24 14:07:24

问题


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?


回答1:


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.




回答2:


I finally figured out using following code:

(defparameter *camera-handle* (cffi:null-pointer))
(defun camera-open ()
  (unless (cffi:null-pointer-p *camera-handle*)
    (EndHVDevice (cffi:mem-ref *camera-handle* :pointer))
    (cffi:foreign-free *camera-handle*))
  (setf *camera-handle* (cffi:foreign-alloc :pointer))
  (BeginHVDevice *camera-handle*))

(defun camera-close ()
  (unless (cffi:null-pointer-p *camera-handle*)
    (EndHVDevice (cffi:mem-ref *camera-handle* :pointer))
    (cffi:foreign-free *camera-handle*)
    (setf *camera-handle* (cffi:null-pointer))))


来源:https://stackoverflow.com/questions/34646657/how-to-call-native-c-function-with-windows-handle-in-common-lisp-cffi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!