我正在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
还有另一种正确的方法吗?