问题描述
我有一个第三方 C 库,它定义了一个类似于以下内容的结构:
I have a third-party C library that defines a struct similar to:
struct myStruct {
int a;
int b;
char str1[32];
char str2[32];
};
还有一个函数,它接受一个指向这个结构体的指针并填充它.我需要我的 Perl6 本机调用来提供该结构,然后读取结果.
And a function that takes a pointer to this struct and populates it. I need my Perl6 native call to provide that struct, and then read the results.
到目前为止,我已经在 Perl6 中定义了结构体:
So far I've got the struct defined in Perl6 as:
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)?
}
还有一个原生调用,例如:
And a native call like:
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
我怎样才能做到这一点?
How can I make this work?
推荐答案
正如其他人所说,目前似乎没有任何方法可以实现这一点.
As others have said, there does not appear to be any way to achieve this at present.
我求助于定义一个新的 C 函数作为解决方法.该函数有效地充当访问器方法,仅返回我需要的作为离散 NativeCall 友好指针的字段.
I've resorted to defining a new C function(s) as a workaround. The function effectively acts as an accessor method returning just the fields that I need as discrete NativeCall-friendly pointers.
希望社区在某个时候能够为此案例提供适当的支持.
Hopefully the community will get to implementing proper support for this case at some point.
这篇关于如何在 Perl6 NativeCall 结构中定义固定长度的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!