Delphi TPath.GetTempPath结果被裁剪 [英] Delphi TPath.GetTempPath result is cropped

查看:481
本文介绍了Delphi TPath.GetTempPath结果被裁剪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



完整的代码如下所示:

  [...] 
var
TmpDir:String;
Len:整数;
begin

[...调用GetTempPath成功,我们有一个有效的临时目录在var TmpDir ...

Len := GetLongPathName(PChar(TmpDir),nil,0); // Len = 37
SetLength(TmpDir,Len-1); //我们要将TmpDir的len设置为37 - 1.
GetLongPathName(PChar(TmpDir),PChar(TmpDir),Len); //只有32个(而不是36个)字符被复制 - 所以我们有一个裁剪的路径 - 但为什么?

end;
[...]

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



有人可以点亮这个吗?

解决方案

在国土安全页面上有关于Windows API函数的注意事项:



的返回缓​​冲区GetLongPathName()和类似的
函数可能会返回截断的
路径,并导致难以找到的错误。


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



如果你有源代码,您可以检查本文中描述的问题是否存在于Delphi 2010实施中。


I am using Delphi 2010 and my program wants to get the system's temp path. I am using TPath.GetTempPath and everything is working fine... at least for me and my coworkers. But on some customer machines this method returns a cropped path which is (of course) not existing. I found out that the problem seems to be the result from underlying call to GetLongPathName().

The complete code looks like this:

[...]
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;
[...]

This only happens on some systems and I don't know why. I found a nasty workaround for this, but I would like to know what's going on here.

Can somebody put some light on this?

解决方案

There is a note about this Windows API function on the Homeland Security pages:

"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

If you have the source code, you could check if the problem described in this article exists in the Delphi 2010 implementation.

这篇关于Delphi TPath.GetTempPath结果被裁剪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆