如何调用本地c函数与Windows HANDLE共同lisp / cffi

本机c头:

typedef HANDLE HCAMERA; int Begin(HCAMERA* h); int End(HCAMERA h); 

HANDLE被定义为:

 typedef void *HANDLE; 

我想要的本地c源代码:

 HCAMERA h; int r = 0; r = Begin(&h); VERIFY(r); r = End(h); VERIFY(r); 

我在sbcl 1.3.1中试过下面的代码,但没有工作。

 (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*)) 

顺便说一句:如何获取外部物体相机 )的地址? 上午我做对了吗?

Solutions Collecting From Web of "如何调用本地c函数与Windows HANDLE共同lisp / cffi"

你可以得到这样的地址:

 (defun get-foreign-address (obj) (write-to-string (cffi:pointer-address obj) :base 16)) 

如果你有这个C文件

 #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; }; 

你可以看到,例如,通过这个普通的lisp文件,你从lisp和C获得了相同的地址来handle*camera*是不一样的,因为它是按价值传递的。 我在Linux上试了一下,但是我认为它应该和Windows一样,只要将camera.so更改为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) 

可能的错误:如果我使用Emacs中的lisp代码,我没有看到来自C的stdout。我使用sbcl --script file.lisp从命令行执行它。 希望,这可以帮助你。

我终于想通了下面的代码:

 (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))))