Author: Jonas Bilinkevicius
In a TOpenDialog, you can set the initial directory. How do I set it as the
desktop? I could set it as c:\windows\desktop, but then what if Windows is not on
the user's c drive?
Answer:
There is a shell function that can be used to inquire about the location of several
shell-related folders. You need to add ShlObj to the uses clause for this, plus
ActiveX for the CoTaskMemFree.
1 2 procedure FreePidl(pidl: PItemIDList);
3 begin4 CoTaskMemFree(pidl);
5 end;
6 7 procedure TForm1.Button2Click(Sender: TObject);
8 var9 pidl: PItemIDList;
10 buf: array[0..MAX_PATH] of Char;
11 begin12 if Succeeded(ShGetSpecialFolderLocation(Handle, CSIDL_DESKTOP, pidl)) then13 begin14 if ShGetPathfromIDList(pidl, buf) then15 ShowMessage(buf);
16 FreePIDL(pidl);
17 end;
18 end;
See win32.hlp (or better msdn.microsoft.com, the list has been extended for Win98/2000) for a list of CSIDL values. There is also a newer ShGetSpecialFolderPath API that directly returns the path, but it is not available on older Win95 and NT installations.