gpt4 book ai didi

Delphi TPath.GetTempPath 结果被裁剪

转载 作者:行者123 更新时间:2023-12-03 14:43:44 28 4
gpt4 key购买 nike

我正在使用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;
[...]

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

有人可以解释一下吗?

最佳答案

国土安全页面上有关于此 Windows API 函数的注释:

"The return buffer for GetLongPathName() and similar functions might return a truncated path and lead to hard-to-find errors."

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

如果您有源代码,您可以检查一下Delphi 2010实现中是否存在本文所述的问题。

关于Delphi TPath.GetTempPath 结果被裁剪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3440492/

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