raku - 如何在 Perl6 NativeCall 结构中定义固定长度字符串?

标签 raku nativecall

我有一个第三方 C 库,它定义了类似于以下内容的结构:

struct myStruct {
  int a;
  int b;
  char str1[32];
  char str2[32];
};

还有一个函数,它接受指向该结构的指针并填充它。我需要 Perl6 native 调用来提供该结构,然后读取结果。

到目前为止,我在 Perl6 中定义的结构如下:

class myStruct is repr('CStruct') {
  has int32 $.a;
  has int32 $.b;
  has Str $.str1; # Option A: This won't work as Perl won't know what length to allocate
  has CArray[uint8] $.str2; # Option B: This makes more sense, but again how to define length?  
                     # Also, would this allocate the array in place, or 
                     #    reference an array that is separately allocated (and therefore not valid)?
}

原生调用如下:

sub fillStruct(myStruct) is native('test_lib') { ... }
my $struct = myStruct.new();
fillStruct($struct); # Gives a seg fault with any variation I've tried so far

我怎样才能做到这一点?

最佳答案

正如其他人所说,目前似乎没有任何方法可以实现这一目标。

我已经采用定义新的 C 函数作为解决方法。该函数有效地充当访问器方法,仅返回我需要的字段作为离散的 NativeCall 友好指针。

希望社区能够在某个时候为该案例提供适当的支持。

关于raku - 如何在 Perl6 NativeCall 结构中定义固定长度字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46481221/

相关文章:

sequence - 为什么 Perl 6 序列 'A' ... 'AA' 只有一个元素?

raku - NativeCall 加载我不调用的库符号

winapi - 将复杂结构传递给 Windows API

raku - 在 NativeCall perl6 模块中声明指针 [void] 的奇怪消息

使用 fcntl 从 stdout 创建新文件描述符在文件中失败

regex - perl6 中的变量量词

role - 为什么 Perl 6 Str 扮演 Positional 角色,如何更改 []?

asynchronous - 运行 Perl6 套接字服务器时 MOAR 进程膨胀

mixins - for什么时候调用迭代器方法?

raku - 在 Perl 6 中声明 "native"类型