2

(Delphi 2006) I am getting the Common documents folder in order to create another folder off it during my app startup. This has been working fine - it always returns:

C:\Documents and Settings\All Users\Documents\

but I have just received a bug report from a Spanish user that includes a startup log that shows the app was trying to create:

MyApp\  

instead of:

C:\Documents and Settings\All Users\Documents\MyApp\ 

i.e. the common docs folder string was empty. The code to get this is:

function GetCommonDocumentsFolder : TFilename ;

begin
Result := GetSystemPath (CSIDL_COMMON_DOCUMENTS) ;
end ;

I also note in my researching of this problem that there is also a system call:

SHGetSpecialFolderPath 

Which one should I be using? GetSystemPath (CSIDL_COMMON_DOCUMENTS) has worked for me (at least in English locale Windows XP).

So 2 questions really, possibly related:

  • why does GetSystemPath (CSIDL_COMMON_DOCUMENTS) return null?
  • should I in fact be using SHGetSpecialFolderPath ?

(boy, this was a hard one to find tags for)

Source for the mysterious GetSystemPath:

function GetSystemPath (Folder: Integer) : TFilename ;

{   Call this function with one of the constants declared above. }

var
    PIDL    : PItemIDList ;
    Path    : LPSTR ;
    AMalloc : IMalloc ;

begin
Path := StrAlloc (MAX_PATH) ;
SHGetSpecialFolderLocation (Application.Handle, Folder, PIDL) ;
if SHGetPathFromIDList (PIDL, Path) then
    begin
    Result := IncludeTrailingPathDelimiter (Path) ;
    end
else
    begin
    Result := '' ;
    end ;    ;
SHGetMalloc(AMalloc) ;
AMalloc.Free (PIDL) ;
StrDispose (Path) ;
end;
rossmcm
  • 5,493
  • 10
  • 55
  • 118
  • So, ultimately, you're asking whether the use an API function, or a library function that calls a different API function. That doesn't really matter — they both do the same thing. What you *should* be doing is checking the API function's return value so you know whether you have a valid path. Your library function unhelpfully returns an empty string instead of telling you that it couldn't do what you requested. – Rob Kennedy Mar 10 '11 at 04:07

1 Answers1

1

You should call SHGetSpecialFolderPath when you want to know the path corresponding to a CSIDL.

I don't know what GetSpecialFolderPath is, I can't find it in my Delphi. Did you mean SHGetSpecialFolderPath? I also can't find GetSystemPath, but that doesn't change my answer!

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I did mean SHGetSpecialFolderPath. Thanks. The reason why you couldn't find GetSystemPath is because (so I've just discovered) it's a routine I must have got from somewhere - I've added it to the question. It probably answers the question - I imagine it's failing for some reason and returning a null string. I guess SHGetSpecialFolderPath will do the job without failing under Spanish Windows XP. My Bad. (or more accurately, My Stupidity) – rossmcm Mar 09 '11 at 20:12
  • @ross That explains it. Just use SHGetSpecialFolderPath and all will be well! – David Heffernan Mar 09 '11 at 20:13