gpt4 book ai didi

utf-16 - Perl6 NativeCall with Str 被编码为 ('utf16' ) 得到随机损坏的结果

转载 作者:行者123 更新时间:2023-12-04 15:29:22 24 4
gpt4 key购买 nike

我正在映射 GetFullPathName使用 NativeCall 的 perl6 脚本中的 windows API ,因此我写了以下内容:

#!perl6
use NativeCall;
constant \WIN32_MAX_PATH = 260;

#I may use directly $path.IO.absolute()
sub Win32-GetFullPathName(
Str $lpFileName is encoded('utf16'),
int32 $nBufferLength,
#Str $lpBuffer is encoded('utf16') is rw,
Blob $lpBuffer is rw,
Str $lpFilenameIndex is rw)
returns int32
is native('kernel32.dll')
is symbol('GetFullPathNameW')
{ * }


my $path = '.';
my $fp-size = Win32-GetFullPathName(
$path, # $path ~ "\0", # <!-- this hack make it working fine
WIN32_MAX_PATH,
#my Str $fpath = ' ' x WIN32_MAX_PATH;
my $buffer = buf16.allocate(WIN32_MAX_PATH),
Str );
put "[$fp-size] => ", $buffer.decode('utf16').substr(0, $fp-size);
代码随机运行,直到我附加一个 "\0"之后 $path .
[编辑] 多次调用的结果:

[12] => D:\dev\pa.t

[12] => D:\dev\pa.

[12] => D:\dev\pa.槟

[9] => D:\dev\pa

[9] => D:\dev\pa


有没有另一种正确的方法来做到这一点?

最佳答案

我怀疑 src/strings/utf16.c 中存在 MoarVM 错误,特别是line 403 :

result = MVM_realloc(result, *output_size);

这可能应该读
result = MVM_realloc(result, *output_size + 2);

如果您可以验证这是否解决了您的问题,请随时联系 file a bug report甚至 create a pull request .

关于utf-16 - Perl6 NativeCall with Str 被编码为 ('utf16' ) 得到随机损坏的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53691576/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com