问题描述
本地c标头:
typedef HANDLE HCAMERA;
int Begin(HCAMERA* h);
int End(HCAMERA h);
HANDLE已定义:
HANDLE is defined:
typedef void *HANDLE;
我想要的本地c源:
HCAMERA h;
int r = 0;
r = Begin(&h);
VERIFY(r);
r = End(h);
VERIFY(r);
我尝试了sbcl 1.3.1中的以下代码,但无法正常工作.
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?
推荐答案
您可以这样获得地址:
(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
.
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)
可能的陷阱:如果我使用Emacs的lisp代码,则看不到C语言的标准输出.我使用sbcl --script file.lisp
从命令行执行了它.希望能以某种方式对您有所帮助.
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.
这篇关于如何在常见的Lisp/cffi中使用Windows HANDLE调用本机C函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!