The Second Cut

Start the Open Dialog in "My Computer"

This question is occasionally asked, and it seems like a tough one. That's because "My Computer" is 1) A virtual folder, not a real one, and 2) The user can change its' name to something else. So even if you could get the "path" for it, you can't be sure what the name is. This solution should work just as well in a pure API call as in BCB. The only VCL code below is the last assignment statement. For my first effort, I used this code. It's based on an old Delphi post by Christian Piene Gundersen. I've put an '*' next to the constants that "worked", and an 'x' next to the ones that failed. Don't forget #include "shlobj.h".


int i;
LPITEMIDLIST PIDL;
char path[MAX_PATH+1];

i=SHGetSpecialFolderLocation(0,CSIDL_TEMPLATES,&PIDL);
if(i==NOERROR)
  {
  SHGetPathFromIDList(PIDL,path);
  CoTaskMemFree(PIDL);
  OpenDialog1->InitialDir=path;
  }
/*
CSIDL_BITBUCKET  x
CSIDL_CONTROLS  x
CSIDL_DESKTOP  *
CSIDL_DESKTOPDIRECTORY  *
CSIDL_DRIVES x
CSIDL_FONTS  *
CSIDL_NETHOOD *
CSIDL_NETWORK x
CSIDL_PERSONAL *
CSIDL_PRINTERS  x
CSIDL_PROGRAMS  *
CSIDL_RECENT    *
CSIDL_SENDTO     *
CSIDL_STARTMENU  *
CSIDL_STARTUP   *
CSIDL_TEMPLATES  *
CSIDL_FAVORITES*
*/

But there's a much better way. It was described by Brad Martinez in a VB group in March, 1999. I recommend you read the entire thread, because Brad provides an afterthought in his second reply. (Here's Brad's personal site.) He observed that many M$ programs accept objects, as well as paths in their input fields. His particular example is Explorer, but the Common Dialogs apparently do the same. And one way to describe an object is by its' Class ID, in GUID form. This reduces the code to a single line, with no COM code at all. Open a File|New|Application and drop an Open Dialog on it, from the Dialogs palette. Write the code to invoke the dialog, but preceede it with:


OpenDialog1->InitialDir="::{20D04FE0-3AEA-1069-A2D8-08002B30309D}";
// My Computer             {20D04FE0-3AEA-1069-A2D8-08002B30309D}
// Network Neighborhood    {208D2C60-3AEA-1069-A2D7-08002B30309D}
// Recycled                {645FF040-5081-101B-9F08-00AA002F954E}

I've included two CLSID's besides My Computer. But "Recycled" is an example of Brad's warning that you should not use some "folders" for this purpose. If you use the CLSID for Recycled, the Open Dialog will open properly, but you'll see that the files listed in the listview window come up with completely different names in the edit box of the Open Dialog. That's certainly related to the fact that the Recyle Bin "knows" where each file or folder came from, and how to put it all back there on demand. I shudder to think what might happen if you work with those files without using the "normal" COM linkage for the Recycle Bin! Brad also points out that Net Hood might fail if the user did not install any networking...

But "My Computer" seems safe, and has always worked for me.

Back to BCB Examples



Copyright © 2003 Timothy H. Buchman
Trademark notices     Privacy statement
Back to Home
 
Published: November 14, 2003
Modified: July 8, 2005