8

我尝试用 perl6 包装 sd-bus,但是函数调用三重指针有问题。
来自 sd-bus.h

int sd_bus_list_names(sd_bus *bus, char ***acquired, char ***activatable); /* free the results */

尝试本地调用

sub sd_bus_list_names(Pointer, Pointer[CArray[Str]] , Pointer[CArray[Str]] ) returns int32 is native('systemd') {*}

我打电话,但我不知道如何取消对 array(@) 获取和可激活变量的引用。

谢谢,对不起我的英语

[编辑]
dwarring 回复解决了我对 derefencies Pointer[CArray[Str]] 的问题

这是一个测试代码:

use v6;
use NativeCall;
sub strerror(int32) returns Str is native {*}
sub sd_bus_default_system(Pointer is rw) returns int32 is native('systemd') {*}
sub sd_bus_unref(Pointer) returns Pointer is native('systemd') {*}
sub sd_bus_list_names(Pointer,Pointer[CArray[Str]] is rw, Pointer[CArray[Str]] is rw ) returns int32 is native('systemd') {*}

my Pointer $bus .= new;
my int32 $error;

$error=sd_bus_default_system($bus);
if $error < 0 {
    my Str $ser = strerror(-$error);
    die "fail, can't test triple pointer, dbus return error $error $ser";
}

my Pointer[CArray[Str]] $acq .= new;
my Pointer[CArray[Str]] $act .= new;
$error=sd_bus_list_names($bus,$acq,$act);

my @love6acq;
loop (my $i = 0; $acq.deref[$i]; $i++){
    @love6acq.push: $acq.deref[$i];
}
@love6acq.say;

my @love6act;
loop (my $i = 0; $act.deref[$i]; $i++){
    @love6act.push: $act.deref[$i];
}
@love6act.say;

sd_bus_unref($bus);
4

1 回答 1

9

以下独立实验对我有用:

C代码:

#include <stdio.h>
static char* strs[3] = { "howdy", "doody", NULL };
extern void ptr_to_strs (char ***ptr) {
  *ptr = strs;
}

乐库代码:

use v6;
use LibraryMake;
use NativeCall;

sub testlib {
    state $ = do {
    my $so = get-vars('')<SO>;
    ~(%?RESOURCES{"lib/test$so"});
    }
}

sub ptr_to_strs(Pointer[CArray[Str]] $strs is rw) is native(&testlib)

my Pointer[CArray[Str]] $a .= new;
ptr_to_strs($a);
say $a.deref[0]; # howdy
say $a.deref[1]; # doody
say $a.deref[2]; # (Str)

使用这种方法(但未经测试),您需要is rw在调用之前添加到签名并创建指针:

# assuming you've already got a $bus object
sub sd_bus_list_names(Pointer, Pointer[CArray[Str]] is rw, Pointer[CArray[Str]] is rw) returns int32 is native('systemd') {*}

my Pointer[CArray[Str]] $acq .= new;
my Pointer[CArray[Str]] $act .= new;
sd_bus_list_names($bus, $acq, $act);
say $acq.deref[0]; # first acquired name
于 2018-05-28T01:11:16.257 回答