如何在 cffi 中将 NULL 作为 :string 发送
我正在尝试从 lisp 端使用 wayland-client,这是我的代码:
(define-foreign-library wayland-client
(:unix (:or "libwayland-client.so.0.20.0" "libwayland-client.so"))
(t (:default "libwayland-client")))
(use-foreign-library wayland-client)
(defcfun "wl_display_connect" :pointer
(name :string))
(wl-display-connect (null-pointer));;return NULL
C 语言代码,来自 here
#include <stdio.h>
#include <stdlib.h>
#include <wayland-client.h>
struct wl_display *display = NULL;
int main(int argc, char **argv) {
display = wl_display_connect(NULL);
if (display == NULL) {
fprintf(stderr, "Can't connect to display\n");
exit(1);
}
printf("connected to display\n");
wl_display_disconnect(display);
printf("disconnected from display\n");
exit(0);
}
我已经测试了 C 版本并且它有效,那么 lisp 版本应该如何正确?
struct wl_display* wl_display_connect(const char *name)
I'm trying to use wayland-client from lisp side, this is my code:
(define-foreign-library wayland-client
(:unix (:or "libwayland-client.so.0.20.0" "libwayland-client.so"))
(t (:default "libwayland-client")))
(use-foreign-library wayland-client)
(defcfun "wl_display_connect" :pointer
(name :string))
(wl-display-connect (null-pointer));;return NULL
Code in C, come from here
#include <stdio.h>
#include <stdlib.h>
#include <wayland-client.h>
struct wl_display *display = NULL;
int main(int argc, char **argv) {
display = wl_display_connect(NULL);
if (display == NULL) {
fprintf(stderr, "Can't connect to display\n");
exit(1);
}
printf("connected to display\n");
wl_display_disconnect(display);
printf("disconnected from display\n");
exit(0);
}
I have test the C one and it worked, how should the lisp one be correct?
How wl_display_connect
been defined from here
struct wl_display* wl_display_connect(const char *name)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不是 CFFI 方面的专家,但我认为诀窍是说事情是
(:pointer :char)
而不是:string
。给定这个简单的 C 函数:以及这个 CFFI 声明:
那么这将起作用:
然后您可以使用外部字符串调用它,但您需要处理它们的分配和取消分配:
(ts "foo")
行不通的。但这将:现在:
显然在您的情况下,您不需要处理从外部字符串的转换。
I am not an expert on CFFI but I think the trick is to say that things are
(:pointer :char)
s not:string
s. Given this trivial C function:And this CFFI declaration:
Then this will work:
And you can then call it with foreign strings, but you need to deal with allocating and deallocating them:
(ts "foo")
won't work. But this will:And now:
Obviously in your case you don't need to deal with the conversion back from the foreign string.
您可以使用
(cffi:null-pointer)
因为 string 是指向 char 数组的指针。You can use
(cffi:null-pointer)
since string is a pointer to char array.