🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #13286

SHBrowseForFolder, BFFM_SETSELECTION, UNC paths (Windows bug?)

#13286
From: Dennis Jones
Date: Sat, 18 Oct 2003 04:16
130 lines
4782 bytes
Hello,

There are a significant number of newsgroup threads regarding the problem of
initializing SHBrowseForFolder() so that it correctly displays UNC paths
when the dialog first appears.  However, after following many suggestions,
and using lots of code found in the newsgroup archives, I still cannot get
it to work correctly.  One person alluded to a possible bug in Win NT/2K,
but then quickly recanted when he found that it would not work in Win98
either.  My latest version of code that is SUPPOSED to work follows.  Does
anyone know why this does not work?  Is there a bug in Windows that prevents
this code form functioning properly?

I am using BCB5.

Thanks,
- Dennis

HRESULT SHPathToPIDL(LPCTSTR szPath, LPITEMIDLIST* ppidl)
{
  LPSHELLFOLDER pShellFolder = NULL;
  OLECHAR wszPath[MAX_PATH] = {0};
  ULONG nCharsParsed = 0;

  // Get an IShellFolder interface pointer
  HRESULT hr = SHGetDesktopFolder(&pShellFolder);
  if(FAILED(hr))
    return hr;

  // Convert the path name to Unicode
  MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szPath, -1, wszPath,MAX_PATH);

  // Call ParseDisplayName() to do the job
  hr = pShellFolder->ParseDisplayName(NULL, NULL, wszPath,&nCharsParsed,
ppidl, NULL);

  // Clean up
  pShellFolder->Release();

  return hr;
}
//-------------------------------------------------------------------------

// Callback for the BrowseForFolder, which purpose is to set the initial
folder
int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM /*lParam*/,
LPARAM lpData)
{
  LPITEMIDLIST  pidl;

  SHPathToPIDL( (char *)lpData, &pidl );

  switch (uMsg) {
    case BFFM_INITIALIZED:
      // this does not work for UNC paths
//      ::SendMessage(hwnd, BFFM_SETSELECTION, true, lpData);

      // this is SUPPOSED to work for UNC paths, but still does not!
      ::SendMessage(hwnd, BFFM_SETSELECTION, false, (LPARAM)pidl);
      break;
  }

  // Global pointer to the shell's IMalloc interface.
  LPMALLOC g_pMalloc;

  // Get the shell's allocator.
  if (!SUCCEEDED(SHGetMalloc(&g_pMalloc)))
    return false;

  g_pMalloc->Free(pidl);

  return 0;
}
//-------------------------------------------------------------------------

bool BrowseForFolder(HWND hWnd, char *buf, char *prompt)
{
  // Global pointer to the shell's IMalloc interface.
  LPMALLOC g_pMalloc;

  // Get the shell's allocator.
  if (!SUCCEEDED(SHGetMalloc(&g_pMalloc)))
    return false;

  BROWSEINFO   bi;
  LPITEMIDLIST  pidlMyComputer;  // PIDL for MyComputer folder
  LPITEMIDLIST  pidlBrowse;      // PIDL selected by user

  // Get the PIDL for the Desktop folder.
  if (!SUCCEEDED(SHGetSpecialFolderLocation(hWnd, CSIDL_DESKTOP,
&pidlMyComputer)))
    return false;

  // Fill in the BROWSEINFO structure.
  bi.hwndOwner = hWnd;
  bi.pidlRoot = pidlMyComputer;
  bi.pszDisplayName = buf;
  bi.lpszTitle = prompt;
  bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_RETURNFSANCESTORS;
  bi.lpfn = BrowseCallbackProc;
  bi.lParam = (LPARAM)buf;

  // Browse for a folder and return its PIDL.
  pidlBrowse = SHBrowseForFolder(&bi);
  bool result = (pidlBrowse != 0);
  if (result) {
   if (!SHGetPathFromIDList(pidlBrowse, buf))
     result = false;

    // Free the PIDL returned by SHBrowseForFolder.
    g_pMalloc->Free(pidlBrowse);
  }

  // Free the PIDL returned by SHGetSpecialFolderLocation
  g_pMalloc->Free(pidlMyComputer);

  return result;
}
//-------------------------------------------------------------------------

void __fastcall TSettingsDialog::btnBrowseClick(TObject *Sender)
{
  char buf[MAX_PATH];
  strcpy( buf, editPath->Text.c_str() );
  if ( BrowseForFolder( Handle, buf, "Please choose a location for the log
file(s):" ) )
  {
    editPath->Text = buf;
  }
}
//-------------------------------------------------------------------------
Technical Details
Message-ID: <3f90be9d@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,8dd37dadfc9c37c0,start
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2003-10-17 21:38:56 PST
From: "Dennis Jones" <djones@nospam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: SHBrowseForFolder, BFFM_SETSELECTION, UNC paths (Windows bug?)
Date: Fri, 17 Oct 2003 21:16:30 -0700
Lines: 132
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
NNTP-Posting-Host: 204.200.27.7
Message-ID: <3f90be9d@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1066450589 204.200.27.7 (17 Oct 2003 21:16:29 -0700)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail