Delphi TPath.GetTempPath结果被裁剪

Pat*_*ick 9 delphi winapi path delphi-2010

我正在使用Delphi 2010,我的程序想要获得系统的临时路径.我正在使用TPath.GetTempPath,一切都很好......至少对我和我的同事来说.但是在某些客户机器上,此方法返回一条裁剪路径(当然)不存在.我发现问题似乎是来自GetLongPathName()的底层调用的结果.

完整的代码如下所示:

[...]
var
 TmpDir : String;
 Len : Integer;
begin

 [... Call to GetTempPath succeeds and we have a valid temp directory in short "~" notation in var TmpDir ...]

 Len := GetLongPathName(PChar(TmpDir), nil, 0);      // Len = 37
    SetLength(TmpDir, Len - 1);                         // We want to set the len of TmpDir to 37 - 1.
    GetLongPathName(PChar(TmpDir), PChar(TmpDir), Len); // Only 32 (instead of 36) characters are copied - so we have a cropped path - But why?!

end;
[...]
Run Code Online (Sandbox Code Playgroud)

这只发生在一些系统上,我不知道为什么.我找到了一个讨厌的解决方法,但我想知道这里发生了什么.

有人可以对此有所了解吗?

mjn*_*mjn 6

Homeland Security页面上有关于此Windows API功能的说明:

"GetLongPathName()和类似函数的返回缓冲区可能返回截断的路径,导致难以发现的错误."

https://buildsecurityin.us-cert.gov/bsi-rules/home/g1/753-BSI.html

如果您有源代码,则可以检查Delphi 2010实现中是否存在本文中描述的问题.