native c header:
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*))
顺便说一句:如何获得外国对象的地址(相机)?我做得对吗?
答案 0 :(得分:1)
你可以得到这样的地址:
(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
从命令行执行它。希望,这会以某种方式帮助你。
答案 1 :(得分:0)
我终于想出了以下代码:
(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))))