我有一个第三方 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/