Delphi TPath.GetTempPath Result Truncated

I am using Delphi 2010, and my program wants to get the system time path. I use TPath.GetTempPath and everything works fine ... at least for me and my colleagues. But on some client machines, this method returns a clipped path that (of course) does not exist. I found out that the problem seems to be the result of a basic call to GetLongPathName ().

The full code is as follows:

[...] 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 is going on here.

Can anyone tell about this?

+6
winapi path delphi delphi-2010
source share
2 answers

There is a note about this feature of the Windows API on the "Homeland Security" pages:

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

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

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

+4
source share

What happens if you try:

 var longpath : string; SetLength(longpath,MAX_PATH); SetLength(longpath, GetLongPathName(PChar(TmpDir),PChar(LongPath),MAX_PATH)); 

It worked for me, your version truncated the way.

+3
source share

All Articles