6

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

#!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"after $path

[编辑] 多次调用的结果:

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

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

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

[9] => D:\dev\pa

[9] => D:\dev\pa

还有另一种正确的方法吗?

4

1 回答 1

3

我怀疑 MoarVM 中的错误src/strings/utf16.c,特别是第 403 行

result = MVM_realloc(result, *output_size);

这可能应该读

result = MVM_realloc(result, *output_size + 2);

如果您可以验证这是否解决了您的问题,请随时提交错误报告甚至创建拉取请求

于 2018-12-10T20:47:04.500 回答