如何在 common lisp / cffi 中使用 windows HANDLE 调用本机 c 函数
How to call native c function with windows HANDLE in common 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*))
顺便说一句:如何获取异物(相机)的地址?我做得对吗?
您可以获得这样的地址:
(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 的标准输出。我使用 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))))
原生 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*))
顺便说一句:如何获取异物(相机)的地址?我做得对吗?
您可以获得这样的地址:
(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 的标准输出。我使用 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))))